i using gridview display data. need add default sorting 2 columns follows:
(case when (inbound_datetime null , outbound_datetime < now()) or (inbound_datetime not null , inbound_datetime < now()) 9999 when (inbound_datetime not null , outbound_datetime < now()) inbound_datetime else outbound_datetime end) asc
if add code in order by
clause in data provider query, of other sorting options don't work because condition applied query itself.
i tried apply default sorting other grid, not working (the method searches attribute name):
'sort' => [ 'defaultorder' => [ '(case when (inbound_datetime null , outbound_datetime < now()) or (inbound_datetime not null , inbound_datetime < now()) 9999 when (inbound_datetime not null , outbound_datetime < now()) inbound_datetime else outbound_datetime end)' => sort_asc, ], ],
is there way make default sorting work , still keep ability use grid sorting column names correctly?
i did more thinking , have come solution problem.
i define query in controller method opens view. added condition checks if sort parameter passed request. if passed, create query without order by
clause. if sort parameter not passed (no column sorting done), create query order by
clause. in controller method:
if (yii::$app->request->get('sort')) { $query = travel::find(); } else { $query = travel::find() ->orderby('(case when (inbound_datetime null , outbound_datetime < now()) or (inbound_datetime not null , inbound_datetime < now()) 9999 when (inbound_datetime not null , outbound_datetime < now()) inbound_datetime else outbound_datetime end) asc'); }
Comments
Post a Comment