Answer the question
In order to leave comments, you need to log in
Checkbox as gridview filter?
Hello fellow friends. I haven't tormented you with stupid questions for a long time, but the time has come.
In the GridView header, I add content to display records only with a label in the base is_own = 1|0
[
'content' => '
<div class="checkbox">
<label>
<input type="checkbox" id="isownonly" name="SetsSearch[is_own]" class="grid-filter" ' . ($searchModel->is_own == 'on' ? 'checked' : '') . '>
Только наш КЦ
</label>
</div>',
],
Answer the question
In order to leave comments, you need to log in
the issue is solved by such a crutch as mocking a checkbox with a hidden field:
php
[
'content' => '
<div class="checkbox">
<label>
<input type="checkbox" id="isownonly" class="" ' . ($searchModel->is_own == '1' ? 'checked' : '') . '>
Только наши
</label>
</div>
<input type="hidden" class="grid-filter" name="SetsSearch[is_own]" value="' . ($searchModel->is_own == '1' ? '1' : '0') . '">
',
],
$(document).on('change', '#isownonly', function (e) {
$('[name*=is_own]').val(
$(this).is(':checked') ? 1 : 0
).trigger('change');
});
I have this situation (yii2 + GridView + Pjax):
Initially, it was like you. I applied your solution and it worked.
Another problem appeared: if you go to another pagination page (for example, &page=12), and then filter with any filter, then the _GET parameter remains, and after filtering it goes straight to the last page. It turned out &page12, and the paginator showed the last page after filtering.
Both problems were solved by adding it to the GridView
. At the same time, you need to specify 'uncheck' => null if you use Html::activeCheckbox, since _GET parameters in the address bar are doubled when the checkbox is checked.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question