$range = explode(';',$this->range);
$query->orFilterWhere([
'and',
['>=','some_value_attribute',$range[0]],
['<=','some_value_attribute',$range[1]]
]);
class Offers extends ActiveRecord{
...
public function getOffers()
...
}
class ProductController extends Controller
{
public $searchModel;
public function init()
{
$this->searchModel = new ProductSearchModel();
}
}
<div class="container">
<?= $this->render('@app/views/product/_search', ['model' => Yii::$app->controller->searchModel]); ?>
</div>
<?=\app\widgets\offers\OffersWidget::widget([
'conditions'=>$conditions,
])?>
class OffersWidget extends Widget
{
public $conditions;
public function run()
{
$searchModel = new OffersSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
if($conditions = $this->conditions)
{
foreach($conditions as $item) {
$dataProvider->query->andWhere($item);
}
}
return $this->render('index', [
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
]);
}
public function init()
{
parent::init();
$view = Yii::$app->getView();
$this->registerAssets();
// $view->registerJs($this->getJs());
// $this->sourcePath = __DIR__ . "/assets";
}
public function registerAssets()
{
$view = $this->getView();
OffersAssets::register($view);
}
}
<div class="page-container ptb-60">
<div class="container">
<div class="row row-rl-10 row-tb-20">
<div class="page-sidebar col-md-4 col-xs-12">
<aside class="sidebar blog-sidebar">
<div class="row row-tb-10">
<div class="col-xs-12">
<div class="widget best-rated-deals panel pt-20 prl-20">
<h3 class="widget-title h-title">
<?=Yii::t('app','filter')?>
</h3>
<?=$this->render( '_search', [
'model' => $searchModel
])?>
</div>
</div>
</div>
</aside>
</div>
<div class="page-content col-xs-12 col-md-8">
<section class="section deals-area">
<header class="page-control panel ptb-15 prl-20 pos-r mb-30">
<ul class="list-control-view list-inline">
<li>Варианты отображения:</li>
<li>
<a href="<?=Url::current(['display'=>'list'])?>">
<i class="fa fa-bars"></i></a>
</li>
<li>
<a href="<?=Url::current(['display'=>'grid'])?>">
<i class="fa fa-th"></i></a>
</li>
</ul>
</header>
<div class="row row-masnory row-tb-20">
<?
Pjax::begin([
'id' => 'notes',
'timeout' => false,
'enablePushState' => false,
// 'clientOptions' => ['method' => 'POST']
]);?>
<?/*=ListView::widget([
'dataProvider' => $dataProvider,
// 'entityName' => 'offers',
'itemView' => '@app/modules/offers/views/frontend/default/_view',
'itemOptions' => [
'class' => 'col-sm-6 col-lg-4'
],
]);*/?>
<?php Pjax::end() ?>
</div>
</section>
</div>
</div>
</div>
</div>
<div class="offers-search">
<br/>
<?php yii\widgets\Pjax::begin(['id' => 'search_note']) ?>
<?php $form = ActiveForm::begin([
'options' => ['data-pjax' => true ],
'method' => 'post',
]); ?>
<div class="mb-20 p-lg-10">
<?=$form->field($model, 'range_price')->widget(Slider::classname(),[
'name' => 'range_price', // slider
'sliderColor' => Slider::TYPE_GREY,
'handleColor' => Slider::TYPE_DANGER,
'pluginOptions' => [
'orientation' => 'horizontal',
'handle' => 'round',
'min' => 0,
'max' => 10000,
'step' => 500,
'range' => true,
'tooltip'=>'always'
],
])->label(false);?>
</div>
<div class="mb-20 p-lg-10">
<?=$form->field($model, 'range_sale')->widget(Slider::classname(),[
'name' => 'range_sale', // slider
'sliderColor' => Slider::TYPE_GREY,
'handleColor' => Slider::TYPE_DANGER,
'pluginOptions' => [
'orientation' => 'horizontal',
'handle' => 'round',
//'handle'=>'triangle',
'min' => 0,
'max' => 100,
'step' => 5,
'range' => true,
'tooltip' => 'always',
'precision' => 2,
],
])->label(false);
?>
</div>
<label for=""><?=Yii::t('app','place')?></label>
<?=AutoCompleteWidget::widget([
'entity' => 'place',
'action' => Url::to(['/places/default/autocomplete']),
'className' => '@app/modules/places/models/Places',
'attribute'=>'place_id',
'model'=>$model,
'form'=>$form,
])?>
<?= $form->field($model, 'category_id')->dropDownList(CategoryOffers::getArrayList(), [
'prompt' => '--'.Yii::t('app','change').'--'
]) ?>
<?= $form->field($model, 'brand_id')->dropDownList(Brands::getArrayList(), [
'prompt' => '--'.Yii::t('app','change').'--'
]) ?>
<div class="form-group">
<?= Html::submitButton(Yii::t('app', Yii::t('app','search')), ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton(Yii::t('app', Yii::t('app','reset')), ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
<?phpyii\widgets\Pjax::end() ?>
</div>
'method' => 'post',
'method' => 'get',
$("#search_note").on("pjax:end", function() {
$.pjax.reload({
container:"#notes"
});
});
class OffersSearch extends Offers
{
public $id;
public $name;
public $date_from_start;
public $date_from_finish;
public $date_to_start;
public $date_to_finish;
public $brand_id;
public $place_id;
public $category_id;
public $range_price;
public $range_sale;
/*id
exist*/
public function rules()
{
return [
[['id', 'brand_id', 'category_id', 'place_id'] ,'integer'],
[['name'], 'string'],
[[ 'date_from_start', 'date_from_finish', 'date_to_start', 'date_to_finish'], 'safe'],
];
}
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
public function search($params)
{
$query = Offers::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$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;
}
$query->andFilterWhere([
'id' => $this->id,
'brand_id' => $this->brand_id,
]);
if($this->category_id) {
$category = CategoryOffers::findOne($this->category_id);
$query->andFilterWhere([
'category_id' => array_merge([$category->id], $category->children()->select('id')->column())
]);
}
if($this->place_id) {
$place = Places::findOne($this->place_id);
$query->andFilterWhere([
'place_id' => array_merge([$place->id], $place->children()->select('id')->column())
]);
}
if($this->range_price) {
$range = explode(';', $this->range_price);
$query->orFilterWhere([
'and',
['>=','price',$range[0]],
['<=','price',$range[1]]
]);
}
if($this->range_sale) {
$range = explode(';', $this->range_sale);
$query->orFilterWhere([
'and',
['>=','price',$range[0]],
['<=','price',$range[1]]
]);
}
$query
->andFilterWhere(['like', 'LOWER(name)', mb_strtolower($this->name)])
->andFilterWhere(['>=', 'date_from_stamp', $this->date_from_start ? strtotime($this->date_from_start . ' 00:00:00') : null])
->andFilterWhere(['<=', 'date_from_stamp', $this->date_from_finish ? strtotime($this->date_from_finish . ' 23:59:59') : null])
;
return $dataProvider;
}
}