Hello all,

I have a problem with the theme pager.

The situation : I have a search form. When I submit this form, I display the results in a table on the same page with a pager. This is ok for the form, and for the display of results in the table pager, but, when I change the page, the table disappear because I am working on the same page.

Summary of the code :

Form creation :

function my_module_form($form, &$form_state) {
    $form['my_form'] = array(
            '#type' => 'select',
    )        ...

    $form['submit'] = array(
            '#type' => 'submit',
            '#value' => t('OK'),
    );
    
    //with this, I can display the results of the research on the same page
    if (!empty($form_state['results_table'])) {
        $form['results_table'] = array('#markup' => $form_state['results_table']);
    }

    return $form;
}

Then the submit method :

function my_module_form_submit($form, &$form_state) {

    //get values of the research...
    ...

    //display the table pager
    $per_page = 10;
    $current_page = pager_default_initialize(count($rows), $per_page);

    // split array into page sized chunks
    $chunks = array_chunk($rows, $per_page, TRUE);

    $table_element = array(
        '#theme' => 'table',
        '#header' => $header,
        '#rows' => $chunks[$current_page],
    );

    $form_state['results_table'] = drupal_render($table_element);
    $form_state['results_table'] .= theme('pager', array('quantity',count($rows)));

    $form_state['rebuild'] = TRUE;
}

When Drupal display the results in the table, I have the pager, but if I change the page, the table will not be displayed (because of the rebuild true?)

I don't know how I can do that.

Thank's

Comments

jaypan’s picture

Unfortunately this isn't going to be easy or doable the way you are trying to do it. The issue is that the submit function is only fired when you click a submit button, not when you click links. The pager links are links, so the submit function will not be fired. You are generating the table in your submit function, which is why you will never see the table after clicking the pager links.

You haven't actually shown how your form element 'my_select' relates to the table you are building, so it's hard to give specific help. But, basically you need to do something like the following:

// Note that $selected value will only have a value when the form has been submitted
function some_form($form, &$form_state, $selected_value = FALSE)
{
    $form['my_form'] = array(
            '#type' => 'select',
    )        ...
    $form['submit'] = array(
            '#type' => 'submit',
            '#value' => t('OK'),
    );
    // $selected_value will only have a value after submit (see submit function)
    if ($selected_value) {
        // not shown - use $selected_value to do whatever it was you were
        // previously doing in your submit function
        $per_page = 10;
        $current_page = pager_default_initialize(count($rows), $per_page);
        // split array into page sized chunks
        $chunks = array_chunk($rows, $per_page, TRUE);
        $form['results_table'] = array(
           '#theme' => 'table',
           '#header' => $header,
           '#rows' => $chunks[$current_page],
        );
        $form['pager'] = array
        (
          '#theme' => 'pager',
        );
    }

    return $form;
}

function some_form_submit($form, &$form_state)
{
  // Redirect to the form, appending the selected value to the path.
  // the selected value will then become $selected_value in the form
  // definition.
  $form_state['redirect'] = 'path/to/form/' . $form_state['values']['my_form'];
}

Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.

drupal_37200’s picture

I'm not sure, I'm understand you. In the submit function, what can I do with the path 'path/to/form/' ?

And then, the "my_form" is just on the search form? Not on the results table?

jaypan’s picture

In the submit function, what can I do with the path 'path/to/form/' ?

You are accessing your form at a certain path, right? That's the path you will use to replace path/to/form.

Edit: you could also use this:

  $form_state['redirect'] = current_path() . '/' . $form_state['values']['my_form'];
And then, the "my_form" is just on the search form? Not on the results table?

I'm sorry, I don't understand what you are asking here.

Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.

drupal_37200’s picture

You are accessing your form at a certain path, right?

In fact, I am showing my form in a block, so the URL is the base URL.

jaypan’s picture

You can switch to a $_GET query then.

function some_form($form, &$form_state)
{
    $form['my_form'] = array(
            '#type' => 'select',
    )        ...
    $form['submit'] = array(
            '#type' => 'submit',
            '#value' => t('OK'),
    );
    // $_GET['selected_value'] will only be set after submit (see submit function)
    if (isset($_GET['selected_value']) {
        $selected_value = $_GET['selected_value'];
        // not shown - use $selected_value to do whatever it was you were
        // previously doing in your submit function
        $per_page = 10;
        $current_page = pager_default_initialize(count($rows), $per_page);
        // split array into page sized chunks
        $chunks = array_chunk($rows, $per_page, TRUE);
        $form['results_table'] = array(
           '#theme' => 'table',
           '#header' => $header,
           '#rows' => $chunks[$current_page],
        );
        $form['pager'] = array
        (
          '#theme' => 'pager',
        );
    }
    return $form;
}
function some_form_submit($form, &$form_state)
{
  // Redirect to the same page, setting the selected value as $_GET['selected_value']
  $form_state['redirect'] = array(current_path(), array('query' => array('selected_value' => $form_state['values']['my_form'])));
}

Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.

drupal_37200’s picture

My website name is "drupal" and I display the block which contain the form on the "content". So the URL of the page which show the form is :

/drupal/?q=en

drupal_37200’s picture

With this code :

Notice: Undefined index: selected_value on submit function

jaypan’s picture

Sorry, this line:

$form_state['redirect'] = array(current_path(), array('query' => array('selected_value' => $form_state['values']['selected_value'])));

Should be:

$form_state['redirect'] = array(current_path(), array('query' => array('selected_value' => $form_state['values']['my_form'])));

(According to your original code. You probably have to change it to whatever your actual code is)

Code edited/fixed in earlier post.

Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.

rakesh.gectcr’s picture

<?php
$per_page = 10;
// Initialize the pager
$current_page = pager_default_initialize(count($rows), $per_page);
// Split your list into page sized chunks
$chunks = array_chunk($rows, $per_page, TRUE);
// Show the appropriate items from the list
$output = theme('table', array('header' => $header, 'rows' => $chunks[$current_page]));
// Show the pager
$output .= theme('pager', array('quantity',count($rows)));
?>

---
Kind regards,

Thank you,
Rakesh James