R
R
Roman Nazaruk2017-06-16 01:55:50
Yii
Roman Nazaruk, 2017-06-16 01:55:50

Yii2 checkbox search?

Hello, I have a form with checkboxes and an input and a Model

The form
<form class="search-block clearfix" id="filter" method="get">
      <fieldset class="search-column">
        <legend>Инфраструктура:</legend>
        <ul class="type-checkbox">
          <li>
            <input type="checkbox" name="swimmingpool" id="swimmingpool-field" checked>
            <label for="swimmingpool-field">Бассейн</label>
          </li>
          <li>
            <input type="checkbox" name="parking" id="parking-field">
            <label for="parking-field">Парковка</label>
          </li>
          <li class="type-checkbox">
            <input type="checkbox" name="wifi" id="wifi-field">
            <label for="wifi-field">Wi-fi</label>
          </li>
        </ul>
      </fieldset>
      <fieldset class="search-column">
        <legend>Тип жилья:</legend>
        <ul class="type-checkbox">
          <li>
            <input type="checkbox" name="hotel" id="hotel-field" checked>
            <label for="hotel-field">Гостиница</label>
          </li>
          <li>
            <input type="checkbox" name="motel" id="motel-field" checked>
            <label for="motel-field">Мотель</label>
          </li>
          <li>
            <input type="checkbox" name="apart" id="apart-field" checked>
            <label for="apart-field">Аппартаменты</label>
          </li>
        </ul>
      </fieldset>
      <div class="search-column-range">
        <div class="range-title">Стоимость в сутки(Р):</div>
        <div class="price-filter">
          <div class="min-price">
            <input type="text" name="start-price" id="start-price-field">
            <label for="start-price-field">от 0</label>
          </div>
          <div class="max-price">
            <input type="text" name="final-price" id="final-price-field">
            <label for="final-price-field">до 3000</label>
          </div>
        </div>
        <button class="show-hotels" type="submit">Показать</button>
      </div>
    </form>

Model
<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Article;

/**
 * ArticleSearch represents the model behind the search form about `app\models\Article`.
 */
class HotelSearch extends Article
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'user_id'], 'integer'],
            [['hotelname', 'type', 'min_price', 'image'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        // $query = Article::find()->where([
        //   'type' => $this->type,
        //   'swimmingpool' => $this->swimmingpool,
        //   'parking' => $this->parking,
        //   'wifi' => $this->wifi,
        // ]);

        $query = Article::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
          'query' => $query,
            'pagination' => [
                'pageSize' => 5,
            ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'user_id' => $this->user_id,
        ]);

        $query->andFilterWhere(['like', 'hotelname', $this->hotelname])
            ->andFilterWhere(['like', 'type', $this->type])
            ->andFilterWhere(['like', 'min_price', $this->min_price])
            ->andFilterWhere(['like', 'image', $this->image])
            ->andFilterWhere(['like', 'swimmingpool', $this->swimmingpool])
            ->andFilterWhere(['like', 'parking', $this->parking])
            ->andFilterWhere(['like', 'wifi', $this->wifi]);

        return $dataProvider;
    }
}


Can you please tell me how to make a request so that when you select the necessary parameters in the checkbox and input and click the "Show" button, a list from the database is displayed?
or one request is not enough?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Timofeev, 2017-06-16
@webinar

Everything that happens in the browser is jquery or pure js. Yii2 (server side) has nothing to do with it. catch a click, send ajax, in response you give the server the list that you need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question