Show only 5 item in yii2 List view

1k views Asked by At

In my yii2 application list view need to show only 5 product limit. This my View page

 <?=  ListView::widget( [
            'dataProvider' => $dataProvider,
            'itemView' => '_item',
            'summary' => '',
            ] ); ?> 

and this my controller

$searchModel = new HorseAdsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider, 
        ]);

this my Model search Function

public function search($params)
{  
    $query = HorseAds::find();  
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]); 
    $this->load($params); 
    if (!$this->validate()) {  
        return $dataProvider;
    } 
    // grid filtering conditions
    $query->andFilterWhere([
        'product_id' => $this->product_id,
        'producttype' => $this->producttype,
        'productname' => $this->productname, 
    ]); 
    return $dataProvider;
}

Please help me solve this, thanks

1

There are 1 answers

0
Sandeep Kapil On BEST ANSWER

Try somthing like this: idea is that set limit with find and set pagination to false

public function search($params)
{  
    $query = HorseAds::find()->limit(5);
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'pagination' => false
    ]); 
    $this->load($params); 
    if (!$this->validate()) {  
        return $dataProvider;
    } 
    // grid filtering conditions
    $query->andFilterWhere([
        'product_id' => $this->product_id,
        'producttype' => $this->producttype,
        'productname' => $this->productname, 
    ]); 
    return $dataProvider;
}