Hi,
I’m trying to add 3 button after my GridView which contain a checkbox column, the delete one is working fine but the edit one is not. Any idea why ?
This the JavaScript code that I use.
<script>
$(document).ready(
function(){
$('.sup').on('click',function(){
var keys = $('#grid').yiiGridView('getSelectedRows');
$.post({
url: 'index.php?r=/agence/delete&id='+keys,});
});
$('.edi').on('click',function(){
var keys = $('#grid').yiiGridView('getSelectedRows');
$.post({
url: 'index.php?r=/agence/update&id='+keys,});
});
}
)
</script>
Do you have some javascript errors? Is ".edi" click handler called?
I don’t have any error.
I added a ‘console.log(3);’ inside that function and it show up fine.
Is update action method accepting an array of id? Because keys could be an array, is it right?
It accept one key.
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->agence_id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
I modified my code like this :
$('.edi').on('click',function()
{
var key = $('#grid').yiiGridView('getSelectedRows');
window.location = "http://localhost/index.php?r=agence%2Fupdate&id=" + key;
}
);
Now it works but I don’t know if this is good practice or if there is a better way to do it. Any suggestion would be very welcome.