Pregunta

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?

¿Fue útil?

Solución

In prestashop 1.6 (I think it has to be the same in PS 1.5) :

The easiest way I found to get the sorting and filtering working in custom ModuleAdminController is to init the parent of postProcess() if it is overrided :

public function postProcess() {
    parent::postProcess(); 
    ...
}

And the sorting/filtering works just like it has to without any custom code.

Enjoy. = )

Otros consejos

Yes indeed I did find a solution but didn't have time to post about it. They key here is the following two lines:

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

So in addition to all of the things I have mentioned in the example, we also need to fetch the values for these fields from the query string. In my case the query string parameter looks like [tablename]Orderby= i.e. in above example it will be custom_tableOrderby=

To fetch the value from query string do the following in the construct function:

$sortBy = Tools::getValue('custom_tableOrderby');
$sortWay = Tools::getValue('custom_tableOrderway', 'ASC'); // default sortWay is Ascending
$this->_defaultOrderWay = $sortWay;

if ($sortBy == 'field_name_in_your_view') {
    $this->_defaultOrderBy = 'field_name_in_table';
} else if ($sortBy == 'another_field_name_in_your_view') {
    $this->_defaultOrderBy = 'another_field_name_in_table';

I hope it works for you as I implemented this in Prestashop 1.5 a very long time ago.

You must include parent::postProcess(); in postProcess function

public function postProcess()
{
    parent::postProcess();
}

Do you try setting $this->list_simple_header=false; in the __construct function?

Sort/filter/pagination is did by the property HelperList->simple_header.

With a controller, all works fine. In a module, we have only simple headers if we call a HelperList (except if you add missing functions to do full headers). So, sort/filter/pagination in a list are only for controller. {Prestashop 1.5}

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top