RenderList – Create two renderList on same controller in prestashop 1.7

Many time we need to show database information on our controller using renderList. But there is only one render list possibly to display on same controller.

In post,  i will make you understand how can we write two or more render list with different tables and class name on same controller without putting any condition. As we know render list contain className and tableName to display information. So there is only one className and one tableName then how we can write two render list on same controller using two different tables.

Lets start with here

View of normal render list, we have used field list in constructor.

public function __construct()
{
    $this->bootstrap = true;
    $this->table = 'yourTableName';
    $this->className   = 'yourClassName';
    $this->fields_list = array(
        'id' => array(
            'title' => $this->l('Id'),
            'align' => 'center'
            )
        );
    $this->identifier = 'id';
    parent::__construct();
}


The above renderList is simple as we have seen in many controllers. We can use same code in our renderList function. This code will only create one render List. We need to display another List with different table then what ?.

If we use array to renderList to display multiple then ?? Then it will only display last array of render List, means it will overwrite all the array with last one.

Lets See The Code To Display Two RenderList With Different Table Names
public function __construct()
{
    $this->bootstrap = true;
    $this->table = 'yourTableName';
    $this->className = 'yourClassName';
    $this->identifier = 'id';
    parent::__construct();
}

public function renderList()
{
     $this->bulk_actions = array(
          'delete' => array(
              'text' => $this->l('Delete selected'),
              'confirm' => $this->l('Delete selected items?'),
              'icon' => 'icon-trash',
          )
      );
      $this->initFirstRenderList();
      $lists = parent::renderList();
     
      $this->initSecondRenderList();
      $lists .= parent::renderList();

      return $lists;
}
We have used __constructor to define our className and tableName that will be used in our renderList to display information. By default it takes className and tableName from the constructor.

Now, we are using renderList function to call initFirstRenderList() and initSecondRenderList(), means we are here calling two method. Methods name can be anything here.

After calling initFirstRenderList(), we just called the parent method of renderList and store it in a variable to use in second List.

Lets see the code of initFirstRenderList

protected function initFirstRenderList()
{
    $this->toolbar_title = $this->l('First Render List Title');

    $this->fields_list = array(
        'first_field' => array(
            'title' => $this->l('First Title'),
            'align' => 'center',
        ),
        'second_field' => array(
            'title' => $this->l('Second Title'),
            'align' => 'center',
          )
    );
}
Now, Lets see the code of secondRenderList

protected function initSecondRenderList()
{
    unset($this->fields_list, $this->_select, $this->_join);
    $this->table = 'Second table for second render list';
    $this->className = 'Second class name for second render list';
    $this->identifier = 'id';
    $this->toolbar_title = $this->l('Second RenderList Title');
    $this->fields_list = array(
         'first_field' => array(
         'title' => $this->l('First title'),
             'align' => 'center',
             'class' => 'fixed-width-xs',
         ),
         'second_field' => array(
             'title' => $this->l('Second title'),
             'align' => 'center'
          )
      );
 }
See, the difference between these two function, in second function initSecondRenderList() in the starting we just unset all the variables like

$this->fields_list, $this->_select, $this->_join
in order to create second renderList with different class and table. We just unset all the previous variables. Now we can use different className different tabeleName to display different information in second render List.

See the outcome

Comments

Popular Posts