Jump to content

Admin module sorting and filtering in list view not working


sttaq

Recommended Posts

I am working on a back office module controller. I am able to display my object in a list view and also create a new one using the helper form. However, the default list view that I have, does not sort or filter anything. The list view shows the controls for sorting and filtering but clicking on them does nothing. Here is my back office controllers:

class AdminCustomController extends AdminController {

    public $module;

    public function __construct() {
        $this->table = 'custom_table';
        $this->className = 'CustomTable';
        $this->module = 'customtable';
        $this->lang = false;
        $this->bootstrap = true;
        $this->need_instance = 0;        

        // Building the list of records stored within the "test" table
        $this->fields_list = array(
            'id_custom_table' => array(
                'title' => $this->l('ID'),
                'align' => 'center',
                'width' => 25,
                'type' => 'text'
            ),
            'name' => array(
                'title' => $this->l('Name'),
                'width' => 'auto',
                'type' => 'text',
                'orderby' => true,
                'search' => true   
//the ordering and filtering controls do appear but they don't work
            ),//...some more fields
        );

        $this->_select = '....';

        $this->_join = '
        LEFT JOIN `' . _DB_PREFIX_ . 'product_lang` b ON (b.`id_product` = a.`id_product`)';

        $this->_defaultOrderBy = 'a.some_date';
        $this->_defaultOrderWay = 'ASC';

        $this->context = Context::getContext();

        parent::__construct();
    }

    public function renderForm() {

        // Building the Add/Edit form
        $this->fields_form = array(
            'submit' => array(
                'title' => $this->l('    Save   '),
                'class' => 'button'
            )
        );
        
        $this->addJqueryUI(array('ui.datepicker', 'ui.autocomplete'));
        return parent::renderForm();
    }

    public function renderList() {
        return parent::renderList();
    }
}

I have looked at code of other modules and also default PS controllers and all of them just return the parents list view unless it is modified and by default the sorting and filtering feature works fine on them. I did not see any sorting or filtering specific code and that is why my admin controller does not have any.

 

I would appreciate if you can help me understand and enable the sorting and filtering in my back office list view. I feel that I have missed something but I can't figure out what?

 

Please help!

 

Thanks

  • Like 2
Link to comment
Share on other sites

sorting and filtering function should be defined within your module file to sort and filter data listing for your helper list

// for example, your sorting action URL is index.php?submitSorting&sortBy=example_field&sortWay=DESC
$sortBy = Tools::getValue('sortBy');
$sortWay = Tools::getValue('sortWay', 'ASC'); // default sortWay is Ascending

// for example, your filter action URL is index.php?submitFilter&filter_id=1
$filter_id = Tools::getValue('filter_id');

if($sortBy == 'example_field')
    $mydata = [YOUR QUERY] ORDER BY `example_field` '.$sortWay;
elseif($filter_id)
    $mydata = [YOUR QUERY] WHERE `id_whatever` = '.$filter_id;
else
    $mydata = [DEFAULT QUERY];
  • Like 1
Link to comment
Share on other sites

Thanks for the response. Do I have to put this code in construct method or in the renderList? thanks

 

 

 

sorting and filtering function should be defined within your module file to sort and filter data listing for your helper list

// for example, your sorting action URL is index.php?submitSorting&sortBy=example_field&sortWay=DESC
$sortBy = Tools::getValue('sortBy');
$sortWay = Tools::getValue('sortWay', 'ASC'); // default sortWay is Ascending

// for example, your filter action URL is index.php?submitFilter&filter_id=1
$filter_id = Tools::getValue('filter_id');

if($sortBy == 'example_field')
    $mydata = [YOUR QUERY] ORDER BY `example_field` '.$sortWay;
elseif($filter_id)
    $mydata = [YOUR QUERY] WHERE `id_whatever` = '.$filter_id;
else
    $mydata = [DEFAULT QUERY];
Link to comment
Share on other sites

That depend on your need, or depend on your programing structure.

But no matter where, the sorting and filtering variable should be able to be use for your data query processing

 

Take a look deep inside Prestashop AdminController.php classes file, the Prestashop standard methods for sorting and filtering is available there

 

 

Many thanks for your guidance. I was able to enable sorting though I would like to point out that the sorting variables that you mentioned do not exist in PS 1.5.6. I used $this->_defaultOrderBy and $this->_defaultOrderBy instead. I placed sorting code in my controllers construct method. I also had to set the 'orderby' variable to true, when declaring the $this->fields_list array, for the fields for which I had to display the sorting control.

Edited by sttaq (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...