Forums › Forums › OroPlatform › OroPlatform – Programming Questions › How to add columns to contact datagrid and change an order?
This topic contains 9 replies, has 4 voices, and was last updated by Pedro Molina 6 years, 10 months ago.
Starting from March 1, 2020 the forum has been switched to the read-only mode. Please head to StackOverflow for support.
- CreatorTopic
- April 16, 2014 at 6:32 am #33334
I have clean installation of Orocrm with some user data. I created a ContactBundle in src directory do adjust templates to my needs. When i put datagrid.yml into my bundle i get error: “Datagrid source “Array” does not exist”. How can i add columns and change the order?
- CreatorTopic
- AuthorReplies
- April 17, 2014 at 1:55 am #33335
Hello, klipach.
Look’s like you set incorrect datagrid source type parameter. Could you, please, put your datagrid configuration here?
April 17, 2014 at 3:26 am #33336Hello @klipach
Please take a look to our standalone package, there is an example how bundle could be extended, how datagrid could be modified. And also take a look to event listener that changes columns order.
April 27, 2014 at 2:03 am #33337Is there some where an example how can i change columns order and add columns in Customers grid (or any other grid)?
April 28, 2014 at 2:04 am #33338Basic solution for that is to create listener on buildBefore that will update datagrid configuration – there you can customize ordering and add other columns.
Listener example: https://github.com/orocrm/crm/blob/master/src/OroCRM/Bundle/CallBundle/EventListener/Datagrid/CallListener.phpAdvanced way is to extend datagrid, update it and reuse in your bundle.
Configuration example: https://github.com/orocrm/crm/blob/master/src/OroCRM/Bundle/ContactBundle/Resources/config/datagrid.yml#L345May 22, 2014 at 1:19 am #33339Thank you for a reply, as i understand if i want to modify contacts-grid i can do it only via listener?
I need to show middleName, address and city.I found a way how to remove columns from datagrid but can’t add new one. Can you give me a hint how to do it? Here is my Listener:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566<?phpnamespace My\ContactBundle\EventListener\Datagrid;use Doctrine\ORM\EntityManager;use Oro\Bundle\DataGridBundle\Datasource\Orm\OrmDatasource;use Oro\Bundle\DataGridBundle\Event\BuildAfter;use Oro\Bundle\DataGridBundle\Event\BuildBefore;use Oro\Bundle\DataGridBundle\Datagrid\Common\DatagridConfiguration;class CallListener{/*** @var EntityManager*/protected $entityManager;/*** @param EntityManager $entityManager*/public function __construct(EntityManager $entityManager){$this->entityManager = $entityManager;}/*** Remove useless fields in case of filtering** @param BuildBefore $event*/public function onBuildBefore(BuildBefore $event){$config = $event->getConfig();$this->removeColumn($config, 'countryName');$this->removeColumn($config, 'source');$this->removeColumn($config, 'contactSourceLabel');}/*** @param DatagridConfiguration $config* @param string $fieldName*/protected function removeColumn(DatagridConfiguration $config, $fieldName){$config->offsetUnsetByPath(sprintf('[columns][%s]', $fieldName));$config->offsetUnsetByPath(sprintf('[filters][columns][%s]', $fieldName));$config->offsetUnsetByPath(sprintf('[sorters][columns][%s]', $fieldName));}/*** Add required filters** @param BuildAfter $event*/public function onBuildAfter(BuildAfter $event){$datasource = $event->getDatagrid()->getDatasource();if ($datasource instanceof OrmDatasource) {/** @var QueryBuilder $query */$queryBuilder = $datasource->getQueryBuilder();}}}May 22, 2014 at 9:44 am #33340You should add column to query builder select and add columns configuration with sorters and filters.
I’d recommend you to do it in datagrid.yml in ContactBundle first just to find out and test how it should look like, and then do exactly the same thing from listener.
Here is example if such listener https://github.com/orocrm/platform/blob/master/src/Oro/Bundle/WorkflowBundle/Datagrid/WorkflowStepColumnListener.php#L131 – don’t look at business logic, just try to find how configuration is modified.
May 23, 2014 at 1:26 am #33341Thank you, did it. And how i can change the order of columns in the listener?
May 23, 2014 at 2:16 am #33342You simply have to put columns in correct order – f.e. you can use array_splice to inject your column in the middle of configuration. Simple example (without array_splice) can be found here https://github.com/orocrm/OroCRMMagentoContactUsBundle/blob/master/Datagrid/ContactRequestDatagridHelper.php.
May 26, 2017 at 6:43 am #33343Hi, how did you manage to add columns? i still dont get to do that with this method. I have this code to add some columns, but cant add certain columns to case grid and im unable to apply a method in the “field” value, for example:
I need to show the “fullName” of related contact column of the case datagrid, that is a concatenation of first name and last name. Here is my code, if anyone can help me: - AuthorReplies
The forum ‘OroPlatform – Programming Questions’ is closed to new topics and replies.