Magento 2 分页
Magento 2 Pagination
我找不到在 magento 2 中对我的实体进行分页的方法。我有以下代码:
public function getPosts()
{
if (!$this->hasData('posts')) {
$posts = $this->_postCollectionFactory->create()->addOrder(
PostInterface::CREATED,
PostCollection::SORT_ORDER_DESC
);
$this->setData('posts', $posts);
}
return $this->getData('posts');
}
在 magento 1.x 我有一个自定义块,它有 "page/html_pager" 类型,但我在 magento 2 文档中找不到对我的实体进行分页的方法......我从我的块控制器中获取它们(上面的代码)。
参考以下link,在该示例中添加了分页。
http://www.mage-world.com/blog/create-the-news-list-page-via-frontend-in-magento-2.html
您应该查看文件 toolbar.phtml。在这里您可以找到一个名为
的 class 方法
$block->getPagerHtml()
此方法调用实体的分页。例如在类别页面中,产品将是实体。但我猜你可以随时更改此默认方法。如果您继续使用此方法,您会发现块 class 位于
\Magento\Catalog\Block\Product\ProductList\Toolbar.php
您将找到函数 getPagerHtml()
public function getPagerHtml()
{
$pagerBlock = $this->getChildBlock('product_list_toolbar_pager');
if ($pagerBlock instanceof \Magento\Framework\DataObject) {
/* @var $pagerBlock \Magento\Theme\Block\Html\Pager */
$pagerBlock->setAvailableLimit($this->getAvailableLimit());
$pagerBlock->setUseContainer(
false
)->setShowPerPage(
false
)->setShowAmounts(
false
)->setFrameLength(
$this->_scopeConfig->getValue(
'design/pagination/pagination_frame',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
)
)->setJump(
$this->_scopeConfig->getValue(
'design/pagination/pagination_frame_skip',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
)
)->setLimit(
$this->getLimit()
)->setCollection(
$this->getCollection()
);
return $pagerBlock->toHtml();
}
return '';
}
结论:
在 app\code\Your\Custom\Block\Toolbar.php 中创建一个扩展到的自定义模块
\Magento\Catalog\Block\Product\ProductList\Toolbar
namespace Your\Custom\Block\;
Class Toolbar extends \Magento\Catalog\Block\Product\ProductList\Toolbar
{
public function getPagerHtml()
{
..... Your Code for you post entities .....
}
}
您的布局 xml 添加工具栏块并添加块 class -> your_custom_index.xml
<block class="Your\Custom\Block\Toolbar" name="product_list_toolbar" template="Your_Custom::product/list/toolbar.phtml">
模板 -> view\frontend\templates\product\list\toolbar.phtml:
<?php
echo $block->getPagerHtml();
Now this is an example on how you would extend this functionality.
Read more about creating and extending core modules because you'll
need a few more files to extend custom module that I'm not posting
here because they get out of the main subject.
在 Magento 2 中向自定义块添加分页有以下步骤
步骤 1
public function getPosts()
{
$page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;`enter code here`
$pageSize=($this->getRequest()->getParam('limit'))? $this->getRequest()->getParam('limit') : 5;
if (!$this->hasData('posts')) {
$posts = $this->_postCollectionFactory->create();
$posts->addOrder('field name','ASC');
}
return $posts;
}
我找不到在 magento 2 中对我的实体进行分页的方法。我有以下代码:
public function getPosts()
{
if (!$this->hasData('posts')) {
$posts = $this->_postCollectionFactory->create()->addOrder(
PostInterface::CREATED,
PostCollection::SORT_ORDER_DESC
);
$this->setData('posts', $posts);
}
return $this->getData('posts');
}
在 magento 1.x 我有一个自定义块,它有 "page/html_pager" 类型,但我在 magento 2 文档中找不到对我的实体进行分页的方法......我从我的块控制器中获取它们(上面的代码)。
参考以下link,在该示例中添加了分页。
http://www.mage-world.com/blog/create-the-news-list-page-via-frontend-in-magento-2.html
您应该查看文件 toolbar.phtml。在这里您可以找到一个名为
的 class 方法 $block->getPagerHtml()
此方法调用实体的分页。例如在类别页面中,产品将是实体。但我猜你可以随时更改此默认方法。如果您继续使用此方法,您会发现块 class 位于
\Magento\Catalog\Block\Product\ProductList\Toolbar.php
您将找到函数 getPagerHtml()
public function getPagerHtml()
{
$pagerBlock = $this->getChildBlock('product_list_toolbar_pager');
if ($pagerBlock instanceof \Magento\Framework\DataObject) {
/* @var $pagerBlock \Magento\Theme\Block\Html\Pager */
$pagerBlock->setAvailableLimit($this->getAvailableLimit());
$pagerBlock->setUseContainer(
false
)->setShowPerPage(
false
)->setShowAmounts(
false
)->setFrameLength(
$this->_scopeConfig->getValue(
'design/pagination/pagination_frame',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
)
)->setJump(
$this->_scopeConfig->getValue(
'design/pagination/pagination_frame_skip',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
)
)->setLimit(
$this->getLimit()
)->setCollection(
$this->getCollection()
);
return $pagerBlock->toHtml();
}
return '';
}
结论:
在 app\code\Your\Custom\Block\Toolbar.php 中创建一个扩展到的自定义模块 \Magento\Catalog\Block\Product\ProductList\Toolbar
namespace Your\Custom\Block\;
Class Toolbar extends \Magento\Catalog\Block\Product\ProductList\Toolbar
{
public function getPagerHtml()
{
..... Your Code for you post entities .....
}
}
您的布局 xml 添加工具栏块并添加块 class -> your_custom_index.xml
<block class="Your\Custom\Block\Toolbar" name="product_list_toolbar" template="Your_Custom::product/list/toolbar.phtml">
模板 -> view\frontend\templates\product\list\toolbar.phtml:
<?php
echo $block->getPagerHtml();
Now this is an example on how you would extend this functionality. Read more about creating and extending core modules because you'll need a few more files to extend custom module that I'm not posting here because they get out of the main subject.
在 Magento 2 中向自定义块添加分页有以下步骤
步骤 1
public function getPosts()
{
$page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;`enter code here`
$pageSize=($this->getRequest()->getParam('limit'))? $this->getRequest()->getParam('limit') : 5;
if (!$this->hasData('posts')) {
$posts = $this->_postCollectionFactory->create();
$posts->addOrder('field name','ASC');
}
return $posts;
}