2

I am using drupal 7

The following code turns an array into a pagered table.

$rows = $data[data];
$per_page = 30;
// 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)));
echo $output;

Is it possible to add filters to the table?

Thanks

Updated 21/04/2016 as Per Neograph734s answer

I have built a module based around the code Neograph734 gave as an example.

My array is built like

Array
(
    [0] => Array
        (
            [EventTime] => 23/04/2016 15:00
            [EventType] => Foo
            [EventName] => Bar 
            [EventRating] => 99.12
        )

    [1] => Array
        (
            [EventTime] => 23/04/2016 18:00
            [EventType] => FooFoo
            [EventName] => BarBar 
            [EventRating] => 94.84
        )

The only ammendment I have made is in field to seach as below

foreach ($rows as $key => $row) {
  if (preg_match("/" . $form_state['storage']['title'] . "/", $row['EventName'])) {
    $temp_rows[$key] = $row;
  }
}

The table initially loads, I have a text area and a submit button. When I enter a search term the page reloads but the table does not filter, it still shows all results.

The html out put is

<tbody>
<tr>
<td>23/04/2016 15:00</td>
<td>Foo</td>
<td>Bar</td>
<td>99.12</td>
</tr>

What more do I have to change to get the text field to search the results?

Thanks

  • Does it have to be programmatically? You can use Views to output a table with filters. – Neograph734 Apr 18 '16 at 20:38
  • Hi, I'm getting my data externally through an API. I don't know if there's a way for views to output an array. – Sam Healey Apr 18 '16 at 20:45
  • That would be possible, but would involve even more code. So not your best option then. – Neograph734 Apr 18 '16 at 20:48
1

It can be done by wrapping your table in a form, and then use the form API to render your table and re-render it on submit. You can have a look at this comment: https://www.drupal.org/node/1770512#comment-6428358

In short the render funtion:

function MY_MODULE_build_form($form, &$form_state) {
  // Filter field.
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title to look for'),
    '#size' => 15,
    '#default_value' => isset($form_state['storage']['title']) ? $form_state['storage']['title'] : '',
  );

  // Submit button.
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t("Find"),  
  );

  // Modify table array here. 
  // If your API supports filters, you can use that too. 
  // My regex is bad, so you might need to tweak this.
  if (isset($form_state['storage']['title'])) {
    $temp_rows = array();

    foreach ($rows as $key => $row) {
      if (preg_match("/" . $form_state['storage']['title'] . "/", $row['field_to_search'])) {
        $temp_rows[$key] = $row;
      }
    }

    $rows = $temp_rows;
  }

  // Render the table array.
  $form['table'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => t('Empty Rows')
  );      

  return $form;
}

And the Submit function

function MY_MODULE_build_form_submit($form, &$form_state) {
  // Set the provided filter value in the storage.
  $form_state['storage']['title'] = $form_state['values']['title'];
  // Show the form again.
  $form_state['rebuild'] = TRUE;  
}

You might want to fancy it up with some fieldsets, but those are left out to keep the answer understandable.

|improve this answer|||||
  • Hi thanks for the starting point, I have the table and search field in a form but the table does not filter. I have updated my answer. – Sam Healey Apr 21 '16 at 19:51
  • @SamHealey, you should add some debug code to see if the submit functions is getting called and $form_state['storage']['title'] actually receives the value of $form_state['values']['title']. Install Devel and use the dpm() function to read $form_state during the submit. – Neograph734 Apr 22 '16 at 11:42
1

DataTables module offers you a theming function (see theme_datatable in your source) or a views formatter.

It is a quite handy and popular jQuery table sort/filter/pagination plugin

But only client-side ! If you want the filter data server-side you need to use the form API too.

You can overwrite DataTables module behavior in your theme to initialize the jQuery plugins according your needs.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.