Magento Asked by DRAJI on December 14, 2021
I have created one custom module with title & status field attribute in admin grid.
Grid collections in module_test_grid_block.xml
<arguments>
<argument name="id" xsi:type="string">testGrid</argument>
<argument name="dataSource" xsi:type="object">NamespaceModuleModelResourceModeltestCollection</argument>
<argument name="default_sort" xsi:type="string">id</argument>
<argument name="default_dir" xsi:type="string">desc</argument>
<argument name="grid_url" xsi:type="url" path="*/*/grid"><param name="_current">1</param></argument>
</arguments>
I want to show the data which are enabled. Have any option to add default filter in admin grid collections?
EDIT
<block class="NamespaceModuleBlockAdminhtmlTestGrid" name="namespace_module_test.grid" as="grid">
<arguments>
<argument name="id" xsi:type="string">testGrid</argument>
<argument name="dataSource" xsi:type="object">NamespaceModuleModelResourceModeltestCollection</argument>
<argument name="default_sort" xsi:type="string">id</argument>
<argument name="default_dir" xsi:type="string">desc</argument>
<argument name="grid_url" xsi:type="url" path="*/*/grid"><param name="_current">1</param></argument>
<argument name="default_filter" xsi:type="array">
<item name="status" xsi:type="string">1</item>
</argument>
</arguments>
.
.
</block>
In Block – Grid.php
namespace NamespaceModuleBlockAdminhtmlTest;
use MagentoBackendBlockWidgetGrid as WidgetGrid;
class Grid extends WidgetGrid
{
public function _construct()
{
parent::_construct();
if ($this->hasData('default_filter')) {
// print_r($this->getData('default_filter'));die;
$this->setDefaultFilter($this->getData('default_filter'));
}
}
}
I have followed this link to create admin grid in magento 2
http://www.mage-world.com/blog/grid-and-form-in-magento-2-admin-panel-part-1.html
The above solutions didn't work for me.
I solved the problem by extending Grid and looping through the items to filter, I even defined my own XML variable to allow me to (OR/AND) the filters, for more information see below:
XML:
<block class="MyModuleMyExtensionBlockAdminhtmlListsGrid" name="ordersprocess_grid.grid" as="grid">
<arguments>
<argument name="id" xsi:type="string">order_id</argument>
<argument name="dataSource" xsi:type="object">MyModuleMyExtensionModelOrderResourceModelProcessingCollection</argument>
<argument name="default_filter" xsi:type="array">
<item name="complete" xsi:type="string">0</item>
<item name="cancelled" xsi:type="string">0</item>
</argument>
<argument name="my_filter_type" xsi:type="string">and</argument>
<argument name="default_sort" xsi:type="string">something_date</argument>
<argument name="default_dir" xsi:type="string">DESC</argument>
<argument name="save_parameters_in_session" xsi:type="boolean">1</argument>
</arguments>
PHP Class:
namespace MyModuleMyExtensionBlockAdminhtmlLists; class Grid extends MagentoBackendBlockWidgetGrid { public function _construct() { parent::_construct(); $FilterType = $this->getData('my_filter_type'); if ($this->hasData('default_filter')) { foreach ($this->getDefaultFilter() as $Field => $Value) { $this->getCollection()->addFilter ($Field, $Value, $FilterType); } } } }
With the above code, you don't need to rely on "default_filter", if you follow my way, you could easily do the same as I did to "my_filter_type" to create your own something like "my_filter_field" which prevent the code from conflicting with God-Knows-What Magento does with the "default_filter" variable.
I hope this helps,
H
Answered by Heider Sati on December 14, 2021
After trying many codes which doesn't work, finally find a way doesn't need coding - make use of saving custom views function which could save filter and column settings.
After setting filter and column settings, click on the Default View button with the eye icon, then click Save View As… to enter a name for your view. Then you could click the custom view whenever you want to get the grid just the way you like it, and you can save custom views as many as you like.
You can also go back and use the edit icon to rename it or delete it later on, or to return to the default view.
I think this is a very good function in Magento 2 :)
Answered by Key Shang on December 14, 2021
If you define grid collection thought layout than you can use updater to add default filter.
<argument name="dataSource" xsi:type="object">
TutorialSimpleNewsModelResourceNewsCollection
<updater>TutorialSimpleNewsModelResourceNewsCollectionUpdater</updater>
</argument>
and
<?php
namespace TutorialSimpleNewsModelResourceNewsCollection;
class CollectionUpdater implements MagentoFrameworkViewLayoutArgumentUpdaterInterface
{
/**
* Update grid collection according to chosen order
*
* @param TutorialSimpleNewsModelResourceNewsCollection $argument
* @return TutorialSimpleNewsModelResourceNewsCollection
*/
public function update($argument)
{
$argument->addFieldToFilter('you_field', 'value');
return $argument;
}
}
or Extend Grid block
class Grid extends MagentoBackendBlockWidgetGrid
{
protected function _prepareCollection()
{
if ($this->getCollection()) {
foreach ($this->getDefaultFilter() as $field => $value) {
$this->getCollection()->addFieldToFilter($field, $value);
}
}
return parent::_prepareCollection();
}
}
Answered by KAndy on December 14, 2021
you need to add this inside the arguments tag:
<argument name="default_filter" xsi:type="array">
<item name="field_name_here" xsi:type="string">value here</item>
</argument>
if your arguments are contained in this block
<block class="MagentoBackendBlockWidgetGrid" name="some.name.here" as="grid">
you need to create your own class that extends MagentoBackendBlockWidgetGrid
like this:
<?php
namespace NamespaceModuleBlockAdminhtmlWhatever;
class Grid extends MagentoBackendBlockWidgetGrid
{
public function _construct()
{
parent::_construct();
if ($this->hasData('default_filter')) {
$this->setDefaultFilter($this->getData('default_filter'));
}
}
}
and modify the block tag above to
<block class="NamespaceModuleBlockAdminhtmlWhateverGrid" name="some.name.here" as="grid">
if you already use a custom grid and not the default MagentoBackendBlockWidgetGrid
you don't need to create the class.
You just need to copy the _construct
method from above in your class.
Answered by Marius on December 14, 2021
here is the reference on devdocs:
http://devdocs.magento.com/guides/v2.0/ui-components/ui-secondary-filter.html
If You need more info on that - you can create a pull request or an issue via docs page - just click "Edit this page on GitHub" link. Docs are constantly updated and improved.
Answered by EquinoxBlack on December 14, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP