Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grid Filters for Grid with source arrayProvider Not working #62

Open
tajveez-73 opened this issue Jan 3, 2022 · 3 comments
Open

Grid Filters for Grid with source arrayProvider Not working #62

tajveez-73 opened this issue Jan 3, 2022 · 3 comments
Assignees

Comments

@tajveez-73
Copy link

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
Copy link
Collaborator

Vinai commented Mar 17, 2022

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

@Vinai Vinai self-assigned this Mar 17, 2022
@renttek
Copy link

renttek commented May 23, 2024

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
Copy link

vivekmehta17 commented Jul 10, 2024

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 ?















</filters>

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants