0

i have a grid in which i display data from a DataProvider. The issue is when i click on a row, it returns the following javascript issue:

actions.js:216 Uncaught TypeError: Cannot read property 'index' of undefined
    at UiClass._getCallback (actions.js:216)
    at UiClass.applyAction (actions.js:156)
    at registry.js:59
    at Registry._resolveRequest (registry.js:418)
    at Registry._addRequest (registry.js:385)
    at Registry.get (registry.js:229)
    at async (registry.js:58)
    at column.js:291
    at UiClass.applySingleAction (column.js:253)
    at UiClass.applyFieldAction (column.js:228)

I assume something's wrong with either the configuration of the grid, of the columns or the actionsColumn.

The code for my grid is the following:

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">my_grid.my_grid_data_source</item>
            <item name="deps" xsi:type="string">my_grid.my_grid_data_source</item>
        </item>
        <item name="spinner" xsi:type="string">my_columns</item>
    </argument>

...

    <columns name="my_columns">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="childDefaults" xsi:type="array">
                    <item name="fieldAction" xsi:type="array">
                        <item name="provider" xsi:type="string">my_grid.my_grid.my_columns.actions</item>
                        <item name="target" xsi:type="string">applyAction</item>
                        <item name="params" xsi:type="array">
                            <item name="0" xsi:type="string">view</item>
                            <item name="1" xsi:type="string">${ $.$data.rowIndex }</item>
                        </item>
                    </item>
                </item>
            </item>
        </argument>

...

        <actionsColumn name="actions" class="Company\Module\Ui\Component\Listing\Column\MyActions">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="resizeEnabled" xsi:type="boolean">false</item>
                    <item name="resizeDefaultWidth" xsi:type="string">100</item>
                    <item name="viewUrlPath" xsi:type="string">*/*/edit</item>
                    <item name="urlEntityParamName" xsi:type="string">id</item>
                    <item name="indexField" xsi:type="string">id</item>
                    <item name="sortOrder" xsi:type="number">200</item>
                </item>
            </argument>
        </actionsColumn>
    </columns>
</listing>

and in MyActions class i have the following:

...
public function prepareDataSource(array $dataSource)
{
    if (isset($dataSource['data']['items'])) {
        foreach ($dataSource['data']['items'] as & $item) {
            $name = $this->getData('name');
            if (isset($item['id'])) {
                
                $item[$name]['edit'] = [
                    'href' => $this->urlBuilder->getUrl(
                        self::MY_URL_PATH_EDIT,
                        [
                            'id' => $item['id']
                        ]
                    ),
                    'label' => __('Edit')
                ];

            }
        }
    }
}
                
...

Any idea what am i missing?

2
  • What is name of primary key in DB?
    – Ankit
    Mar 29, 2021 at 11:35
  • Hello @Ankit, the primary key field is "id"
    – Robert
    Mar 29, 2021 at 11:39

2 Answers 2

0

You don't have indexField in your XML, this field helps initialize the actions array. I suggest you copy from Magento's core modules (any) and modify it instead of writing your own.

Link example to cms module: https://github.com/magento/magento2/blob/81672a278634e59965d2694e8f9107abf104ab14/app/code/Magento/Cms/view/adminhtml/ui_component/cms_block_listing.xml#L188

2
  • Hello Jimmy, i actually do have the indexField, it's <item name="indexField" xsi:type="string">id</item>
    – Robert
    Mar 30, 2021 at 6:42
  • My bad. Everything looks ok to me. For situation like this, I always debug the JS to check why the error happen.
    – Jimmy
    Mar 30, 2021 at 6:48
0

I actually figured it out, i had two issues:

1) i needed to add a column with the "id" field in the grid. (my primary key from the database table). That is because i was using that key in my MyActions.prepareDataSource() function.

$item[$name]['edit'] = [
    'href' => $this->urlBuilder->getUrl(
        self::MY_URL_PATH_EDIT,
        [
            'id' => $item['id']
        ]
    ,
    'label' => __('Edit')
];

2) in the "<settings>" section of the columns, i had to write "edit" instead of "view" to match the action from the MyActions.prepareDataSource() which was "edit":

XML:

<item name="0" xsi:type="string">edit</item>

PHP:

 $item[$name]['edit'] = [ ... ]

After this, it works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.