Retain Checkbox values in Yii gridview pagination

后端 未结 3 1519
故里飘歌
故里飘歌 2020-12-30 08:19

I have a gridview which contains a checkbox column and also uses pagination. When I check some checkboxes in the first page and navigate to the second page and check another

3条回答
  •  遥遥无期
    2020-12-30 08:43

    Also make this work on normal form submit:

    I wanted to add this as a comment on bool.dev's answer, but I do not have enough reputation to do that yet. So I had to put it in a separate answer.

    bool.dev, your answer is great and it works well, thanx.

    However, as intended, it only works when ajax calls update the gridview. I have the gridview forming part of a form, so I wanted it to also work on normal submission of the form, otherwise the checkboxes are not loaded again when there are other validation errors on the form.

    So, in ADDITION to what you did, I added hidden fields on my form e.g.:

    
    
    

    Then, before submitting the form, my sumbit button runs your getChecked() and getUncheckeds() functions and store their results in the hidden fields:

    if ($('#checkedBox1').length >0)   {$('[name=checkedBox1]').val(getChecked());}
    if ($('#uncheckedBox1').length >0) {$('[name=uncheckedBox1]').val(getUncheckeds());}
    

    In the controller, besides from checking for $_GET['checkedIds'], you also check for $_POST['checkedBox1'] and store its values to session in the same way you do for $_GET['checkedIds'], using the same session variable.

    Do the same with $_POST['uncheckedBox1'].

    That should work.

提交回复
热议问题