Magento 2:フロントエンドでカスタムグリッドでポケットベルを追加する

magento.stackexchange https://magento.stackexchange.com/questions/115116

  •  29-09-2020
  •  | 
  •  

質問

私は顧客アカウントに1つのグリッドを追加し、そのグリッドでポケットベルを追加したいとき、その時、その時点でエラーが発生しました。

SQLSTATE [42S22]:1054不明な列 'main_table.attribute_id' '' field list 'で、queryは、custommodule_testとしてcount(distint main_table.attribute_id)を選択しました。ここで(customer_id=' 2 '){"is_exception":false} []

実際にはattribute_idファイルがありません。私の表には、あなたが見ているとおりのファイル名のフィルタリングです。

見てください:

test.phtml

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

ブロック/ 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');
}
.

注意してください:私が直接

 $this->getCollection() ;
からPHTMLファイルを呼び出すと、正しいコレクションがありました。

役に立ちましたか?

解決

要件を満たすために、多くのアプローチを経験し、Magento2カスタムコレクションでのページ区切りの最善の解決策を見つけました。ここで我々は最善の方法を説明するつもりです手順に従ってください。

注:Magento2に基本モジュールを作成したと仮定してください。ここでiPRAGMATECHは私たちのパッケージであり、IPREWARDは私たちのモジュールです。それに応じてクラス名を変更してください。

ステップ1:MyRewadという名前のコントローラ、アクションインデックス(myreward / index.php)を作成し、メソッドを実行するために次のコードを追加します。

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

ステップ2:ブロックを作成する(テーブルのモデルをすでに作成していると仮定して、カスタムテーブルがあり、モデルを報酬として作成しました)名前REWARD.PHPと次のコードを追加しました。このコードでは、カスタムコレクションにポケットベルを追加しました。

<?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;
}
.

} ステップ3:レイアウトファイル/コード/ iPRAGMATECH / IPREWARD / VIEW / FROONTEND / 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>
.

ステップ4:PHTMLファイルをAPP / CODE / IPRAGMATECH / IPREWARD / VIEW / FROONTEND / TEMPLATES / MYREWARD / REWARD.PHTMLとして追加し、次のコードを追加

画像の説明が入力されています

と出力はこの

のように見えます

画像の説明が入力されています

Page Limitが表示されていないCSSの問題に直面しているので、同じ問題がある場合は、次のCSSを使用してください。

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

これがあなたをたくさん助けることを願っています。 Magentoのカスタマイズに関する問題があるかどうかお知らせください。

ライセンス: CC-BY-SA帰属
所属していません magento.stackexchange
scroll top