2013-11-04 57 views
0

我做了一個帶底部工具欄的extjs網格來創建分頁行爲,但它不工作,我不知道問題出在哪裏,我已經用本地數據完成了,但是當我使用數據庫時它不起作用,它會一次顯示所有數據。
下面的代碼:
尋呼不工作,Extjs網格

Ext.require([ 'Ext.data.*', 'Ext.grid.*' ]); 

Ext.onReady(function() { 
var categoriesStore = Ext.create('Ext.data.JsonStore', { 
    autoLoad: true, 
    pageSize : 10,  
    proxy: { 
      type: 'ajax', 
      url: 'GridDataBase/categories.php', 
      reader: { 
       type: 'json', 
       totalProperty: 'total', 
       root: 'categories', 
       idProperty: 'name' 
      }    
    }, 
    fields : ['id','name'] 
}); 

var panelOfData = Ext.create('Ext.grid.GridPanel',{ 
    store : categoriesStore, 
    columns : [ { 
     id : 'id', 
     header : "Id", 
     width : 20, 
     sortable : true, 
     dataIndex : 'id' 
    }, { 
     id : 'name', 
     header : "Category Name", 
     width : 75, 
     sortable : true, 
     dataIndex : 'name' 
    } ], 
    //stripeRows : true, 

     //paging bar on the bottom 
    bbar: Ext.create('Ext.PagingToolbar', { 
     store: categoriesStore, 
     displayInfo: true, 
     displayMsg: '{0} - {1} of {2}', 
     emptyMsg: "No categories to display" 
    }), 
    autoExpandColumn : 'name', 
    height : 350, 
    width : 600, 
    renderTo : 'grid-ex', 
    title : 'Categories' 
}); 

categoriesStore.loadPage(1); 
console.log(categoriesStore.count()); // the log here shows 0 
}); 


這裏的PHP文件:

function GetCategories() { 
    $query = "SELECT id, name FROM categorie"; 
    $categories = array("categories" => array()); 
    @mysql_connect("localhost","superuser","SS4") or die(); 
    mysql_select_db("dbz"); !mysql_error() or die(); 
    $result = mysql_query($query); 

    $num = mysql_num_rows($result); 
    $i = 0; 

    while ($row = mysql_fetch_assoc($result)) { 
     $categories["categories"][$i] = $row; 
     $i++; 
    } 

    return json_encode($categories); 
} 
echo GetCategories(); 
+0

你是否在'categories.php'中實現了分頁?尋呼的功能是將尋呼相關參數傳遞給你的php腳本,但服務器端的實現在你手中。 – Krzysztof

+0

頁面只提供一個json輸出,數據來自mysql數據庫。 –

回答

0

ExtJS的代碼發送一些額外的PARAMS實現對服務器啓動,限制了尋呼和這些PARAMS有在查詢中用於將記錄發送回商店的頁面大小限制。服務器響應還應該返回一個屬性,其中包含記錄總數(要分配給商店的totalProperty),該屬性將用於計算頁數。

store.load({ 
     params:{ 
      start:0, 
      limit: itemsPerPage 
     } 
    }); 

希望它有幫助!

+0

我不知道我應該把你的代碼放在哪裏,我必須在工具欄中實現一個處理程序,它將包含你提供的代碼? –

+0

加載商店時,必須傳遞開始和限制參數值,然後查詢應返回等於從開始限制參數值的記錄。最初開始將是0,並且限制是要顯示多少記錄(例如20)。所以,現在你的查詢應該有20個記錄,即0-19。同樣在下一頁開始參數的值將是20,並且限制參數將保持爲20.現在,您的查詢應返回20條記錄,即20-39等等。 – Rajinder

0

你還沒有在服務器端實現分頁。試試這個:

function GetCategories($start, $limit) { 
    $query = "SELECT id, name FROM categorie LIMIT $start, $limit"; 
    $categories = array("categories" => array()); 
    @mysql_connect("localhost","superuser","SS4") or die(); 
    mysql_select_db("dbz"); !mysql_error() or die(); 
    $result = mysql_query($query); 

    $num = mysql_num_rows($result); 
    $i = 0; 

    while ($row = mysql_fetch_assoc($result)) { 
     $categories["categories"][$i] = $row; 
     $i++; 
    } 

    return json_encode($categories); 
} 
$start = isset($_GET['start']) ? $_GET['start'] : 0; 
$limit = isset($_GET['limit']) ? $_GET['limit'] : 0; 

echo GetCategories($start, $limit);