Important

You are browsing the documentation for version 4.1 of OroCommerce, OroCRM and OroPlatform, which is no longer maintained. Read version 5.1 (the latest LTS version) of the Oro documentation to get up-to-date information.

See our Release Process documentation for more information on the currently supported and upcoming releases.

Array Datasource

This datasource provides the ability to set data for the datagrid from thearray.

Example

1 datagrids:
2     DATAGRID_NAME_HERE:
3         source:
4             type: array

Configuration

To configure datasource, create a datagrid event listener and subscribe to the oro_datagrid.datagrid.build.after.DATAGRID_NAME_HERE event.

1 acme_bundle.event_listener.datagrid.my_custom_listener:
2     class: Acme\Bundle\AcmeBundle\EventListener\Datagrid\MyCustomListener
3     tags:
4         - { name: kernel.event_listener, event: oro_datagrid.datagrid.build.after.DATAGRID_NAME_HERE, method: onBuildAfter }
 1 <?php
 2
 3 namespace Acme\Bundle\AcmeBundle\EventListener\Datagrid;
 4
 5 use Oro\Bundle\DataGridBundle\Datasource\ArrayDatasource\ArrayDatasource;
 6 use Oro\Bundle\DataGridBundle\Event\BuildAfter;
 7 use Oro\Bundle\DataGridBundle\Exception\UnexpectedTypeException;
 8
 9 class MyCustomListener
10 {
11     /**
12     * @param BuildAfter $event
13     */
14     public function onBuildAfter(BuildAfter $event)
15     {
16         $datagrid = $event->getDatagrid();
17         $datasource = $datagrid->getDatasource();
18
19         if (!$datasource instanceof ArrayDatasource) {
20             throw new UnexpectedTypeException($datasource, ArrayDatasource::class);
21         }
22
23         // Create datagrid source array
24         $source = [
25             // row 1
26             [
27                 'first_column' => 'Value in first row and first column',
28                 'second_column' => 'Value in first row and second column'
29             ],
30             // row 2
31             [
32                 'first_column' => 'Value in second row and first column',
33                 'second_column' => 'Value in second row and second column'
34             ],
35             // ...
36         ];
37
38         $datasource->setArraySource($source);
39     }
40 }

Predefined columns can be defined using the following configuration:

 1 datagrids:
 2     DATAGRID_NAME_HERE:
 3         source:
 4             type: array
 5         columns:
 6             first_column:
 7                 label: Column 1 Label
 8         sorters:
 9             columns:
10                 first_column:
11                     data_name: first_column