在yii2列表视图

时间:2016-12-23 07:22:24

标签: yii2 yii2-model

在我的yii2应用程序列表视图中,只需显示5个产品限制。 这是我的查看页面

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

这是我的控制器

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

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

这是我的模型搜索功能

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;
}

请帮我解决这个问题,谢谢

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:想法是使用find设置限制并将分页设置为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;
}