How to use addFieldToFilter on ui grid
-
13-04-2021 - |
문제
From them image above you can see the first record does not have name.
I want all the record which have name should be displayed and which doesn't have should not be listed in grid.
I know how to use addFieldToFilter in php file but don't know how to use in UI Grid.
$collection =$this->MainOr->create()
->addFieldToFilter("parent_id", array("neq" => 0));
해결책
Please add filter in collection.php with
protected function _initSelect()
{
parent::_initSelect();
$this>addFieldToFilter("parent_id", array("neq" => 0));
return $this;
}
and extend collection.php in your grid/collection.php file
Try and Let me know if you want more help!!!
다른 팁
To add value to grid in ui component. Sample here. First you should see in view/adminhtml/ui_component/xxxx_listing.xml
<dataSource name="xxxx_grid_data_source">
<argument name="dataProvider" xsi:type="configurableObject">
<argument name="class" xsi:type="string">CompanyName\ModuleName\Ui\DataProvider\XXXXX\VendorDataProvider</argument>
<argument name="name" xsi:type="string">vendor_grid_data_source</argument>
<argument name="primaryFieldName" xsi:type="string">entity_id</argument>
<argument name="requestFieldName" xsi:type="string">id</argument>
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="update_url" path="mui/index/render" xsi:type="url"/>
</item>
</argument>
</argument>
<argument name="data" xsi:type="array">
<item name="js_config" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
</item>
</argument>
</dataSource>
That defind dataProvider CompanyName\ModuleName\Ui\DataProvider\XXXXX\VendorDataProvider
and class dataProvider will be like this.
<?php
namespace CompanyName\ModuleName\Ui\DataProvider\Xxxxx;
use Magento\Customer\Ui\Component\Listing\AttributeRepository;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\Search\SearchCriteriaBuilder;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\View\Element\UiComponent\DataProvider\Reporting;
use Magento\Framework\Api\Search\SearchResultInterface;
use CompanyName\ModuleName\Model\ResourceModel\XXXXX\CollectionFactory;
class VendorDataProvider extends
\Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider
{
/**
* @var AttributeRepository
*/
private $attributeRepository;
/**
* @param string $name
* @param string $primaryFieldName
* @param string $requestFieldName
* @param Reporting $reporting
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param RequestInterface $request
* @param FilterBuilder $filterBuilder
* @param AttributeRepository $attributeRepository
* @param array $meta
* @param array $data
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
Reporting $reporting,
SearchCriteriaBuilder $searchCriteriaBuilder,
RequestInterface $request,
FilterBuilder $filterBuilder,
AttributeRepository $attributeRepository,
array $meta = [],
array $data = []
) {
$this->attributeRepository = $attributeRepository;
parent::__construct(
$name,
$primaryFieldName,
$requestFieldName,
$reporting,
$searchCriteriaBuilder,
$request,
$filterBuilder,
$meta,
$data
);
}
/**
* @param SearchResultInterface $searchResult
* @return array
*/
protected function searchResultToOutput(SearchResultInterface $searchResult)
{
$arrItems = [];
$arrItems['totalRecords'] = $searchResult->getTotalCount();
$arrItems['items'] = [];
foreach ($searchResult->getItems() as $item) {
$arrItems['items'][] = $item->getData();
}
return $arrItems;
}
/**
* {@inheritdoc}
*/
public function getData()
{
$collection = $this->getSearchResult();
$collection
->addAttributeToSelect([
'logo',
'email',
'shop_name',
'status'
]);
$data = $this->searchResultToOutput($collection);
return $data;
}}
you can rewrite function getData to addAttributeToSelect for collection. Then it will show result to grid. If it work please vote for me. :)
@Rutvee Sojitra, I applied the changes but nothing reflects, also extended the collection.php in the grid collection. Can you help me..