Good day! There is a project on Yii2, something like a catalog, I bring out product data via a GridView, only basic information is displayed when you go to the product page there is already detailed information there.

It is necessary to make a filter both by common fields and additional ones, but that these additional ones are not present in the GridView table, and the filter itself was.

Tell me, please, how to implement this.

  • Additional data, as I understand it, is related by connections, is there a one-to-many connection in the model itself? If yes then this is implemented in the Search model where the filtering logic is registered - modelfak
  • Yes, you are right, I do not understand how to display such a filter in the GridView, these additional input and select? - Rumato
  • @Rumato, go directly into the GridView and look for where it builds / displays filters. Create your class like MyGridView in which override the method that displays the filter row. That's all. Hint method is renderFilters (). - Makarenko_I_V

1 answer 1

Sorry - little information. Describe in more detail the model, database and which tables will be used.

In general, a typical approach - Gii suggests it by the way - you make its inherited version of a Model to a Model; Model Gii will generate you a model in which it will write: / ** * A Model represents your models\Модель . * / class ModelSearch extends Model

In the search model itself, you can arbitrarily add as many parameters as you like to the search method used in the grid. It is difficult to guess what kind of obd you have, but for example how it might look (from me). I can not say that all approaches here are the only true ones, but it shows the meaning and methods of use.

 public function search($params, $type = 1, $map = null, $paginationOff = null) { $query = Product::find(); if (isset($params['type']) && in_array($params['type'], array_flip(Product::getTypes()))) { $this->setAttribute('type', $params['type']); } else { $this->setAttribute('type', $type); } $this->load($params); // array load $type2 = @$params['ProductSearch']['type2']; $appPlatforms = @$params['ProductSearch']['appPlatforms']; $country = @$params['ProductSearch']['country_id']; $apps_count = @$params['ProductSearch']['apps_count']; $appCategory = @$params['ProductSearch']['appCategory']; $qty_staff = @$params['ProductSearch']['qty_staff']; $since_date = @$params['ProductSearch']['since_date']; $title = @$params['ProductSearch']['title']; $city = @$params['ProductSearch']['city_id']; $dataProvider = new ActiveDataProvider([ 'query' => $query, 'pagination' => [ 'pageSize' => self::PAGE_SIZE ], ]); if ($paginationOff) { $dataProvider->pagination = false; } $query->andFilterWhere([ 'uid' => $this->uid, 'type' => $this->type, ]); // Ключевое слово if ($title) { $query->andFilterWhere(['like', 'Product.title', $title]); } // Type2 parametr if ($type2) { if (is_array($type2)) { foreach ($type2 as $item) { $query->andFilterWhere(['Product.type2' => $item]); } } } // Страна if ($city) { if (is_array($city)) { $query->andFilterWhere(['in', 'Product.city_id', $city]); } } if ($country) { if (is_array($country)) { $query->andFilterWhere(['in', 'Product.country_id', $country]); } } // Колличество сотрудников if ($qty_staff) { if (is_array($qty_staff)) { $query->andFilterWhere(['in', 'Product.qty_staff', $qty_staff]); } } // Год основания if ($since_date) { if (is_array($since_date)) { $query->andFilterWhere(['in', 'Product.since_date', $since_date]); } } // технологии if (@$params['ProductSearch']['_tech']) { $query->leftJoin('technology', '`technology`.`Product_uid` = `Product`.`uid`'); $query->andFilterWhere(['`technology`.title' => $this->_tech]); } // Специализация if (@$params['ProductSearch']['_spec']) { $query->leftJoin('specialization', '`specialization`.`Product_uid` = `Product`.`uid`'); $query->andFilterWhere(['`specialization`.title' => $this->_spec]); } if ($this->type == 1) { // приложения $query->leftJoin('app_to_dev', '`app_to_dev`.`devId` = `Product`.`uid`'); $query->leftJoin('application', '`application`.`id` = `app_to_dev`.`appId`'); $query->leftJoin('app_developer', '`app_developer`.`devId` = `Product`.`uid`'); $query->leftJoin('application as applicationDev', '`applicationDev`.`developer_native_id` = `app_developer`.`native_id`'); $query->select(['Product.*, count(`app_developer`.id) + count(`app_to_dev`.id) as count, `application`.`store` as store, `applicationDev`.`store` as store2']); $query->groupBy(['`Product`.`uid`']); if ($appPlatforms) { if (is_array($appPlatforms)) { if (count($appPlatforms) == 1) { foreach ($appPlatforms as $item) { $query->andFilterWhere(['or', ['`application`.`store`' => $item], ['`applicationDev`.`store`' => $item]]); } } elseif (count($appPlatforms) > 1) { $query->andFilterWhere(['or', ['in', '`application`.`store`', $appPlatforms], ['in', '`applicationDev`.`store`', $appPlatforms]] ); } } } return $dataProvider; }