I have created a gridview widget. I can select multiple rows using the class CheckboxColumn::class and I can update those rows using the action in my controller. The gridview, button and js look like this:
<?= Html::a(Yii::t('backend', 'Change selected'), ['update-selected'], [
'class' => 'btn btn-success mb-1',
'id' => 'updateSelection',
'data' => [
'toggle' => 'modal',
'target' => '#modal'
]
]) ?>
<?= GridView::widget([
'id' => 'prId',
'dataProvider' => $prSearch->search(Yii::$app->request->queryParams),
'filterModel' => $prSearch,
'filterSelector' => 'filter',
'columns' => [
[
'class' => CheckboxColumn::class,
'name' => 'CheckBoxSelection',
],
[
'attribute' => 'name',
],
[
'attribute' => 'visible'
]
],
]); ?>
<?php
$this->registerJS(<<<JS
var action = '';
$('#updateSelection').on('click', function(event) {
event.preventDefault();
submitForm();
});
function submitForm() {
var url = $('#updateSelection').attr('href');
var keys = $('#prId').yiiGridView('getSelectedRows');
console.log(keys);
$.ajax({
type: 'POST',
url: url,
data: {keylist: keys}
});
}
JS
, View::POS_READY);
And here we have the controller:
public function actionUpdateSelected()
{
$pr = new pR();
if (Yii::$app->request->isPost && Yii::$app->request->post('keylist')) {
$pr = pR::find()
->where(['IN', 'id', Yii::$app->request->post('keylist')])
->all();
foreach ($pR as $pr) {
$pr->visibility = 0;
$pr->save();
}
}
return 1
}
This makes sure all of the selected rows get a visibility of 0 (there are 4) Now I want the user to be able to open a modal with a dropdownlist so he can choose the visibility himself. So i added this to the controller:
$updateForm = new UpdateForm();
return $this->renderAjax('UpdateModal', [
'prId' => new pR(),
'updateForm' => $updateForm,
]);
I created the modal and the form:
//modal
<?php $form = ActiveForm::begin(['id' => 'updateSelectedVisibility', 'type' => ActiveForm::TYPE_HORIZONTAL]);?>
<div class="modal-body">
<?= $form->field($updateForm, 'id')->hiddenInput()->label(false); ?>
<?= $form->field($updateForm, 'visibility')->dropDownList([
0 => 'none',
1 => 'some',
2 => 'much'
3 => 'yes',
], ['id' => 'visibility-dropdown']) ?>
</div>
And i made a form ofcourse:
class updateForm extends Model
{
/**
* @var string
*/
public $id;
/**
* @var string
*/
public $visibility;
public function rules()
{
return [
[['id', 'visibility'], 'string'],
[['visibility'], 'required'],
];
}
public function attributeLabels()
{
return [
'id' => 'Id',
'visibility' => 'Visibility',
];
}
}
Now I've tried calling the keys array into the modal like this: $keys = Yii::$app->request->post('keylist');
although nothing seems to work. Does anybody know how to do this?
Aucun commentaire:
Enregistrer un commentaire