Yii2 Gridview Custome column not display sort - yii2

I custome Gridview Column so sort not display
[
'attribute' => 'name',
'format' => 'raw',
'header' => $type == 1 ? 'Tên khách hàng' : 'Tên liên hệ',
'value' => function ($model) {
return Html::a($model->name, ['update', 'id' => $model->id], ['class' => 'alink']);
},
],
and sort not display
So how customer header and add sort?

i comment attribute header so it work.

i change header by label attribute so it work perfect.

Related

How to reset kartik depdrop to initial state with initial data

i have a depdrop in my project like
<?= $form->field($model, 'neighborhood_id')->widget(Select2::classname(), [
'data' => ArrayHelper::map($query,'id','placeWithCity'),
'options' => ['placeholder' => 'Select Your Neighborhood','id'=>'select_place'],
])->label(false); ?>
And
<?=$form->field($model, 'building_id')->widget(DepDrop::classname(), [
'data' =>ArrayHelper::map(Buildings::find()->all(),'id','buildingWithPlace'),
'options' => ['placeholder' => 'Select The Building','id'=>'select_building'],
'type' => DepDrop::TYPE_SELECT2,
'pluginOptions' => [
'depends' => ['select_place'],
'url'=>Url::to(['property-commercial-rent/buildings']),
'loadingText' => 'Loading buildings ...',
]
])->label(false);?>
The second dropdown is depend on the first one.Initially they both have full list of data without any filtering.When i select a neighborhood in the first dropdown then the second populates with the building names under that neighborhood. Then i have a reset button like and has an action like
$( "#reset-location" ).click(function() {
$(select_place).val('').trigger('change');
$(select_building).val('').trigger('change');
});
And this click resetting the both select.BUt the problem is the building has only the items under the previously select neighborhood.I want the make it all like initial stage.How can i do this
You don't need a reset button. On the neighbourhood field allow clear, so it would be like this
<?= $form->field($model, 'neighborhood_id')->widget(Select2::classname(), [
'data' => ArrayHelper::map($query,'id','placeWithCity'),
'options' => ['placeholder' => 'Select Your Neighborhood','id'=>'select_place'],
'pluginOptions' => [
'allowClear' => true
],
])->label(false); ?>
Then there will be a x in the field to clear it.

Yii2 logout Method Not Allowed (#405)

I'm using adminlte advanced template for backend. I want to add logout in the left column.
I've read other posts and I understand I've to add data method post. I've added it in following line in left.php file, but it doesn't work. How to make it work?
<?= dmstr\widgets\Menu::widget(
[
'options' => ['class' => 'sidebar-menu tree', 'data-widget'=> 'tree'],
'items' => [
['label' => 'Logout', 'icon' => 'file-code-o', 'url' => ['/site/logout'], 'data-method'=>'post'],
]
) ?>
It is extending the yii\widgets\Menu and you need to specify the template to modify or add any attribute to the link as the data-method="post" needs to be added to your link you should change the code to the following
echo
dmstr\widgets\Menu::widget(
[
'options' => ['class' => 'sidebar-menu tree', 'data-widget'=> 'tree'],
'items' => [
['label' => 'Logout', 'icon' => 'file-code-o', 'url' => ['/site/logout'], 'template'=>'{label}'],
]
);
You can add a form in to your click field:
$items[] = [
[
'label' => 'Logout',
'icon' => 'file-code-o',
'url' => ['/site/logout'],
'template' => Html::beginForm(array('site/logout')) .
Html::submitButton('Logout') . Html::endForm(),
],
];

Yii2 - Search link in the gridview not working

In my yii2 project, I'm using Pjax GridView.
My index page:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'batch',
[
'attribute' => 'file_import',
'format' => 'raw',
'value'=>function ($data) {
return Html::a($data->file_import, ['/device/index', 'DeviceSearch', 'batch' => $data->batch]);
},
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
The link in the file_import column goes to http://localhost/index.php/device/index?1=DeviceSearch&batch=200325806610154437. But in this url, all the data is showing instead of showing only the search result. I wanted to set the file_import column as an url which will show only the search result by the provided parameter in the url.
Thank you in advance.
Change URL route to
['/device/index', 'DeviceSearch[batch]' => $data->batch]

Yii2. Kartik Select2 widget width

My Select2 control is inside column with bootstrap class 'col-sm-3'. Also it is attached to form field. But width does not want to be 100% of parent column. If I make browser window smaller (after columns auto-rearangment) Select2 follows width of the parent column. Also if I specify width in px it changes the width. What could be a problem? Same story for form->field->textInput. Also I found the field width does not follow parent column width if the form is inline.
<?= $form->field($model, 'categories')->widget(Select2::className(), [
'data' => Category::availableCategories(),
'model' => $model,
'attribute' => 'categories',
'theme' => Select2::THEME_KRAJEE,
'size' => Select2::MEDIUM,
'showToggleAll' => false,
'language' => 'en',
'options' => ['placeholder' => 'Select a category...', 'id' => 'myselect'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
'width' => '200px',
],
])
?>
I had inline layout in my form. This was a problem.

Yii 2 Editable, how to use editable inside form?

$form = ActiveForm::begin();
..
echo Editable::widget([ //this break outter form, because this generate another form
'name'=>'person_name',
'asPopover' => true,
'value' => 'Kartik Visweswaran',
'header' => 'Name',
'size'=>'md',
'options' => ['class'=>'form-control', 'placeholder'=>'Enter person name...']
]);
ActiveForm::end();
So, I tried,
echo Form::widget([
'model'=>$model,
'form'=>$form,
'columns'=>1,
'attributes'=>[
'title'=>[
'label'=>false,
'type' => Editable::INPUT_TEXT,
'widgetClass' => Editable::className(),
'options' => [
'asPopover' => true,
]
],
]
]);
but, it shows input box all the time, not editable text.
how can I use editable widget inside form? without breaking outter form?
You can try this way:
<?= $form->field($model, 'person_name')->Editable::widget([
'name'=>'person_name',
'asPopover' => true,
'value' => 'value',
'header' => 'Name',
'size'=>'md',
'options' => ['class'=>'form-control', 'placeholder'=>'Enter person name...']
]);
?>
Note : Not tested yet.