I'm trying to make one of our GridView modules customisable for our team. I have been able to allow the users the show and hide certain columns using 'visible'=> if ( $data->field_name == "some_value" ) ? true : false,
I want to the users to be able to order the columns to their preference, any idea on how I can accomplish this please.
Thank you.
Gridview columns are sorteable but if you want to add default sorting you can try this with your provider
$dataProvider->setSort([
'attributes' => [
'product_name' => [
'asc' => ['product_name' => SORT_ASC],
'desc' => ['product_name' => SORT_DESC],
'default' => SORT_ASC
],
'date' => [
'asc' => ['date' => SORT_ASC],
'desc' => ['date' => SORT_DESC],
'default' => SORT_ASC,
],
],
'defaultOrder' => [
'date' => SORT_ASC
]
]);
Related
I use the select2 widget from kartik:
echo $form->field($model, 'person_ids')->widget(Select2::classname(), [
'data' => [1 => 'test1', 2 => 'test2'],
'theme' => Select2::THEME_KRAJEE_BS5,
'hideSearch' => true,
'options' => [
'multiple' => true
],
'pluginOptions' => [
'allowClear' => true
],
])->label('Personal');
The rendering of the search field is wrong - it intends the selected options. Any ideas how to fix this?
how sorting values from database using Select2-widget from kartik in framework yii2? Try like this, but nothing is sorting as well as no error, neither in php nor in Js.:
[
'attribute' => 'id_rubrik',
'value' => function($model) {
return $model->rubrik->bez;
},
'filterType' => GridView::FILTER_SELECT2,
'filter' => \yii\helpers\ArrayHelper::map(\backend\models\LRubrik::find()->orderBy('bez DESC')->asArray()->all(), 'id', 'bez'),
'filterWidgetOptions' => [
'pluginOptions' => ['allowClear' => true],
],
'filterInputOptions' => ['placeholder' => 'Bitte wählen..', 'id' => 'grid-ds-substanz-search-id_rubrik']
],
How can i make this input type with number. I mean when the page is opened in mobile phone, it'll be open the number keyboard.
[
'class'=>'kartik\grid\EditableColumn',
'headerOptions' => ['style' => 'width:10%', 'class'=>'text-center'],
'editableOptions'=>[
'asPopover' => false,
'inputType'=>\kartik\editable\Editable::INPUT_TEXT,
// Change here:
'editableValueOptions'=>['type'=>'number']
],
'attribute'=>'quantity',
'label'=>'Quantity',
],
EDIT ->> add 'editableValueOptions'=>['type'=>'number']
Use editableValueOptions. As the documentation says:
editableValueOptions: array, the HTML attributes for the editable value displayed.
[
'class'=>'kartik\grid\EditableColumn',
'headerOptions' => ['style' => 'width:10%', 'class'=>'text-center'],
'editableOptions'=>[
'asPopover' => false,
'inputType'=>\kartik\editable\Editable::INPUT_TEXT,
// Change here:
'editableValueOptions'=>['type'=>'number']
],
'attribute'=>'quantity',
'label'=>'Quantity',
],
'attribute' => 'quantity',
'class' => 'kartik\grid\EditableColumn',
'editableOptions'=>[
'valueIfNull' => 'not set',
'inputType' => \kartik\editable\Editable::INPUT_HTML5,
'options' => [
'type' => 'number',
'min' => '0.5',
'step' => '0.5',
],
],
You should use options array as shown above
tried to check the documentation for that, but I still don't know how to do it on just one attribute.. I have:
'attribute' => 'user_id',
'label' => Module::t('app', 'USER_ID_LABEL'),
'headerOptions' => [
'style' => 'text-align: center;'
],
'contentOptions' => [
'style' => 'text-align: center;'
],
I need to sort that attribute values, how to do it? I checked in the documentation and tried like this:
'attribute' => [
'user_id' => [
'asc' => ['company_name' => SORT_ASC],
],
],
But this didn't work. I need to sort the values automatically by ASC. Thank you for the help
In your search query use :
orderBy(['company_name' => SORT_ASC])
I am trying to add a custom filter named Sort By Month in the grid view header here is an example of the previous version of the site which I am revamping see below image
I was looking into the layout option of the grid view and added a drop-down in the layout template before {items}
GridView::widget(
[
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'layout' => '{summary}{errors}' . \kartik\widgets\Select2::widget(
[
'model' => $searchModel,
'attribute' => 'filter_month',
'theme' => \kartik\widgets\Select2::THEME_DEFAULT,
'data' => $searchModel->getFilterMonths(),
'pluginEvents' => [
"select2:select" => 'function() { $("#w2").submit();}',
// 'select2:select'=> new \yii\web\JsExpression("function(){console.log('here')}"),
],
'options' => [
'placeholder' => '--Select Month--',
],
'pluginOptions' => [
'allowClear' => true,
'width' => '160px',
],
]
) . '{items}{pager}',
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'name',
'title',
'catalog',
'upc_code',
[
'attribute' => 'created_on',
'label' => 'Created On',
'filter' => \yii\jui\DatePicker::widget(['dateFormat' => 'yyyy-MM-dd', 'model' => $searchModel, 'attribute' => 'created_on']),
'format' => 'html',
],
[
'attribute' => 'status',
'label' => 'Status',
'format' => 'raw',
'value' => function ($data) {
switch ($data->status) {
case 0:
return "Being Edited (" . $data->created_on . ")";
break;
case 1:
return ($data->maxdate == '') ? 'Active' : 'Active';
break;
case 2:
return "Expired";
break;
}
},
],
['class' => 'yii\grid\ActionColumn'],
],
]
);
Now I want to submit the filters form when I select an option from the drop-down, but could not figure out how to attach the default filter submit event with the drop-down options so that it filters the results when I select any option in the drop-down.
For Custom filter you have used the perfect layout but you have to provide your custom field as a filterSelector.
"filterSelector" => "#". Html::getInputId($searchModel, 'AttributeName'),