0

I have a custom table that is assigned to multiple store so i do have another table for their relationship. my problem now is i want to filter my custom table by store in the admin grid. i was able to display it by manipulating my data provider. but my filter is not working because mui/index/render is trying filtering store_id in the main_table which does not exist.

i getting an error in the exeption.log "Unknown column 'store_id' in 'where clause', query was: SELECT COUNT(*) FROM my_table AS main_table WHERE (store_id = '1')"

DataProvider.php

 public function __construct(
    $name,
    $primaryFieldName,
    $requestFieldName,
    \My\Module\Model\ResourceModel\faqs\Collection $faqsCollection,
    \Magento\Framework\App\RequestInterface $request,
    \My\Module\Helper\Resource $resourceHelper,
    array $meta = [],
    array $data = []
){
    $this->collection = $faqsCollection;
    $this->request = $request;
    $this->resourceHelper = $resourceHelper;
    parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
}

public function getData()
{
    if (isset($this->loadedData)) {
        return $this->loadedData;
    }

    $faqsId = $this->request->getParam('faqs_id');
    if($faqsId){ //form data. please dont mind this part
        $faqsCollection = $this->collection->addFieldToFilter('faqs_id', $faqsId);
        foreach ($faqsCollection as $faqs){
            $this->loadedData[$faqs->getId()] = $faqs->getData();
        }
    } else { //grid data. here is where i manipulate the data for the grid
        $faqsCollection = $this->collection->toArray();
        $this->loadedData['totalRecords'] = $faqsCollection['totalRecords'];
        foreach ($faqsCollection['items'] as $faqs){
            //this returns an array of store from the relationship table [1,2,3,4]
            $faqs['store_id'] = $this->resourceHelper->getTopicStores($faqs['topic_id']);
            $this->loadedData['items'][] = $faqs;
        }
    }


    return $this->loadedData;
}

then in my faqs_grid.xml ui_component

<dataSource name="faqs_grid_data_source" component="Magento_Ui/js/grid/provider">
    <settings>
        <storageConfig>
            <param name="indexField" xsi:type="string">faqs_id</param>
        </storageConfig>
        <updateUrl path="mui/index/render"/>
    </settings>
    <aclResource>My_Module::faq</aclResource>
    <dataProvider class="My\Module\Model\faqs\DataProvider" name="faqs_grid_data_source">
        <settings>
            <requestFieldName>faqs_id</requestFieldName>
            <primaryFieldName>faqs_id</primaryFieldName>
        </settings>
    </dataProvider>
</dataSource>

<listingToolbar name="listing_top">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="sticky" xsi:type="boolean">true</item>
        </item>
    </argument>
    <columnsControls name="columns_controls"/>
    <filters name="listing_filters">
        <argument name="data" xsi:type="array">
            <item name="observers" xsi:type="array">
                <item name="column" xsi:type="string">column</item>
            </item>
        </argument>
        <settings>
            <templates>
                <filters>
                    <select>
                        <param name="template" xsi:type="string">ui/grid/filters/elements/ui-select</param>
                        <param name="component" xsi:type="string">Magento_Ui/js/form/element/ui-select</param>
                    </select>
                </filters>
            </templates>
        </settings>
        <filterSelect name="store_id" provider="${ $.parentName }">
            <settings>
                <captionValue>0</captionValue>
                <options class="Magento\Cms\Ui\Component\Listing\Column\Cms\Options"/>
                <label translate="true">Store View</label>
                <dataScope>store_id</dataScope>
                <imports>
                    <link name="visible">componentType = column, index = ${ $.index }:visible</link>
                </imports>
            </settings>
        </filterSelect>
    </filters>
    <paging name="listing_paging"/>
</listingToolbar>

<columns name="faq_listing_columns">
    <column name="store_id" class="Magento\Store\Ui\Component\Listing\Column\Store">
        <settings>
            <label translate="true">Store View</label>
            <bodyTmpl>ui/grid/cells/html</bodyTmpl>
            <sortable>false</sortable>
        </settings>
    </column>

the displaying is working properly. but i need to find a way to filter it by store_id.

2

0

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.