我是一個5天的Yii粉絲試圖學習這個偉大的框架的繩索。如果你能忍受我讀完我的問題並幫助我,我會非常感激。CGridview自定義數據提供不加載所有記錄來源(懶加載)
因此,在這裏,我正在檢索由在線市場的特定商家通過其Web服務銷售的產品的列表。當我這樣的查詢
api.marketplace.com?merchant_id=1234
網絡服務,我會回來它由數據
- 由商戶1234出售的產品數量的兩個主要部分(total_products在JSON響應)
- 銷售產品的清單(排列)(list_product)。此數組中的每個元素分別代表一個具有兩個字段的不同Product對象:分別爲product_id和product_name。
所以如果total_products = 934,就會有在陣列中934層的元件。 當我把它放在代碼,我得出的DataProvider看起來像這樣
class ProductDataProvider extends CDataProvider /* or should I extend other better class? */ {
public function __construct($config = array()) {
foreach($config as $key=>$value)
$this->$key=$value;
}
protected function fetchData() {
$request = "api.marketplace.com?merchant_id=1234";
$response = retrieveProductsAndProcessJSONResponseIntoAppropriateDataStructure($request);
$this->setTotalItemCount($response->total_products);
if(($pagination=$this->getPagination())!==false)
$pagination->setItemCount($this->getTotalItemCount());
return $response->list_products;
}
protected function calculateTotalItemCount() {
return $this->getTotalItemCount();
}
}
我的模型
class Product extends CModel {
public $product_id;
public $product_name;
public function display() {
return new ProductDataProvider(array(
'pagination'=>array(
'pageSize'=>10,
));
}
}
和我查看
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'product-grid',
'dataProvider'=>$model->display(),
'columns'=>array(
'product_id',
'product_name',
),
));
這與分頁和所有運作良好。但是,檢索所有934產品需要的時間太長。我們可以對Web服務執行此操作。
api.marketplace.com?merchant_id=1234&page_size=10&page_no=1
這隻會檢索前10條記錄。我們可以玩的查詢字符串變量page_no檢索中的10 total_products從網絡返回組產品後續頁面的服務仍然是934
所以我的問題是,我怎麼放這一點,以便ProductDataProvider
只能在任何時候檢索10個產品並將其加載到CGridView中? CGridView必須知道總共有934種產品可以實現分頁。用戶瀏覽到CGridView的其他頁面將觸發ProductDataProvider檢索與正在查看的頁碼相對應的下10條記錄。我希望我的冗長的問題是可以理解的。
研究數據提供者的分頁系統。它通常針對SQL分頁,但是通過自定義分頁類,您應該能夠按需要處理事情。 – acorncom