I use yii2 and I want to make gridview column 'description' sortable in a case insensitive way. There is my code:
$dataProvider = new ArrayDataProvider([
'allModels' => $query->find(),
'sort' => [
'attributes' => ['name','description],
],
'pagination' => [
'pageSize' => $this->pageSize,
],
]);
When I click on column description to sort, it show like this:
As you see it sort case sensitive I want to sort case Insensitive, how I can do that? Any idea?
In order to sort rows of ArrayDataProvider in a case-insensitive way you should extend ArrayDataProvider itself, because internally it uses ArrayHelper::multisort and if you want it sort the way you want you have to pass SORT_STRING | SORT_FLAG_CASE as fourth argument to the method. By default its value equal to SORT_REGULAR constant.
Here the implementation:
<?php
namespace app\dataproviders;
use yii\helpers\ArrayHelper;
/**
* Class ArrayDataProvider
*/
class ArrayDataProvider extends \yii\data\ArrayDataProvider
{
/** @inheritdoc */
protected function sortModels($models, $sort)
{
$orders = $sort->getOrders();
if (!empty($orders)) {
ArrayHelper::multisort(
$models,
array_keys($orders),
array_values($orders),
SORT_STRING | SORT_FLAG_CASE
);
}
return $models;
}
}
And after it use the extended class instead of \yii\data\ArrayDataProvider
Example of usage:
$dataProvider = \app\dataproviders\ArrayDataProvider([
'allModels' => $query->find(),
'sort' => [
'attributes' => ['name','description'],
],
'pagination' => [
'pageSize' => $this->pageSize,
],
]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With