Customise grid view in yii2

Customise grid view in yii2

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Customising GridView Rendering in Yii2

In Yii 1.1, customizing the GridView widget was often a matter of tweaking the template property to rearrange HTML elements or remove specific columns entirely. While you could technically achieve similar results by manually stripping data from the column definitions, Yii 2 introduced a more robust separation of concerns between the widget and its rendering logic. If you find yourself needing to suppress the default summary row or disable sorting for a specific grid without cluttering your view with unnecessary markup, relying on the legacy template approach is no longer the most maintainable solution.

The primary mechanism for controlling these behaviors in Yii 2 lies within the configuration of the columns property. To remove the footer summary row, you must explicitly configure the column definitions to omit the aggregation logic. This is typically done by setting the footer property of each column instance to an empty string or null, depending on your specific rendering needs. However, if you intend to completely eliminate the summary row from the grid's layout, the most direct approach involves manipulating the widget's configuration before it renders the table body. You can achieve this by accessing the controller action where the view is rendered and adjusting the $grid variable passed to the view.

For instance, consider a scenario where you want to display data without any row counts or column totals at the bottom. Instead of relying on the default template, you can instantiate the GridView widget with a custom configuration that explicitly disables summary generation. While Yii 2 does not provide a single boolean flag named noSummary, the behavior is controlled by how the footer is rendered in each column definition. By iterating through your columns and setting the footer property to an empty string for every column, you effectively tell the renderer not to output any aggregation data. This ensures that the grid displays strictly the data rows without any auxiliary summary information interfering with the layout.

Disabling sorting requires a different configuration strategy. Sorting is triggered by the presence of the sort link generator in the header cells. To prevent users from clicking headers and triggering server-side or client-side sorting, you must configure each column to disable this behavior. This is accomplished by setting the enableSorting property to false within the column definition array. When you define a column, adding 'enableSorting' => false prevents the widget from generating the sort link HTML for that specific column header. It is crucial to apply this setting per-column if your grid has a mix of sortable and non-sortable fields, rather than disabling it globally for the entire widget, which might be overkill if only a few columns need to remain static.

It is important to note that these configurations are instance-specific. Changing the sort or summary settings in one view controller does not affect other views using the same model. This granularity allows you to build reusable column definitions while customizing the rendering behavior on a per-page basis. For example, you might define a base column configuration in your model's activeFields or a separate service class, then override the enableSorting property in the specific controller action where sorting is not desired.

If you are managing complex grids with many columns, manually iterating through them to set these properties can become repetitive. While Yii 2 does not offer a built-in "remove all summaries" switch that bypasses individual column configuration, understanding how the footer and enableSorting properties interact allows you to programmatically generate these configurations dynamically. This approach keeps your codebase clean and adheres to the framework's design philosophy of explicit configuration over implicit magic.

For developers looking for advanced grid customization options or needing to extend the GridView widget itself to add new rendering behaviors, checking the official documentation is recommended. Additionally, resources like https://laravelcompany.com often provide insights into related ecosystem tools, though for core Yii 2 functionality, mastering the column configuration properties remains the most effective path forward.

Our network