How to combine two attributes on select2 in yii2 - yii

I want to attribute kodepos and kabupaten in one dropdown. and kodepos and kabupaten attributes are included in the database. Can anyone help me? thank you
<?= $form->field($model, 'origin')->label('Origin')->widget(Select2::classname(), [
'data' => ArrayHelper::map(MKota::find()->all(),'kodepos','kodepos'),
'theme' => Select2::THEME_BOOTSTRAP,
'language' => 'en',
'options' => ['placeholder' => 'Pilih Kode Pos','required' => true,'style'=>'width:500px','maxlength' => true],
'pluginOptions' => [
'allowClear' => true
],
]);
?>
<?= $form->field($model, 'origin_name')->label('Origin')->widget(Select2::classname(), [
'data' => ArrayHelper::map(MKota::find()->all(),'kabupaten','kabupaten'),
'theme' => Select2::THEME_BOOTSTRAP,
'language' => 'en',
'options' => ['placeholder' => 'Pilih Kota Asal','required' => true,'style'=>'width:500px','maxlength' => true],
'pluginOptions' => [
'allowClear' => true
],
]);
?>

You need to change the ArrayHelper::map() to use the closure for the third parameter or the $to parameter and return the 2 fields concatenated as you want.
The field names are not clear that you want to show as text in the first and second drop-down so I assume the integer value to be coming from the id field.
I will add the code that will show the id and kodepos concatenated as the text for the first drop-down and show the id and kabupaten concatenated as the text for the second drop-down. Change them accordingly in the code
<?= $form->field($model, 'origin')->label('Origin')->widget(Select2::classname(), [
'data' => ArrayHelper::map(MKota::find()->all(),'kodepos',function($model){return $model->id.$model->kodepos}),
'theme' => Select2::THEME_BOOTSTRAP,
'language' => 'en',
'options' => ['placeholder' => 'Pilih Kode Pos','required' => true,'style'=>'width:500px','maxlength' => true],
'pluginOptions' => [
'allowClear' => true
],
]);
?>
<?= $form->field($model, 'origin_name')->label('Origin')->widget(Select2::classname(), [
'data' => ArrayHelper::map(MKota::find()->all(),'kabupaten',function($model){return $model->id.$model->kabupaten}),
'theme' => Select2::THEME_BOOTSTRAP,
'language' => 'en',
'options' => ['placeholder' => 'Pilih Kota Asal','required' => true,'style'=>'width:500px','maxlength' => true],
'pluginOptions' => [
'allowClear' => true
],
]);
?>
EDIT:
You want the kodepos and kabupaten in one drop-down rather than making 2 drop-downs so change the code to the following
<?= $form->field($model, 'origin')->label('Origin')->widget(Select2::classname(), [
'data' => ArrayHelper::map(MKota::find()->all(),function($model){return $model->kodepos.$model->kabupaten}),function($model){return $model->kodepos.$model->kabupaten}),
'theme' => Select2::THEME_BOOTSTRAP,
'language' => 'en',
'options' => ['placeholder' => 'Pilih Kode Pos','required' => true,'style'=>'width:500px','maxlength' => true],
'pluginOptions' => [
'allowClear' => true
],
]);
?>

Related

duplicate pages issue on installed script website yii framework

