F
F
future coder2020-07-13 13:24:46
Yii
future coder, 2020-07-13 13:24:46

Yii2, pjax, checkbox. Problem with checkboxes?

Friends, I got into a very strange situation for me). In general, I have a search bar and a filter with checkboxes (genres, authors). The form is wrapped in pjax. Gridview dataprovider, etc. I do not use, I write everything by hand. When changing the form or checkboxes, an ajax request for an action is made and everything changes without reloading. Everything works fine, the only thing is, when I select checkboxes from top to bottom in turn, it works fine, the data changes in accordance with the selected genre, and when I select one of the previous genres (i.e. not down, but up in the list), it no longer works, each time shows data from the bottom selected genre. It turns out that Yii::$app->request->post('genre') does not change when the previous genre is selected. I hope I explained at least a little bit.)

Here is the controller

class BookController extends Controller
{

    public function actionIndex()
    {
        if (Yii::$app->request->post()) {
            $dd = Yii::$app->request->post('genre');
        }

        $genres = Genre::find()->all();
        $authors = Authors::find()->with()->all();
        $books = Books::find()->all();

        $q = Yii::$app->request->post('q');
        $author = Yii::$app->request->post('author');
        $genre = Yii::$app->request->post('genre');

        if ($author && !$genre && !$q) {
            $books = Books::find()->joinWith(['booksAuthors'])->where(['books_authors.author_id' => $author])->all();
        }
        elseif (!$author && $genre && !$q) {
            $books = Books::find()->joinWith(['booksGenres'])->where(['books_genres.genre_id' => $genre])->all();
        }
        elseif (!$author && !$genre && $q) {
            $books = Books::find()->where(['like', 'name', $q])->all();
        }
        elseif ($author && !$genre && $q) {
            $books = Books::find()->joinWith(['booksAuthors'])->where(['like', 'name', $q])->andWhere(['books_authors.author_id' => $author])->all();
        }
        elseif (!$author && $genre && $q) {
            $books = Books::find()->joinWith(['booksGenres'])->where(['like', 'name', $q])->andWhere(['books_genres.genre_id' => $genre])->all();
        }
        elseif ($author && $genre && !$q) {
            $books = Books::find()->joinWith(['booksGenres', 'booksAuthors'])->where(['books_authors.author_id' => $author])->andWhere(['books_genres.genre_id' => $genre])->all();
        }
        elseif ($author && $genre && $q) {
            $books = Books::find()->joinWith(['booksGenres', 'booksAuthors'])->where(['like', 'name', $q])->andWhere(['books_authors.author_id' => $author])->andWhere(['books_genres.genre_id' => $genre])->all();
        }

//        var_dump(Books::find()->joinWith(['booksAuthors'])->where(['like', 'name', 'три'])->andWhere(['books_authors.author_id' => 444])->all());

//        if (Yii::$app->request->post('q')) {
//            $q = Yii::$app->request->post('q');
//            $books = Books::find()->where(['like', 'name', $q])->all();
//        }

        return $this->render('index', [
            'genres' => $genres,
            'books' => $books,
            'authors' => $authors,
            'dd' => $dd,
        ]);
    }
}


Here is the view
$this->registerJs(
    "
            $(document).on('change keyup', '.form-horizontal', function(){
                $('#login-form').closest('form').submit();
            });
        "
);

?>

<div class="row row-no-gutters">
    <div class="col-xs-12 col-md-8">
        <?php Pjax::begin(['linkSelector' => '#testpjax']); ?>
        <?php var_dump($dd); ?>
        <?php
        $form = ActiveForm::begin([
            'action' => 'index',
            'method' => 'post',
            'id' => 'login-form',
            'options' => ['class' => 'form-horizontal', 'data-pjax' => ''],
        ]) ?>

        <div class="form-group">
            <div>
                <input name="q" value="<?= Yii::$app->request->post('q') ?>" type="search" class="form-control"
                       placeholder="Найти ..." style="display: inline; width: 90%">
                <?= Html::submitButton('Найти', ['class' => 'btn btn-primary', 'id' => 'testpjax']) ?>
                <p class="toggle" style="margin-top: 10px; font-size: 18px" onclick="$('.col-md-4').toggle('display')">
                    Фильтр
                    <span class="glyphicon glyphicon-triangle-bottom""></span>
                </p>
                <div class="col-md-4" style="display: none">
                    <p>Жанры</p>
                    <?php foreach ($genres as $genre) : ?>
                        <?php $checked = ''; ?>
                        <?php if (Yii::$app->request->post('genre') == $genre->id) : ?>
                            <?php $checked = 'checked'; ?>
                            <p><input <?= $checked ?> type="checkbox" name="genre" value="<?= $genre->id ?>"> <?= $genre->name ?></p>
                        <?php else: ?>
                            <p><input type="checkbox" name="genre" value="<?= $genre->id ?>"> <?= $genre->name ?></p>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </div>
                <div class="col-md-4" style="display: none">
                    <p>Авторы</p>
                    <?php foreach ($authors as $author) : ?>
                        <?php $checked = ''; ?>
                        <?php if (Yii::$app->request->post('author') == $author->id) : ?>
                            <?php $checked = 'checked'; ?>
                        <?php endif; ?>
                        <p><input <?= $checked ?> type="checkbox" name="author" value="<?= $author->id ?>"> <?= $author->name ?></p>
                    <?php endforeach; ?>
                </div>
                <div class="col-md-4" style="display: none"></div>
            </div>
        </div>
        <?php ActiveForm::end() ?>

        <?php foreach ($books as $book) : ?>
            <h3><?php echo $book->name . '<br>'; ?></h3>
            <img src="../images/<?php echo $book->image ?>" alt="..." class="img-thumbnail" style="width: 200px">
            <footer>
                Автор:
                <?php foreach ($book->booksAuthors as $books_authors) : ?>
                    <cite title="Source Title">
                        <a href="#"><?php echo $books_authors->author->name; ?></a>
                    </cite>
                <?php endforeach; ?>
            </footer>
            <footer>
                Жанр:
                <?php foreach ($book->booksGenres as $books_genres) : ?>
                    <cite title="Source Title">
                        <a href="#"><?php echo $books_genres->genre->name; ?></a>
                    </cite>
                <?php endforeach; ?>
            </footer>
            <br>
            <small><?php echo $book->description ?></small>
            <hr>
        <?php endforeach; ?>
        <?php Pjax::end(); ?>
    </div>
</div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kim, 2020-07-13
@kimono

Gridview dataprovider, etc. I do not use, I write everything by hand.

Fine. Commendable. But your hands write worse than those who wrote GridView and DataProvider.
Ignoring your shitty code, the problem is that when you check checkboxes, they are sent in the exact order they appear on the page, not the order in which you click them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question