The current implementation of Serial Column is not capable of working with a SORT_DESC. It would be logical to have this feature in scenarios in which you are representing the most recent records first (which is very common).
Two options:
A) parameter in SerialColumn
B) a class like ReverseSerialColumn
what is the benefit of this column anyway? The number is not unique when data changes...
this column is useful for people to know how many items they have active, how many articles they have created, how many messages they still have open, etc etc.
I would like to have that option as well.
@rokasr are you sure you're talking about serial column and not sortable ids?
Yes I am pretty sure about that. I recently needed something like "B) a class like ReverseSerialColumn"
I can describe situation for you: my customer asked me to sort their users by created_at DESC. I did that, however they were not pleased by seeing that newest user has #1 next to it, since they know their first user registered long time ago. All I would have to do is to reverse those numbers, yet there is no function like that at the moment.
Instead of a new class how about using DataColumn::$value
[
'name' => 'Descending order',
'value' => function ($model, $key, $index, $column) {
return $column->grid->dataProvider->totalCount - $index + 1;
}
]
we can also add a property value to SerialColumn but then it will be equal to DataColumn imo
I don't think we must implement ReverseSerialColumn in the core code.
easy code formula :
totalCount - (serialIndex) + 1
example :
protected function renderDataCellContent($model, $key, $index)
{
$pagination = $this->grid->dataProvider->getPagination();
if ($pagination !== false) {
return $pagination->totalCount - ($pagination->getOffset() + $index + 1) + 1;
} else {
return $pagination->totalCount - ($index + 1) + 1;
}
}
Thanks to @agussuhartono I write it as class
class ReverseSerialColumn extends Column
{
/**
* @inheritdoc
*/
public $header = '#';
/**
* @inheritdoc
*/
protected function renderDataCellContent($model, $key, $index)
{
$pagination = $this->grid->dataProvider->getPagination();
if ($pagination !== false) {
return $pagination->totalCount - ($pagination->getOffset() + $index);
} else {
return $this->grid->dataProvider->getTotalCount() - ($index);
}
}
}
And use in gridView as:
'columns' => [
[
'class' => ReverseSerialColumn::class,
],
]
great @mkozlovv