1

I want to sort a column in TbJSONGridView by date , I have this code

$this->widget(
        'bootstrap.widgets.TbJsonGridView', array(
    'dataProvider' => $model->searchPending(),    
    'type' => 'striped bordered condensed',
    'summaryText' => false,
    'cacheTTL' => 10, // cache will be stored 10 seconds (see cacheTTLType)
    'cacheTTLType' => 's', // type can be of seconds, minutes or hours
    'enablePagination' => true,
    'columns' => array(
 array(
            'name' => 'Pickup Date',
            'value' => '$data->carShippeds[0]->pickup_date',
            'type'=>'date',
        ),
))

but the column I get it is from a related model, and the sorting is not working, when I clicked on the column header it does nothing.

what's wrong with the code???

Gabriel

General Electric
  • 1,176
  • 3
  • 21
  • 44

2 Answers2

0
$data->carShippeds[0]->

This shows us that the current model has many cars shipped. You are only showing the first one in there. While that works, Yii has no way of knowing that you are only showing the first value so your sorting has no chance of working. Create a view that only selects your first pickup date, make a join in your criteria with the main model, show the field without [0]-> and it will work.

Mihai P.
  • 9,307
  • 3
  • 38
  • 49
0

Change the below section:

array(
     'name' => 'Pickup Date',
     'value' => '$data->carShippeds[0]->pickup_date',
     'type'=>'date',
),

To this:

array(
     'name' => 'pickup_date',
     'header' => 'Pickup Date',
     'value' => '$data->carShippeds[0]->pickup_date',
     'type'=>'date',
),

Then in your 'form' when you want to return DataProvider write the specific sorting code for this association like below:

return new DataProvider($query, [
            'pagination' => [
                'pagesize' => 20,
            ],
            'sort' => [
                'sortVar' => 'sort',
                'defaultOrder' => ['t.carShippeds.pickup_date asc'],
                'attributes' => [
                    'pickup_date' => [
                        'asc' => 't.carShippeds.pickup_date',
                        'desc' => 't.carShippeds.pickup_date DESC',
                    ],
                    '*',
                ],

            ],
]);

Please notice that I did not know the join alias you have for your t.carShippeds or even whether it is t or another entity. replace it or give me more information about your DataProvider and I can help more.

Mohamad Eghlima
  • 970
  • 10
  • 23