Domanda

Ho aggiunto una griglia nell'account del cliente e ottenendo la raccolta anche. Ma quando voglio aggiungere il cercapersone in quella griglia, quella volta che sto ricevendo un errore come:

.

sqlstate [42s22]: colonna non trovata: 1054 colonna sconosciuta 'main_table.attribute_id' in 'field list', query è stato: Select Count (distinto main_table.attribute_id) da custommodule_test come main_table Where (Customer_id= '2') {"IS_Exception": false} []

In realtà non ci sono attribute_id archiviato è lì nella mia tabella né sto filtrando per quel nome archiviato come si vede.

Si prega di dare un'occhiata:

Test.phtml

<?php if ($block->getPagerHtml()): ?>
    <div class="order-products-toolbar toolbar bottom"><?php echo $block->getPagerHtml(); ?></div>
<?php endif; ?>
.

Block / Test.php

protected function _prepareLayout()
{
    parent::_prepareLayout();
    if ($this->getCollection()) {
        $pager = $this->getLayout()->createBlock(
            'Magento\Theme\Block\Html\Pager',
            'custom.collection.test'
        )->setCollection(
            $this->getCollection()
        );
        $this->setChild('pager', $pager);
        $this->getCollection()->load();
    }
    return $this;
}
public function getPagerHtml()
{
    return $this->getChildHtml('pager');
}

public function getCollection()
{
    if (!$this->getData('collection')) {
        $this->setCollection(
            $this->_objectMangaer->get('Namespace\Modulename\Model\Test')->getCollection()->addFieldToFilter('customer_id',2)
        );
    }
    return $this->getData('collection');
}
.

Nota: se chiamare direttamente

 $this->getCollection() ;
dal file PHTML, quindi ho ricevuto una collezione corretta.

È stato utile?

Soluzione

Per soddisfare il requisito, abbiamo attraversato molti approcci e abbiamo trovato la soluzione migliore per il Pagination sulla collezione personalizzata Magento2. Qui spiegheremo l'approccio migliore per favore segui i passaggi.

Nota: supponendo di aver creato un modulo di base in Magento2. Qui IPragmatech è il nostro pacco e Ipreward è il nostro modulo. Per favore, cambia il nome della tua classe di conseguenza.

Step 1: Creare un controller denominato MyREWAD, INDICE AZIONE (MYREWARD / INDEX.PHP) e aggiungi il seguente codice da eseguire il metodo

<?php
  namespace Ipragmatech\Ipreward\Controller\Myreward;
  class Index extends \Magento\Framework\App\Action\Action
     {
        public function execute()
           {
               $this->_view->loadLayout();
               $this->_view->renderLayout();
           }
    }
.

Step 2: Creare un blocco (supponendo che tu abbia già creato un modello per la tua tabella. Qui abbiamo un tavolo personalizzato e abbiamo creato il modello come ricompensa) Nome ricompensa e aggiungere il seguente codice. In questo codice, abbiamo aggiunto il cercapersone nella nostra collezione personalizzata.

<?php
namespace Ipragmatech\Ipreward\Block\Myreward;

use Ipragmatech\Ipreward\Block\BaseBlock;

class Reward extends BaseBlock
  {
/**
 * @var \Ipragmatech\Ipreward\Model\Reward
 */
protected $_rewardCollection;

/**
 * Reward constructor.
 * @param \Magento\Framework\App\Action\Context $context
 * @param \Ipragmatech\Ipreward\Model\Reward $rewardCollection
 */
public function __construct(
    \Ipragmatech\Ipreward\Block\Context $context,
    \Ipragmatech\Ipreward\Model\Reward $rewardCollection,
){
    $this->_rewardCollection = $rewardCollection;
    parent::__construct($context);
}

protected function _prepareLayout()
{

    parent::_prepareLayout();
    $this->pageConfig->getTitle()->set(__('My Reward History'));

    if ($this->getRewardHistory()) {
        $pager = $this->getLayout()->createBlock(
            'Magento\Theme\Block\Html\Pager',
            'reward.history.pager'
        )->setAvailableLimit(array(5=>5,10=>10,15=>15,20=>20))
            ->setShowPerPage(true)->setCollection(
            $this->getRewardHistory()
        );
        $this->setChild('pager', $pager);
        $this->getRewardHistory()->load();
    }
    return $this;
}

public function getPagerHtml()
{
    return $this->getChildHtml('pager');
}
/**
 * function to get rewards point transaction of customer
 *
 * @return reward transaction collection
 */
Public function getRewardHistory()
{
    //get values of current page
    $page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;
    //get values of current limit
    $pageSize=($this->getRequest()->getParam('limit'))? $this->getRequest
    ()->getParam('limit') : 5;


    $collection = $this->_rewardCollection->getCollection();
    $collection->setPageSize($pageSize);
    $collection->setCurPage($page);
    $logger->info("Here reward collection: ".$collection->getSelect());
    $logger->info("Here reward collection: Page:".$page." Page size :"
        .$pageSize);
    return $collection;
}
.

} Passaggio 3: aggiunto / modifica il seguente codice sull'applicazione del file di layout / codice / ipragmatech / ipreward / Visualizza / frontend / layout / ipreward_myreward_index.xml

  <?xml version="1.0"?>
  <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"               layout="2columns-left" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <update handle="customer_account"/>
      <body>
        <referenceContainer name="content">          
        <block class="Ipragmatech\Ipreward\Block\Myreward\Reward" name="myreward_reward" template="myreward/reward.phtml">
        </block>
       </referenceContainer>      
   </body>
 </page>
.

Step 4: Aggiungi il file PHTML come App / Codice / IPragmatech / Ipreward / View / Frontend / Templates / MyReward / Reward.phtml e aggiungi il seguente codice

 Inserisci la descrizione dell'immagine qui

E l'output sarà simile a questo

 Inserisci la descrizione dell'immagine qui

A volte abbiamo affrontato alcuni problemi CSS in cui il limite della pagina non viene visualizzato, quindi utilizzare il seguente CSS se hai lo stesso problema.

 .custom-pager .limiter{
      display: block !important;
  }
.

Spero che questo ti aiuterà molto. Fateci sapere se avete qualche problema sulla personalizzazione Magento.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top