Try this..
first we need the data record create like this
PHP Code:
var myRecord = Ext.data.Record.create([{name:"my_field_from_database", type:'string'}]);
we dont need add anymore "mapping" hehe =) just have to be names equals from PHP MySQL
and now we need the greate JSON READER like this
PHP Code:
var myReader = new Ext.data.JsonReader({
root:'results',
totalProperty:'total',
id:'myId'
},myRecord);
and now we need the type of STORE, on this case im using the GrupingStore like this
PHP Code:
var myDataStore = new Ext.data.GroupingStore({
proxy:new Ext.data.HttpProxy({url:'myJSON.php'}),
baseParams:{task:"getMyClients"},
reader:myReader,
sortInfo:{field:'myField',direction:'ASC'},
remoteSort:true,
groupField:'myField', // this is very COOL AMAZING its simple fantastic
});
and now we need a column model like this
PHP Code:
var myColModel = new Ext.grid.columnModel([{dataIndex:'myField', header:'My Nice Field', align:'left', sortable:true, width:100}]);
If u re using a grid_filter plugin u will do like this
PHP Code:
var myFilters = new Ext.ux.grid.GridFilters({
local:false // hehe i will show u in PHP how to do it..
filters : [{dataIndex:'myField', type:'string'}] // types date, numeric, boolean, string
});
if u re using a Andrie.pPageSize plugin u will do like this
PHP Code:
var myPaging = new Ext.ux.Andrie.pPageSize({beforeText:'Show',afterText:results per page'});
and then the finally GRID PANEL so we do like this
PHP Code:
myGrid = new Ext.grid.GridPanel({
id:'my-grid',
autoSizeColumns:true,
colModel:myColModel,
frame:false,
loadMask:true,
plugins:myFilters,
selModel: new Ext.grid.RowSelectionModel({singleSelect:false}),
store:myDataStore,
bbar: new Ext.PagingToolbar({
plugins:[myPaging, myFilters],
pageSize:15,
store:myDataStore,
displayInfo:true,
displayMsg:"Show results of {0} - {1} / {2}"
emptyMsg:"No results found"
}),
view: new Ext.grid.GroupingView({
forceFit:true,
groupText:'{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Items" : "Item"]})'
})
});
myDataStore.load({params:{start:0,limit:15}});
Thats All..
Now the PHP server side
myJSON.php
so we will need 3 functions
1. function to Set Filters Post Values
2. function to Set Filter Comparison
3. function to Find All data in MySQL
PHP Code:
function set_filters_post_values(){
$this->filters = isset($_POST['filter']) ? $_POST['filter'] : $_GET['filter'];
$this->where = array();
if(count($this->filters) > 0){
$i=0;
while(isset($this->filters[$i]) && isset($this->filters[$i]['field'])){
$this->filter = $this->filters[$i];
$this->field = $this->filter['field'];
$this->type = $this->filter['data']['type'];
$this->value = $this->filter['data']['value'];
switch($this->type){
case "string":
$this->where[] = " AND ".$this->field." LIKE '%".$this->value."%' ";
break;
case "numeric":
$this->comparison = $this->set_filter_comparison($this->filter['data'][comparison]);
$this->where[] = " AND ".$this->field." ".$this->comparsion." '".$this->value."'";
break;
case "date":
$this->comparison = $this->set_filter_comparison($this->filter['data'][comparison]);
$this->where[] = " AND ".$this->field." ".$this->comparsion." '".$this->value."'";
break;
case "boolean":
$this->where[] = " AND ".$this->field." = ".$this->value; // value = 0 or value = 1
break;
case "null":
$this->where[] = $this->value ? $this->field . " IS NULL" : " IS NOT NULL";
break;
}
$i++;
}
}
}
function set_filter_comparison($comparison){
$comparison= strtolower($comparison);
switch($comparison){
case "gt":
$return = ">";
break;
case "lt":
$return = "<";
break;
case "gte":
$return = ">=";
break;
case "lte":
$return = "<=";
break;
case "eq":
$return = "=";
break;
case "neq":
$return = "!=";
break;
case "like":
$return = "LIKE";
break;
case "in":
$return = "IN";
break;
case "notin":
$return = "NOT IN";
break;
}
return $return;
}
function find_all(){
$this->limit = isset($_POST['limit']) ? $_POST['limit'] : $_GET['limit'];
$this->start = isset($_POST['start']) ? $_POST['start'] : $_GET['start'];
$this->dir = isset($_POST['dir']) ? $_POST['dir'] : $_GET['dir'];
$this->sort = isset($_POST['sort']) ? $_POST['sort'] : $_GET['sort'];
$this->set_filters_post_values();
$sql = "SELECT * FROM myTable WHERE 1 ";
if(!empty($this->where)){
if(count($this->where)>0){
for($i=0;$i<count($this->where);$i++)
$sql .= $this->where[$i];
}
}
$query_count = $this->db->query($sql);
$count = $this->db->num_rows($query_count);
$sql .= " ORDER BY ".$this->sort." ".$this->dir." LIMIT ".$this->start.",".$this->limit;
$query = $this->db->query($sql);
while($obj = $this->db->fetch_object($query))
$list[] = $obj;
$data = json_encode($list);
$cb = isset($_GET['callback']) ? $_GET['callback'] : '';
print $cb.'({"total":"'.$count.'","results":'.$data.'})';
}
NOTE : i have a DB(MySQL functions) class if u dont u have to do it in place of $this->db->query u do it mysql_query ....
$this->db->fetch_object u have to use mysql_fetch_objetc ...