I need AjaxFiltering to work in my ListView in Yii2. It worked with Yii 1.
In Yii1 I used this example to get it working: http://www.yiiframework.com/wiki/185/clistview-ajax-filtering/
This is pretty important to get my project working. I’m not so familiar with Yii2 yet.
index.php
My JS Code:
[b]<?php
$script = <<< JS
$(’.hairFilter’).change(function(){
hair = $('.hairFilter').serialize();
if (hair == ''){
$.fn.yiiListView.update(
'ajaxListView',
{data: 'hair%5B%5D=1&hair%5B%5D=2&hair%5B%5D=3&hair%5B%5D=4'}
);
}
else {
$.fn.yiiListView.update(
'ajaxListView',
{data: hair}
);
}
});
JS;
$this->registerJs($script);
?>[/b]
Dropdown code for changing results:
[b] <div class="col-md-2">
<?= Html::activeDropDownList($searchModel, 'hair_id',
ArrayHelper::map(ProfileHair::find()->all(), 'id', 'name'),
['prompt'=>'Choose','class'=>'hairFilter']
); ?>
</div>[/b]
ListView code:
[b]<?=
ListView::widget([
'dataProvider' => $dataProvider,
'itemOptions' => ['class' => 'item'],
'itemView' => '_item_view',
'pager' => [
'class' => \kop\y2sp\ScrollPager::className(),
'id' => 'ajaxListView',
'negativeMargin' => '400',
'triggerText' => 'Load More',
'triggerOffset' => 3,
'noneLeftText' => '',
],
]);
?>[/b]
My Controller code snippet:
[b]public function actionIndex(array $hair = array()){
if( count( $hair ) > 0 ){
$criteria->addInCondition( 'hair', $hair );
}
.
.
.
}[/b]
When i change filter value i get following error: TypeError: $.fn.yiiListView is undefined
I think i’m pretty far from getting this working but any help would be appreciate!
If somebody could help me i would be very very glad. Thanks infront! Tutorial would be nice too!