i want to specify that i have almost no experience with php and yii framework
I am trying to setup a classified ads website with yii ,but after i did some seo audit i realised that almost all of my category pages have duplicates like this :
example.com/category?slug=cars -this url is a duplicate that i don't want
example.com/category/cars -this is the url that i want to have
This is my urlManager code :
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
'search' => 'site/search',
'contact' => 'site/contact',
'<controller:conversation>/<action:delete>' => '<controller>/<action>',
'<controller:conversation>/<action:reply>/<conversation_uid:[a-z0-9_\-]+>' => '<controller>/<action>',
'<controller:account>/<action:invoices>/<page:\d+>' => '<controller>/<action>',
'<controller:account>/<action:conversations>/<page:\d+>' => '<controller>/<action>',
'<controller:listing>/<action:index|update|package|preview>/<slug:[a-z0-9_\-]+>' => '<controller>/<action>',
'page/<slug:[a-z0-9_\-]+>' => 'pages/index',
'<controller:category>/<action:location|map-view|get-map-location>' => '<controller>/<action>',
[
'pattern' => 'category/<slug:[a-z0-9_\-]+>/<page:\d+>',
'route' => 'category/index',
],
[
'pattern' => 'category/<slug:[a-z0-9_\-]+>',
'route' => 'category/index',
],
[
'pattern' => 'category/map-view/<slug:[a-z0-9_\-]+>/<page:\d+>',
'route' => 'category/map-view',
],
[
'pattern' => 'category/map-view/<slug:[a-z0-9_\-]+>',
'route' => 'category/map-view',
],
[
'pattern' => 'store/<slug:[a-z0-9_\-]+>/<page:\d+>',
'route' => 'store/index',
],
[
'pattern' => 'store/<slug:[a-z0-9_\-]+>',
'route' => 'store/index',
],
'<url:.+/>' => 'site/redirect'
],
],
If you need any further detailes or specific code ask me and i will try to provide !
Thanks

Kartik Grid Editable Column with expandrow styling not applying

When I pull in a kartik expandable row grid, using pjax the editablecolumn styling is not being applied. What can I do to apply css to this column?
<?php echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'pjax' => false,
'columns' => [
[
'class' => 'kartik\grid\ExpandRowColumn',
'value' => function ($model, $key, $index, $column){
return GridView::ROW_COLLAPSED;
},
'detailUrl' => 'index.php?r=controller/detail'
],
Expanded View
<?php echo GridView::widget([
'dataProvider' => $dataProvider,
'pjax'=> true,
'columns' => [
[
'attribute' => 'brand_name',
'value' => function($model,$key,$idx,$col){
return $model['brand_name'];
},
],
[
'class'=>'kartik\grid\EditableColumn',
'hAlign'=>'center',
'vAlign'=>'middle',
'value' => function($model,$key,$idx,$col){
return 100;
},
'editableOptions' => [
'name' => 'Test',
'header' => 'Test22',
'inputType' => Editable::INPUT_TEXT,
'formOptions' => ['action' => ['/book/editbook']],
'options' => [
'convertFormat'=>true,
'pluginOptions' => ['format' => 'php:Y-m-d']
]
]
],
Maybe you should try to set HTML with containerOptions or contentOptions attribute in editableOptions.

Redirect to Home Page using Nav Bar

I want to add one button in Nav bar for redirect to index.php. Please Help me to complete the task.i want to redirect to this below url
http://localhost/Backup/web/index.php
i trying the below code,
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'encodeLabels' => false,
'items' => [
['label' => 'ContactUs', 'url' => ['index.php']],
],
]);
Try this
'url'=> Yii::$app->homeUrl
Try this :
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'encodeLabels' => false,
'items' => [
['label' => 'ContactUs', 'url' => ['web/index']],
],
]);

Yii2 kartik grid export all columns

