I have a GridView with two columns: country_name and an actionColumn with a button called Save.
The Save button is a hiperlink to, for example:
http://localhost/countries/view?country_name=Argentina
I don't know how to set Argentina to country_name because my view doesn't have a model. I only have data from the column country_name.
GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'country_name',
[
'class' => 'yii\grid\ActionColumn',
'template' => '{save}',
'buttons' => [
'save' => function($model) {
return Html::a('Save', ['/countries/view', 'country_name' => $model['country_name']]); // Error: Illegal string offset 'country_name'
}
]
],
],
])
I am using this Yii2 help.
See ActionColumn:$buttons documentation - model is passed as second argument to button callback:
'buttons' => [
'save' => function($url, $model) {
return Html::a('Save', ['/countries/view', 'country_name' => $model['country_name']]);
}
]
Related
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'),
Below is the snapshot of my index page in Yii2 with Kartik Gridview widget;
Below is the gridview code for the same.
<?= GridView::widget([
'dataProvider' => $dataProvider,
//'filterModel' => $searchModel,
'columns' => [
[
'class' => 'kartik\grid\SerialColumn',
'contentOptions' => ['class' => 'kartik-sheet-style'],
'headerOptions' => ['class' => 'kartik-sheet-style'],
],
//'line_id',
'metal_name',
'item',
'unit_name',
'weight',
['attribute' => 'quantity',
'pageSummary' =>(true),
'value' => function ($model) {
if($model){
return $model->quantity;
}
}],
'rate_per_gram',
'making_charges',
[
'attribute' => 'total',
'format'=>'currency',
'pageSummary' =>(true),
'value' => function ($model) {
if($model){
return $model->total;
}
}
],
['class' => 'kartik\grid\ActionColumn'],
],
'responsive'=>true,
'hover'=>true,
'export'=>false,
'showPageSummary' => true,
]); ?>
I have tried lot of stuff to get plain number format with 2 decimals, but i could succeed in one.
How can I Format my Total column with Indian Currency, viz. Rs. 35670.00 or just 35670.00?
One way is to set currency directly:
[
'attribute' => 'total',
'format' => ['currency', 'INR'],
'pageSummary' => true,
],
PS. You don't need to set value key unless you want to replace it with something else in certain case.
I have a grid that i would like to filter but the filter boxes are not enabled in the column
This is the grid row
[
'attribute'=>' category',
'value'=>function($model){
switch ($model->category){
case 1:{
return "Normal truck";
}
case 2:{
return "Bulker truck";
}
}
},
'filter'=>array(1=>"Normal truck",2=>"Bulker truck"), //the dropdown filter
],
And this is my grid configuration
echo DynaGrid::widget([
'columns' => $columns,
'showPersonalize' => true,
//'allowThemeSetting'=> false,
'options' => ['id' => 'company_truck_grid'],
'gridOptions' => [
'options' => ['id' => 'company_grid_trcl_inside'],
'dataProvider' => $dataProvider,
'filterModel' => $searchModel, //filter is set
............
]
]);
This is whats being displayed
As you can see the category column has no dropdown filter and is disabled.. Where am i wrong..
How can I use in GridView delete selected object,in Yii 2 Framework such as following image:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'id',
'name',
'created_at:datetime',
// ...
],
]) ?>
Add checkbox action column in gridView like
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
[
'class' => 'yii\grid\CheckboxColumn', 'checkboxOptions' => function($data) {
return ['value' => $data->id];
},
],
'id',
'name',
'created_at:datetime',
// ...
],
]) ?>
And Now Access the selected id in your controller like
class YourController extends Controller
{
public function actionHear()
{
if(isset($_REQUEST['selection']))
{
".........Your Code Hear.........."
}
}
}
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),
}
],