yii2 Deleting multiple Rows in GridView using Checkbox - yii2

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.........."
}
}
}

Related

my export in yii2 using kartik-v/yii2-export not working

i am trying to use kartik export and its not working for me. In config file I have added following code:
'modules' => [
'gridview' => [
'class' => '\kartik\grid\Module',
],
],
In Composer i have added folowing code
"kartik-v/yii2-export": "#dev",
"kartik-v/yii2-mpdf":"#dev",
"kartik-v/yii2-grid": "#dev"
My view code is like this:
<?php
use yii\helpers\Html;
use kartik\grid\GridView;
use yii\bootstrap\Tabs;
use kartik\export\ExportMenu;
use yii\widgets\Pjax;
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="general-info-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php
<p>
<?= Html::a('create', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?php
$gridColumns = [
'sfcl_name',
[
'attribute'=> 'org_type',
'value' => 'orgType.cv_lbl'
],
];
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns
]);
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'pjax'=>true,
'columns' => [
'sfcl_name',
'phone',
[
'attribute'=>'regd_dt_ad',
'format'=>['date', 'php:Y-M-d'],
'xlFormat'=>'mmm\-dd\, yyyy', // different date format
'width'=>'100px'
],
[
'attribute'=> 'org_type',
'value' => 'orgType.cv_lbl'
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
My controller code is:
My controller code to call this view : public function actionIndex()
{
$searchModel = new SfclGeneralSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
On click of export menu nothing is happening.In tutorial i saw there were options for exporting to pdf ,html,csv,json,text.such options doesnot appers in my case.Is the css not working or what?
you run "composer update" command?.
also in your web.php
'modules' => [
'gridview' => [
'class' => '\kartik\grid\Module',
],
],
add the property downloadAction, like this:
'modules' => [
'gridview' => [
'class' => '\kartik\grid\Module',
'downloadAction' => 'gridview/export/download',
]
],

Displaying yii2 filterboxes fails in a custom function column

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..

Undefined variable: gridColumns in karik gridview:yii2

I'm trying to use kartik gridview and got the following error. -
Undefined variable: gridColumns
I've checked - Yii2: Kartik Gridview sum of a column in footer. But didn't find the issue in my code.
Code of index.php -
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'showPageSummary' => true,
'columns' => [
['class' => 'kartik\grid\SerialColumn'],
[
'attribute'=>'s_period',
'filter'=>ArrayHelper::map(Salary::find()->asArray()->all(), 's_period', 's_period'),
],
's_empid',
's_empname',
[
'attribute'=>'s_epf',
'pageSummary'=>true
],
//['class' => 'yii\grid\ActionColumn'],
],
]); ?>
if you assign manually the columns you don't need a var iwth columns specification so remove it
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
//'columns' => $gridColumns,
'showPageSummary' => true,
'columns' => [
['class' => 'kartik\grid\SerialColumn'],
[
'attribute'=>'s_period',
'filter'=>ArrayHelper::map(Salary::find()->asArray()->all(), 's_period', 's_period'),
],
's_empid',
's_empname',
[
'attribute'=>'s_epf',
'pageSummary'=>true
],
//['class' => 'yii\grid\ActionColumn'],
],
]); ?>

Why is the button page on my gridview not shown?

i have model from "feedbackmodel" :
public function search($params)
{
$query = Feedback::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort'=> ['defaultOrder' => ['TANGGAL'=>SORT_DESC]],
'pagination' => array('pageSize' => 10),
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'ID_KOMENTAR' => $this->ID_KOMENTAR,
'id' => $this->id,
'TANGGAL' => $this->TANGGAL,
]);
$query->andFilterWhere(['like', 'KOMENTAR', $this->KOMENTAR]);
return $dataProvider;
}
and the gridview code like this:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'layout'=>"{items}",
'tableOptions' => ['class' => 'table table-bordered table-hover'],
'showFooter'=>false,
'showHeader' => false,
'pager' => [
'firstPageLabel' => 'First',
'lastPageLabel' => 'Last',
],
'columns' => [
[ 'attribute' => 'iduser.photo',
'format' => 'html',
'value'=> function($data) { return Html::img($data->imageurl) . " <p class='feedback-username'>" . $data->username . "</p>"; },
'contentOptions'=>['style'=>'max-width: 10px; max-height: 10px'],
],
[ 'attribute' => 'KOMENTAR',
'format' => 'raw',
'value' => function($model) { return $model->KOMENTAR ."<br><p class='feedback-date'>". $model->TANGGAL ."</p>";},
],
[ 'class' => 'yii\grid\ActionColumn',
'contentOptions'=>['style'=>'width: 5px;'],
'template' => '{update} {delete}'
],
],
]); ?>
But why the page button is not shown, its limited but i can see the other data bcos the i cant find the button next or prev to the other page
I test your code
if I remove the layout=>"{items}", the page buttons are show otherwise not (i think your layout assignment mean show only the item and not the pager) try simply comment it
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
//'layout'=>"{items}",
'tableOptions' => ['class' => 'table table-bordered table-hover'],
'showFooter'=>false,
'showHeader' => false,
'pager' => [
'firstPageLabel' => 'First',
'lastPageLabel' => 'Last',
],
..........

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