浏览文章

文章信息

Magento2 前台列表添加分页 | Magento2 add pagenation in frontend table list 10355

block关键代码:

    public function getPagerHtml(): string
    {
        $pagerBlock = $this->getChildBlock('withdrawal_pager');
        if ($pagerBlock instanceof \Magento\Framework\DataObject) {
            $pagerBlock->setUseContainer(
                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 '';
    }
    public function getCollection(): \Aiweline\SocialInfluencerMarketing\Model\ResourceModel\Withdraw\Collection
    {
        if (!$this->collection) {
            $this->collection = $this->withdrawCollectionFactory->create();
            $this->collection->join(
                ['influencer' => 'aiweline_socialinfluencermarketing_socialinfluencer'],
                'influencer.' . SocialInfluencerInterface::SOCIALINFLUENCER_ID . '=main_table.' . SocialInfluencerInterface::SOCIALINFLUENCER_ID
            );
            $this->collection->addFieldToFilter('influencer.' . SocialInfluencerInterface::CUSTOMER_ID, $this->getSocialInfluencerCustomerID());
            $this->collection->setOrder('main_table.' . WithdrawInterface::WITHDRAW_ID, 'DESC');
            if ($this->getLimit()) {
                $curPage = (int)$this->getRequest()->getParam('p', 1);
                $this->collection->setCurPage($curPage);
                $this->collection->setPageSize($this->getLimit());
            }
        }
        return $this->collection;
    }
    private function getLimit(): int
    {
        return (int)$this->getRequest()->getParam('limit', 10);
    }

phtml模板代码:

<?php
/**@var Aiweline\SocialInfluencerMarketing\Block\Withdraw\Index $block */
?>
<style>
    .withdraw-container {
    }
</style>
<h3><?= __('Withdraw History') ?></h3>
<div class="withdraw-container">
    <table class="table">
        <caption><?= __('Withdraw History') ?></caption>
        <thead>
        <tr>
            <th><?= __('Amount') ?></th>
            <th><?= __('Additional') ?></th>
            <th><?= __('Is Complete') ?></th>
            <th><?= __('Created At') ?></th>
        </tr>
        </thead>
        <tbody>
        <?php
        $collection = $block->getCollection();
        if ($collection->count()):
            ?>
            <?php
            /**@var \Aiweline\SocialInfluencerMarketing\Model\Withdraw $item */
            foreach ($collection as $item) {
                $itemData = $item->getDataModel();
                ?>
                <tr>
                    <td><?= $itemData->getAmount() ?></td>
                    <td><?= $itemData->getAddtinal() ?></td>
                    <td>
                        <?php
                        if ($itemData->getIsComplete()) {
                            echo '<b style="color: green">' . __('Complete') . '</b>';
                        } else {
                            echo '<b style="color: red">' . __('Incomplete') . '</b>';
                        }
                        ?>
                    </td>
                    <td><?= $itemData->getCreatedAt() ?></td>
                </tr>
                <?php
            }
        else:
            ?>
            <tr>
                <td colspan="3">
                    <div class="message info empty">
                        <span><?= $block->escapeHtml(__('You have no withdraw history.')); ?></span>
                    </div>
                </td>
            </tr>
        <?php
        endif;
        ?>
        </tbody>
        <tfoot>
        <tr>
            <td colspan="3">
                <?php if ($pager = $block->getPagerHtml()): ?>
                    <div class="toolbar">
                        <div class="hunter-pager">
                            <?= /* @noEscape */
                            $pager ?>
                        </div>
                    </div>
                <?php endif; ?>
            </td>
        </tr>
        </tfoot>
    </table>
    <div>
        <a href="<?= $block->getAddWithdrawUrl(); ?>"><?= __('Apply New Withdraw') ?></a>
    </div>
</div>

效果:




原创