我正在使用yii2框架,我是实习生。 我想问你我在index.php上使用Gridview :: widget,现在我有一个按钮用于多删除和多状态更改,并且它与jquery ajax一起工作正常,但我想传递选中的复选框ID直接控制器/动作而不使用jquery。 谁能帮帮我吗? 如果有人想通过jquery ajax,那么我已经附加了你可以使用的代码,但我想要没有jquery-ajax。 请帮忙。 谢谢, Hunny Malhotra。
my controller code:
//multiple delete action
public function actionDeletem()
{
$id = $_POST['id'];
$model = users::find()->where(['in','user_id',$id])->all();
foreach($model as $data){
$data->deleted = 'Y';
$data->save();
}
echo "Deleted";
}
//multiple change status action
public function actionStatusm()
{
$id = $_POST['id'];
$model = users::find()->where(['in','user_id',$id])->all();
foreach($model as $data) {
if($data->status == 1){
$data->status = 0;
$data->save();
}else if($data->status == 0){
$data->status = 1;
$data->save();
}
}
echo "Status Changed Successfully";
}
below is my index.php code with jqoery code init:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\grid\GridView;
use yii\widgets\Pjax;
use yii\helpers\Url;
/* @var $this yii\web\View */
/* @var $searchModel backend\models\UsersSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Manage Users';
$this->params['breadcrumbs'][] = 'Users';
?>
<div class="users-index">
<h1><?= Html::encode('Users') ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<?php
$user = Yii::$app->user->identity->user_type;
if($user == 1)
{ ?>
<p>
<?= Html::a('Create User', ['create'], ['class' => 'btn btn-success']) ?>
<input type="button" id="delete" class="btn btn-danger" value='Delete'>
<input type="button" id="status" class="btn btn-primary" value='Status'>
<?= Html::a('Export to Excel', ['excel'], ['class' => 'btn btn-info']) ?>
<?= Html::a('Import File', ['import'], ['class' => 'btn btn-success']) ?>
<?= Html::a('Test', ['test'], ['class' => 'btn btn-success']) ?>
</p>
<?php } ?>
<?php Pjax::begin(['id'=>'users']); ?> <?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
[
'class' => 'yii\grid\CheckboxColumn',
'visible' => $user == 1 ? true : false,
'checkboxOptions' => function($model, $key, $index, $widget) {
return ['value' => $model['user_id'] ];
}
],
'user_id',
'first_name',
'last_name',
'email:email',
'mobile',
[
'attribute' => 'status',
'label' => 'Status',
'filter' => Html::activeDropDownList($searchModel, 'status', [1=>'Active',0=>'Inactive'], ['prompt' => 'All']),
'value' => function($model) {
return $model->status == 1 ? 'Active' : 'Inactive';
},
],
[
'attribute' => 'created_on',
'value' => 'created_on',
'filter' => \yii\jui\DatePicker::widget(['model'=>$searchModel,'attribute'=>'created_on','language'=>'en',
'dateFormat' => 'yyyy-MM-dd',]),
'format' => 'html'
],
'created_by',
[
'class' => 'yii\grid\ActionColumn',
'visible' => $user == 1 ? true : false,
'header' => 'Action',
'headerOptions' => ['style' => 'color:#3c8dbc'],
],
],
]);?>
<?php Pjax::end(); ?></div>
<script>
$(document).ready(function(){
$('#delete').click(function(){
var id = $('#w0').yiiGridView('getSelectedRows');
console.log(id);
if(id<1) {
alert("No Rows are Selected");
}else{
if (confirm('Are you sure you want to delete?')) {
$.ajax({
url: "<?= Yii::$app->urlManager->createUrl(['users/deletem']); ?>",
type: "POST",
data: {id: id},
success: function(data){
alert(data);
$.pjax.reload({container:'#users'});
}
});
}
}
});
$('#status').click(function(){
var id = $('#w0').yiiGridView('getSelectedRows');
if(id<1) {
alert("No Rows are Selected");
}else{
$.ajax({
url: "<?= Yii::$app->urlManager->createUrl(['users/statusm']); ?>",
type: "POST",
data: {id: id},
success: function(data){
alert(data);
$.pjax.reload({container:'#users'});
}
});
}
});
});
</script>