Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sonata Admin List View, make more headers sort buttons?

I have an Admin class which has this definition of listFields:

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
            ->addIdentifier('type')
            ->add('created_at', 'datetime')
            ->add('updated_at', 'datetime')
            ->add('created_by')
            ->add('updated_by')
            ->add('is_active')
            ->add('is_deleted')
            ->add('_action', 'actions',
                    array(
                'actions' => array(
                    'view' => array(),
                    'edit' => array(),
                    'delete' => array()
                )
            ))
    ;

}

Only the "type" column is sortable - IE, when you hover over the table header for "Type" you see an asc/desc arrow and can click to re-order the rows based on this column.

How do I get that to show up on more columns?

I tried adding sortable=true but then it's trying to join to another Entity.

like image 720
Jessica Avatar asked Jul 24 '13 15:07

Jessica


2 Answers

# we can sort the related entity properties like. This following condition site is an entity

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('name')
        ->add('site',null,array(
            'sortable'=>true,
            'sort_field_mapping'=> array('fieldName'=>'name'),
            'sort_parent_association_mappings' => array(array('fieldName'=>'site')
            )))
    ;
}

this is the way to sort the related entities in list configuration. Just check this Sort list by an entity field

like image 159
Gara Avatar answered Oct 09 '22 12:10

Gara


Sonata will be able to sort a field if it knows what type it is ; if you list a related entity, it will be impossible to sort.

Here is the configureListFields() from an entity "Event" which has a title and is linked to another entity "City".

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
            ->addIdentifier('title')
            ->add('city')
}

A link will be created to the city but it will not be sortable, instead adding a specific field from "City" will work :

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('title')
        ->add('city.name')
}

Now it's sortable.

like image 25
BatsaxIV Avatar answered Oct 09 '22 11:10

BatsaxIV