How to sort just one attribute values in yii2? - yii2

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])

Related

Wrong rendering search field kartik select2

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 to sort values from database using Select2-widget in yii2

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']
],

Yii2 Girdview User Ordered Columns

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
]
]);

Editable row with number type

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

yii2 grid view searching ,sorting ,filtering for two table

How to searching ,sorting ,filtering for two table in yii2 grid view? after generating from gii tool ,i got searching sorting for one table columns but i have to apply searching ,sorting ,filtering on columns of another table.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'username',
// 'account_activation_token',
'email:email',
'profile.name',
[
'attribute' => 'name',
'value' => 'profile.name',
'enableSorting' => true,
//here i want to use the filter option just like in yii 1.1 but i dont know how to use the textbox search in yii2.0
],
// 'usertype',
// 'status',
// 'created_at',
// 'updated_at',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Try Using:
[
'attribute' => 'name',
'value' => 'profile.name',
'enableSorting' => true,
'filter' => function($model){
return Html::activeTextInput($searchModel, $this->name),
}
],