Controlling Column Position in Magento Grid
When working with custom grids in Magento, you might face issues where columns are always positioned on the right side of the table, regardless of your intentions. This can happen even when calling addColumn multiple times with different positions.
Let’s dive into the problem and find a solution to force column positioning in Magento.
The Issue: Automatic Column Positioning
By default, Magento automatically manages the position of columns in a grid. When you add columns using addColumn, they are appended to the right side of the existing ones. This behavior is controlled by Magento’s core functionality that handles the rendering and layout of admin grids.
Here’s an example of how you might be adding custom columns:
$this->addColumn('pagamenti', array(
'header' => 'Paymentsource',
'width' => '50px',
'align' => 'left',
'type' => 'text',
'renderer' => 'Blablabla_Adminhtml_Block_Sales_Order_Renderer_lol'
));
Even if you add this column before or after others, Magento will always place it on the right side of the table.
Forcing Column Position: The Solution
To gain control over column positioning in your custom grid, you need to understand how Magento’s core grid rendering works. By default, the addColumn method is called inside a loop that iterates through an array of columns. Each call appends the new column to the end of this array.
However, we can override this behavior by creating our own custom grid and implementing the getColumns method manually.
Creating a Custom Grid
First, you need to create your own custom grid class that extends Magento’s base grid class (Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult).
class MyCustomGrid extends \Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
{
public function getColumns()
{
return [
// Define your columns here with their desired positions
'column1' => [
'name' => 'column1',
'index' => 'column1_index', // The column name in the data provider
'header' => [
'label' => __('Column 1'),
'sortable' => false,
],
'filter' => [
'label' => __('Filter Column 1'),
'condition' => ['name' => '%value%', 'type' => 'text'],
],
],
// Add more columns as needed
];
}
}
In this example, we define the column1 array with its name, index, header configuration, and filter settings. By defining all your columns in this method, you gain full control over their order and positioning.
Connecting Your Custom Grid
Next, you need to connect your custom grid to the data provider and load it into the layout file (e.g., sales_order_index.xml).
<referenceContainer name="content">
<block class="Magento\Framework\View\Element\UiComponent\DataProvider" name="my_custom_grid_data_provider">
<arguments>
<argument name="dataSourceId" xsi:type="string">my_custom_grid_datasource</argument>
<argument name="columns" xsi:type="array">
<item name="columns" xsi:type="array">
<item name="column1" xsi:type="array">
<item name="name" xsi:type="string">column1</item>
<item name="index" xsi:type="string">column1_index</item>
<item name="component" xsi:type="string">Magento_Ui/js/grid/columns/input</item>
</item>
</array>
</argument>
</arguments>
</block>
<block class="Magento\Framework\View\Element\UiComponent\DataProvider" name="my_custom_grid">
<arguments>
<argument name="dataSourceId" xsi:type="string">my_custom_grid_datasource</argument>
</arguments>
</block>
</referenceContainer>
In this XML configuration, we create a new data provider with the desired columns and connect it to your custom grid block.
Conclusion: Gaining Control Over Column Positioning
By creating a custom grid class and implementing the getColumns method manually, you can gain full control over the positioning of columns in Magento’s admin grids. This approach allows you to define the exact order and placement of your custom columns without relying on Magento’s default automatic positioning.
Remember that this solution requires a deeper understanding of Magento’s core grid rendering process and may not be suitable for all cases. However, for advanced customizations where precise column control is necessary, this method provides an effective way to override Magento’s default behavior.
For more information on working with grids in Magento, you can refer to the official documentation and resources available on Laravel Company.