Prestashop:如何在HelperList中过滤

时间:2017-03-14 11:55:15

标签: prestashop prestashop-1.6

您好我尝试过滤后台中的List。它显示了过滤器,它也在点击搜索后保存它,但没有发生任何事情。与分页相同。

    $schueler = $this->getAllSchuelerbyDiplom($id_diplom);
    $diplom_name = $this->getDiplomNamebyID($id_diplom);

    $fields_list = array(
        'id_schueler' => array(
            'title' => 'ID',
            'align' => 'center',
            'class' => 'fixed-width-xs',
            'search' => true),

        'customer_name' => array(
            'title' => $this->l('ID Customer')),

        'id_gruppe' => array(
            'title' => $this->l('ID Gruppe')),

        'name' => array(
            'title' => $this->l('Name'),
            'filter_key' => 'name'.$diplom_name),

        'vorname' => array(
            'title' => $this->l('Vorname')),

        'punkte' => array(
            'title' => $this->l('Punkte')),

        'bestanden' => array(
            'title' => $this->l('Bestanden'),
            'active' => 'toggle',
            'class' => 'fixed-width-xs',
            'type' => 'bool'),

        'date_added' => array(
            'title' => $this->l('Datum'),
            'class' => 'fixed-width-xs',
            'type' => 'date'),
    );

    $helper = new HelperList();
    $helper->table = 'no-idea-what-this-is-for';
    $helper->title = $diplom_name;
    $helper->shopLinkType = '';
    $helper->actions = array('view', 'edit', 'delete');
    $helper->listTotal = count($schueler);
    $helper->identifier = 'id_schueler';
    $helper->token = Tools::getAdminTokenLite('AdminModules');
    $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) . '&configure=' . $this->name .'&diplom_name=' . $diplom_name;

    return $helper->generateList($schueler, $fields_list);

我的代码出了什么问题?什么是$ helper->表?我在那里尝试了不同的东西,但没有任何帮助...

修改

public function getAllSchuelerbyDiplom($id_diplom) {
    $query = new DbQuery();
    $query->select('s.*, CONCAT(c.firstname,\' \',c.lastname) AS customer_name');
    $query->from($this->table_name.'_schueler', 's');
    $query->leftJoin('customer', 'c', 's.id_customer = c.id_customer');
    $query->where('s.id_diplom = ' . (int)$id_diplom);
    return Db::getInstance()->ExecuteS($query);
}

1 个答案:

答案 0 :(得分:0)

问题是HelperList对象本身只会显示正在使用的过滤器,但不会为您过滤实际的数据列表。

$this->getAllSchuelerbyDiplom($id_diplom);方法中,我假设您执行SQL查询以检索所有行,但是您需要修改它以考虑任何过滤器,分页(页码和每页行数)或排序。您必须检查GET / POST值或cookie中设置的值以检测它们。

$helper->table设置表名,列表操作前面加上该名称,因此它们与您在页面上可能拥有的任何其他列表不同。

修改

设置分页和页面的示例

public function getPage()
{
    // $tableName must be equal to what you set in $helper->table

    // Check if page number was selected and return it
    if (Tools::getIsset('submitFilter'.$tableName)) {
        return (int)Tools::getValue('submitFilter'.$tableName);
    }
    else {
        // Check if last selected page is stored in cookie and return it
        if (isset($this->context->cookie->{'submitFilter'.$tableName})) {
            return (int)$this->context->cookie->{'submitFilter'.$tableName};
        }
        else {
            // Page was not set so we return 1
            return 1;
        }
    }
}

public function getRowsPerPage()
{
    // $tableName must be equal to what you set in $helper->table

    // Check if number of rows was selected and return it
   if (Tools::getIsset($tableName. '_pagination')) {
        return (int)Tools::getValue($tableName. '_pagination');
    }
    else {
        // Check if number of rows is stored in cookie and return it
        if (isset($this->context->cookie->{$tableName. '_pagination'})) {
            return (int)$this->context->cookie->{$tableName. '_pagination'};
        }
        else {
            // Return 20 rows per page as default
            return 20;
        }
    }
}

public function getAllSchuelerbyDiplom($id_diplom) {
    $query = new DbQuery();
    $query->select('s.*, CONCAT(c.firstname,\' \',c.lastname) AS customer_name');
    $query->from($this->table_name.'_schueler', 's');
    $query->leftJoin('customer', 'c', 's.id_customer = c.id_customer');
    $query->where('s.id_diplom = ' . (int)$id_diplom);

    // Limit the result based on page number and rows per page 
    $query->limit($this->getRowsPerPage(), ($this->getPage() - 1) * $this->getRowsPerPage());

    return Db::getInstance()->ExecuteS($query);
}

您可以在生成列表后放置p(Tools::getAllValues()); d($this->context->cookie->getAll();,然后在列表中设置过滤器,它会显示创建过滤器和排序所需的所有变量。