4

I am creating listing of content using tableselect form type like as follow.

function partners_list($form,&$form_state){
        $header = array(
            'name'=>array('data' => t('Name'),'field' => 'ept.name'),
            'location'=>array('data' => t('Location'),'field' => 'ept.location'),
        );

        $form['search_ew_partner'] = array(
            '#type' => 'textfield',
            '#prefix' => '<div class="serachOuter "><div class="grayBg"><div class="searchInner clearfix"><div class="searchField pull-left">',
            '#suffix' => '</div>',
            '#default_value'=> $form_state['values']['search_ew_partner'],
            '#attributes' => array(
                'placeholder' => 'Search by name'
            )
        );

        $form['submit'] = array(
            '#type' => 'submit',
            '#value' => t('Search'),

            '#prefix' => '<div class="btnOuter clearfix">',
            '#suffix' => '<a href="/partners/list" class="btn grayBtn">Reset</a></div></div></div></div>',

            '#attributes' => array(
                'class' => array('btn blueBtn')
            )
        );

        if(empty($form_state['input'])){
            $results = partners_list_submit($form,$form_state);
            $results=$results['filter_result'];
        }else{
            $results=$form_state['filter_result'];
        }

        $options = array();
        if(!empty($results)){
            foreach($results as $result){
                $options[$result->id] = array(
                    'name' => array('data' => array('#type' => 'link', '#title' => $result->name, '#href' => "partner/details/".$result->id)),
                    'location' => ucfirst($result->location),
                );
            }
        }      

        $form['ew_partners'] = array(
            '#type' => 'tableselect',
            '#header' => $header,
            '#options' => $options,
            '#empty' => t('No partners.'),
            '#attributes' => array(
                'class' => array('table')
            )
        );

        $form['pager'] = array('#markup' => theme('pager'));        
        return $form;
    }

    function partners_list_submit(&$form,&$form_state){
        $form_state['rebuild'] = true;
        $header = array(
            'name'=>array('data' => t('Name'),'field' => 'ept.name'),
            'location'=>array('data' => t('Location'),'field' => 'ept.location'),
        );

        $query = db_select('ew_partners', 'ept');
        if(!empty($form_state['values']['search_ew_partner'])){
            $query->condition('ept.name', '%' . $form_state['values']['search_ew_partner'] . '%', 'LIKE');
        }

        $query = $query->extend('PagerDefault')->extend('TableSort');
        $query->fields('ept',array('id','name','location'));
        $query->limit(2)
            ->orderByHeader($header);


        $results = $query->execute();
        $form_state['filter_result']=$results;
        $form_state['no_cache'] = true;
        return $form_state;
    }

Listing and pagination coming properly when there is no filtration on table. After filtration, pagination is not working. I think due to form_state values are reset when I clicked on next page. The page count and the result for first page is coming properly.

How can i maintain pagination with filter using tableselect?

Thank you in advance Any help appreciated.

5

The search form method type is post thats why when you are submitting the search form the form values are sending as post therefore the form values are coming properly for first page. When your are clicking on second page the form state values are reseting with new form state values as form has default value.

So rather than using post method form I will suggest to use get method type form for search so that the search keyword will always persist in url.

So you have to add

$form['#method'] = 'get';

Access all filtered values using $_GET like

$form['search_ew_partner'] = array(
            '#type' => 'textfield',
            '#prefix' => '<div class="serachOuter "><div class="grayBg"><div class="searchInner clearfix"><div class="searchField pull-left">',
            '#suffix' => '</div>',
            '#default_value'=> $_GET['search_ew_partner'],
            '#attributes' => array(
                'placeholder' => 'Search by name'
            )
        );

And change your code like

if(!empty($_GET['search_ew_partner'])){
            $query->condition('ept.name', '%' . $_GET['search_ew_partner'] . '%', 'LIKE');
        }
| improve this answer | |
  • 2
    Is it not possible with post form type? – Swapnil Patil Mar 19 '14 at 16:23

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.