M
M
My joy2018-10-08 19:26:35
Yii
My joy, 2018-10-08 19:26:35

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>',
],

5bbb849bf1b93604858964.png
So this checkbox is obtained inside the pjax container, and I don’t know if this is related to this or not, but after installation it no longer disappears and always remains checked, even if you try to remove it, and always shows the is_own = 1 entries. how to fix it so that it switches ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
My joy, 2018-10-08
@t-alexashka

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') . '">
        ',
],

js
$(document).on('change', '#isownonly', function (e) {
  $('[name*=is_own]').val(
    $(this).is(':checked') ? 1 : 0
  ).trigger('change');
});

If anyone knows a more elegant solution to make the checkbox filter work with pjax - let me know, it will be useful to everyone.

N
Nikolai, 2019-11-07
@kaliyan

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 question

Ask a Question

731 491 924 answers to any question