Prestashop:如何在HelperList中过滤

Prestashop: How to filter in HelperList

您好,我尝试在后台过滤列表。它显示了过滤器,它在点击搜索后也保存了它,但没有任何反应。与分页相同。

    $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->table 有什么用?我在那里尝试了不同的东西,但没有任何帮助...

编辑

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);
}

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

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

$helper->table 设置 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();,然后在您的列表中设置一个过滤器,它会向您显示创建过滤器和排序所需的所有变量。