I am using kartik grid and want to export all columns present in my table.
Problem is that i want to show only few columns in grid but download all columns in csv file.
Is it possible?
My code
<?php
$gridColumns = [
['class' => 'kartik\grid\SerialColumn'],
[
'class' => 'kartik\grid\EditableColumn',
'attribute' => 'Name',
'vAlign'=>'middle',
'headerOptions'=>['class'=>'kv-sticky-column'],
'contentOptions'=>['class'=>'kv-sticky-column'],
'editableOptions'=>['header'=>'Name', 'size'=>'255'],
'format'=>'text',
],
[
'attribute' => 'Description',
'vAlign'=>'middle',
'headerOptions'=>['class'=>'kv-sticky-column'],
'contentOptions'=>['class'=>'kv-sticky-column'],
'format'=>'text',
],
['class' => 'kartik\grid\DataColumn',
'attribute' => 'image',
'format' => 'html',
'value' => function($model, $key, $index, $column) {
return Html::img('http://localhost/demo/basic_demo/web/www/img/'.$model->image, ['class' => 'abc','width'=>70,'height'=>70]);
},
],
[
'class'=>'kartik\grid\BooleanColumn',
'attribute'=>'Status',
'vAlign'=>'middle',
],
[
'class' => 'kartik\grid\ActionColumn',
'dropdown' => true,
'vAlign'=>'middle',
'viewOptions'=>['title'=>'viewMsg', 'data-toggle'=>'tooltip'],
'updateOptions'=>['title'=>'updateMsg', 'data-toggle'=>'tooltip'],
'deleteOptions'=>['title'=>'deleteMsg', 'data-toggle'=>'tooltip'],
],
[
'class' => '\kartik\grid\ExpandRowColumn',
'header' => '',
'value' => function ($model, $key, $index) {
return GridView::ROW_COLLAPSED;
},
'detailUrl' =>Url::to(['expenses/viewdetail']),
],
];
echo GridView::widget([
'dataProvider'=> $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'autoXlFormat'=>true,
'responsive'=>true,
'hover'=>true,
'condensed'=>true,
'floatHeader'=>true,
'bordered'=>true,
'toolbar'=>['{export}',
'{toggleData}',],
'showPageSummary'=>true,
'panel'=>[
'heading'=>'<h3 class="panel-title"><i class="glyphicon glyphicon-globe"></i> '. Html::encode($this->title).'</h3>',
'type'=>'primary',
'before'=>Html::a('<i class="glyphicon glyphicon-plus"></i>Create Expenses', ['create'], ['class' => 'btn btn-primary']),
'showFooter'=>false
],
'pjax'=>true,
'export'=>[
'fontAwesome'=>true,
'showConfirmAlert'=>false,
'target'=>GridView::TARGET_BLANK,
],
]);
?>
You can do like this in your gird column list add all column and set 'hidden' => true property which you don't want to show in grid-view but this column comes in your csv when you export csv. suppose for example here i am showing you for one column you can implement for this thing all columns. check below example code.
[
'attribute' => 'Description',
'vAlign'=>'middle',
'headerOptions'=>['class'=>'kv-sticky-column'],
'contentOptions'=>['class'=>'kv-sticky-column'],
'hidden' => true,
'format'=>'text',
],
now what happen description column will not display in gird view but it is present in exported file. i hope it is helpful for you.
On columns you my add attribute hiddenFromExport => true
For example:
'columns' => [
[
'attribute' => 'inn',
'format' => 'text',
'hiddenFromExport' => true,
],
],
It's best you pass a different array with the full grid columns rather than just the normal $gridColumns used on the web page. To keep it tidy, I have two functions in my $searchModel, one for grid columns and another for export columns. Then when setting your export menu, just call the function that returns your columns:
ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $searchModel->getExportColumns(),
...
]);
In your search model:
public function getGridColumns(){
return [
// page columns
];
public function getExportColumns(){
return [
// export columns
];

Yii GroupGridView buttons

I use this extension(GroupGridView) in my yii project.
Now, I need help. How to add crud buttons for the grouped elements, to get something like this:
I don't really know is it right sollution.
I've created component GroupButtonColumn:
class GroupButtonColumn extends CButtonColumn {
public $name = 'buttons';
public $value; }
And my view
$this->widget('ext.groupGridView.GroupGridView', array(
'dataProvider' => $model->search(),
'summaryText' => false,
'filter' => $model,
'mergeColumns' => ['t_id', 'buttons'],
'columns' => [
[
'name' => 't_id',
'header' => '№',
'value' => 'CHtml::link($data->t_id, ["/task/view", "id"=>$data->t_id])',
'filter' => false,
'type' => 'raw'
],
/* ... */
[
'class' => 'GroupButtonColumn',
'template' => '{update}, {delete}',
'buttons' => [
'update' => [
'url' => '$this->grid->controller->createUrl("/task/update", array("id"=>$data->t_id))'
],
'delete' => [
'url' => '$this->grid->controller->createUrl("/task/delete", array("id"=>$data->t_id))'
]
]
],
],
));
Try this way:
'mergeColumns' => array('project_id', 'project_id')
/* ... */
'columns' => [
'project_id',
'id',
/* ... */
[
'name' => 'project_id',
'header' => 'Action',
'type'=>'raw',
'value' => function($data){
return 'Start';
}
],