0

I have created a module in which admin form I have shown product collection I want to paginate it. I have used a block and in which I have created Product collection.

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="form_key"/>
    <body>
        <referenceContainer name="content">
<block class="Namespace\MyModule\Block\Adminhtml\Myfolder\View\Create\Productlist" name="quote_product_list" template="Namespace_MyModule::quote/create/list.phtml"/>
            <block class="Namespace\Myfolder\Block\Adminhtml\Myfolder\View\Create\Product" name="namespace.edit.tab.products"/>
        </referenceContainer>
    </body>
</page>

Block File

class Productlist extends \Magento\Backend\Block\Widget\Grid\Extended

{

protected $collectionFactory;
protected $abstractProduct;

public function __construct(
    Context $context,
    AbstractProduct $abstractProduct,
    CollectionFactory $collectionFactory,
   \Magento\Backend\Helper\Data $backendHelper,
   array $data = []     
) {

    $this->collectionFactory = $collectionFactory;
    $this->abstractProduct   = $abstractProduct;
    parent::__construct($context, $backendHelper, $data);
}



public function getProducts() {

   //get values of current page. If not the param value then it will set to 1
    $page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;
//get values of current limit. If not the param value then it will set to 1
    $pageSize=($this->getRequest()->getParam('limit'))? $this->getRequest()->getParam('limit') : 10;

    $productCollection = $this->collectionFactory->create();
    $productCollection->addAttributeToSelect('*');
      $productCollection->setOrder('title','ASC');
    $productCollection->setPageSize($pageSize);
    $productCollection->setCurPage($page);
    return $productCollection;

}

public function getCurrency() {
    $abstractProductBlock = $this->abstractProduct;;
    return $abstractProductBlock;
}

}

Phtml File

<?php $products = $this->getProducts(); $abstractProductBlock = $this->getCurrency();?>

<section class="admin__page-section quote-view-account-information">
<div class="admin__data-grid-wrap admin__data-grid-wrap-static">
     <table class="data-grid" id="sales_order_create_search_grid_table">
         <thead>
            <tr>
                <th data-sort="entity_id" data-direction="asc" class="data-grid-th sortable descend col-id col-entity_id"><span>ID</span></th>
                <th data-sort="name" data-direction="asc" class="data-grid-th _sortable not-sort  col-name"><span>Product</span></th>
                <th data-sort="sku" data-direction="asc" class="data-grid-th _sortable not-sort  col-sku"><span>SKU</span></th>
                <th data-sort="price" data-direction="asc" class="data-grid-th _sortable not-sort  col-price"><span>Price</span></th>
                <th class="data-grid-th col-select no-link col-in_products"><span>Select</span></th>
                <th class="data-grid-th  no-link col-qty"><span>Quantity</span></th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($products as $value) : ?>
             <tr title="#" class="even _clickable">
                <td class=" col-id col-entity_id >"><?php echo $value->getEntityId() ?></td>                           
                <td class=" col-name"><a href="javascript:void(0)" class="action-configure " list_type="product_to_add" product_id="<?php echo $value['entity_id'] ?>">
                <?php if($value->getTypeId() != 'simple'): echo $value->getTypeId(); endif; ?></a><?php echo $value->getName(); ?></td>                           
                <td class=" col-sku"><?php echo $value->getSku();?></td>                            
                <td class=" price col-price col-price"><?php echo $abstractProductBlock->getProductPrice($value)?></td>
                <td class=" col-select col-in_products ">
                    <label class="data-grid-checkbox-cell-inner" for="id_2046">
                        <input type="checkbox" name="" value="2046" id="id_2046" class="checkbox admin__control-checkbox">
                        <label for="id_2046"></label>
                    </label> 
                </td>
                <td class="col-qty">
                 <input type="text" name="qty" value="" disabled="" class="input-text admin__control-text qty input-inactive"></td>
             </tr>
            <?php endforeach; ?>
        </tbody>   
    </table>
</div>

Screenshot of the same: http://prntscr.com/o8b2dc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.