1

I need to display some records in admin data grid. I am displaying this data grid using xml file. I need to fetch only the records with status = 'Active'. How can I implement this condition in model?

This is my table

enter image description here

Collection.php

<?php

 namespace Bridge\Batchcode\Model\ResourceModel\Items;

 class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
 {

    protected function _construct()
    {
        $this->_init('Bridge\Batchcode\Model\Items', 'Bridge\Batchcode\Model\ResourceModel\Items');
    }    
}

bridge_batchcode_items_index.xml

<?xml version="1.0"?>


 <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
 <body>
 <referenceContainer name="content">
    <block class="Bridge\Batchcode\Block\Adminhtml\Items" name="adminhtml.block.promo.quote.grid.container">
        <block class="Magento\Backend\Block\Widget\Grid" name="adminhtml.block.promo.quote.grid" as="grid">
            <arguments>
                <argument name="id" xsi:type="string">bridge_batchcode_items_grid</argument>
                <argument name="dataSource" xsi:type="object">Bridge\Batchcode\Model\ResourceModel\Items\Collection</argument>
                <argument name="default_sort" xsi:type="string">sort_order</argument>
                <argument name="default_dir" xsi:type="string">ASC</argument>
                <argument name="save_parameters_in_session" xsi:type="string">1</argument>
            </arguments>
            <block class="Magento\Backend\Block\Widget\Grid\Export" name="items.grid.export" as="grid.export">
                <arguments>
                    <argument name="exportTypes" xsi:type="array">
                        <item name="csv" xsi:type="array">
                            <item name="urlPath" xsi:type="string">bridge_batchcode/*/export</item>
                            <item name="label" xsi:type="string" translate="true">CSV</item>
                        </item>
                    </argument>
                </arguments>
            </block>
            <block class="Magento\Backend\Block\Widget\Grid\ColumnSet" as="grid.columnSet" name="adminhtml.promo.quote.grid.columnSet">
                <arguments>
                    <argument name="rowUrl" xsi:type="array">
                        <item name="path" xsi:type="string">bridge_batchcode/*/edit</item>
                        <item name="extraParamsTemplate" xsi:type="array">
                            <item name="id" xsi:type="string">getId</item>
                        </item>
                    </argument>
                </arguments>
                <block class="Magento\Backend\Block\Widget\Grid\Column" as="id">
                    <arguments>
                        <argument name="header" xsi:type="string" translate="true">ID</argument>
                        <argument name="index" xsi:type="string">id</argument>
                        <argument name="column_css_class" xsi:type="string">col-id</argument>
                        <argument name="header_css_class" xsi:type="string">col-id</argument>
                    </arguments>
                </block> 

            </block>
        </block>
    </block>
</referenceContainer>
</body>

Bridge/Batchcode/Model/Items.php

<?php

namespace Bridge\Batchcode\Model;

class Items extends \Magento\Framework\Model\AbstractModel
{

  protected function _construct()
  {
    parent::_construct();
    $this->_init('Bridge\Batchcode\Model\ResourceModel\Items');
  }
}

Bridge\Batchcode\Model\ResourceModel\Items.php

<?php

namespace Bridge\Batchcode\Model\ResourceModel;

class Items extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{

  protected function _construct()
  {
    $this->_init('batchcode_entity', 'id');
  }
}

Is there any method to add condition in select method. I am showing only ID in grid now.

7

2 Answers 2

2

You can use _initSelect() to filter collection data.

Add the below function in your Collection.php

protected function _initSelect()
    {
        parent::_initSelect();
        $this->addFieldToFilter('main_table.status', ['eq' => 'active']);
        return $this;
    }

Hope this helps!

2
  • Can I add this filtering in xml file? collection.php file is a common file, editing this file will affect some other pages Mar 19, 2020 at 5:57
  • @GeethuJaison You can create grid collection for it. Mar 19, 2020 at 6:00
0

You can use in view in ui_component like as shown below under item name filter_url_params

<item name="filter_url_params" xsi:type="array">
      <item name="main_table.status" xsi:type="string">1</item>
</item>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.