hyva-themes / magento2-hyva-admin

This module aims to make creating grids and forms in the Magento 2 adminhtml area joyful and fast.
https://hyva-themes.github.io/magento2-hyva-admin/
BSD 3-Clause "New" or "Revised" License
168 stars 39 forks source link

Grid Filters for Grid with source arrayProvider Not working #62

Open tajveez-73 opened 2 years ago

tajveez-73 commented 2 years ago

Hi,

I am making a Hyva grid with source as arrayProvider and have given filters tag in the XML file like following

<filters>
    <filter column="order_id" />
    <filter column="customer_name" />
    <filter column="posted_by"/>
    <filter column="created_at"/>
</filters>

the filters are showing on the grid they aren't working. Is this how we add filters to the grid with the source as arrayProvider or am I missing something.

Thanks.

Vinai commented 2 years ago

That should work. I can apply filters to grids with an array source. I'll have too look into this.

renttek commented 3 months ago

Hey @Vinai

I think I found the problem. When the grid is prepared, the columsn/definition is extracted using \Hyva\Admin\Model\GridSourceType\ArrayProviderGridSourceType::getColumnKeys and \Hyva\Admin\Model\GridSourceType\ArrayProviderGridSourceType::getColumnDefinition. Each of those two calls \Hyva\Admin\Model\GridSourceType\ArrayProviderGridSourceType::getFirstRow internally, which in turn calls \Hyva\Admin\Model\GridSourceType\ArrayProviderGridSourceType::fetchData with some dummy search criteria:

 $searchCriteria = $this->searchCriteriaBuilder->setPageSize(1)->setCurrentPage(1)->create();
 return values($this->gridSourceDataAccessor->unbox($this->fetchData($searchCriteria)))[0] ?? [];

The problem here is, that fetchData with memoize the results, but it does not distinguish between the dummy search criteria and the real one. So when the real search criteria is passed to fetchData, the method will just return the memoized data from the dummy search criteria.

Locally I solved it with a quick and dirty patch, adding a forceRefresh parameter. (but I don't think this is a very good solution):

--- Model/GridSourceType/ArrayProviderGridSourceType.php
+++ Model/GridSourceType/ArrayProviderGridSourceType.php
@@ -136,9 +136,9 @@
      *
      * @return RawGridSourceContainer
      */
-    public function fetchData(SearchCriteriaInterface $searchCriteria): RawGridSourceContainer
+    public function fetchData(SearchCriteriaInterface $searchCriteria, bool $forceRefresh = true): RawGridSourceContainer
     {
-        if (!isset($this->memoizedGridData)) {
+        if ($forceRefresh || !isset($this->memoizedGridData)) {
             $provider = $this->arrayProviderFactory->create($this->arrayProviderClass);

             map(function (HyvaGridSourceProcessorInterface $processor) use ($provider, $searchCriteria): void {
@@ -177,7 +177,7 @@
     private function getFirstRow(): array
     {
         $searchCriteria = $this->searchCriteriaBuilder->setPageSize(1)->setCurrentPage(1)->create();
-        return values($this->gridSourceDataAccessor->unbox($this->fetchData($searchCriteria)))[0] ?? [];
+        return values($this->gridSourceDataAccessor->unbox($this->fetchData($searchCriteria, false)))[0] ?? [];
     }

     public function extractTotalRowCount(RawGridSourceContainer $rawGridData): int
vivekmehta17 commented 1 month ago

Hi renttek

I am also trying to add the filter with the same process as adding below filter . I am calling the grid data with \Vendor\Salesreport\Model\SalesArrayProvider and returning the order data from getHyvaGridData(): array function but when adding the filters its not working how do I get filter data and add public function fetchData in same file any way ?

Thanks