在這裏顯示的名稱/值對的一個Ext.grid.Panel是商店:如何從Ext.data.Store
Ext.define('LFinanceCRM.store.Prospect', {
extend : 'Ext.data.Store',
buffered : false,
autoLoad : true,
remoteFilter : true,
storeId : 'prospect-store',
proxy : {
type : 'ajax',
url : 'services/getProspect.php',
filterParam : undefined,
limitParam : undefined,
startParam : undefined,
pageParam : undefined,
idParam : 'id',
reader : {
type : 'json',
root : 'prospect'
}
},
fields : [
{ name : 'id' },
{ name : 'value' }
],
constructor : function(){
this.callParent(arguments);
console.log('new ' + this.self.getName());
}
});
這裏是PHP代碼:
<?php
include_once 'db.php';
header("Content-type: application/json; charset=utf-8");
$id = @mysql_real_escape_string($_GET['id']);
$link = db_open();
$query = "SELECT name, value FROM Pairs WHERE id = '$id'";
$result = @mysql_query($query, $link);
$pairs = array();
if($result) {
while($row = mysql_fetch_assoc($result)) {
$item = Array();
$item['id' ] = $row['name' ];
$item['value'] = $row['value'];
$pairs[] = $item;
}
}
$response = array('prospect' => $pairs);
print json_encode($response);
@mysql_free_result($result);
@mysql_close($link);
?>
這裏是從PHP接收的JSON:
{prospect:[
{id:'aaa',value:'vvvv'},
{id:'bbb',value:'vvvv'},
...
{id:'yyy',value:'vvvv'},
{id:'zzz',value:'vvvv'},
}]
這裏是視圖:
Ext.define('LFinanceCRM.view.RawDataView', {
extend : 'Ext.grid.Panel',
requires :[],
alias : 'widget.raw-data-view',
autoScroll : true,
title : 'Données brutes',
columnLines : true,
viewConfig : { stripeRows : true },
store : Ext.data.StoreManager.lookup('prospect-store'),
columns : [{
text : 'Nom',
dataIndex : 'name',
sortable : false,
width : '29%'
},{
text : 'Valeur',
dataIndex : 'value',
sortable : true,
width : '70%'
}],
constructor : function() {
this.callParent(arguments);
console.log('new ' + this.self.getName());
}
});
我用煎茶應用構建工具支持MVC模式,這裏是控制器:
Ext.define('LFinanceCRM.controller.Main', {
extend : 'Ext.app.Controller',
id : 'theController',
onNonLuesSelectionChanged : function(panel, selected, eOpts) {
console.log('onNonLuesSelectionChanged: ' + selected[0].data.id);
this.getStore('Prospect').load({
id : selected[0].data.id,
callback : function(records, operation, success) {
var pairs = [];
for(var i = 0; i < records.length; ++i) {
pairs.push(records[i].data);
}
Ext.ComponentQuery.query('client-view')[0].getForm().setValues(pairs);
Ext.ComponentQuery.query('immo-view' )[0].getForm().setValues(pairs);
}
});
},
onSavePairs : function() {
console.log('onSavePairs');
},
...
onMail : function() {
console.log('onMail');
},
...
stores : ['Prospect'],
constructor : function(){
this.callParent(arguments);
console.log('new ' + this.self.getName());
this.control({
'#ProspectsTableNonLues' : { selectionchange : this.onNonLuesSelectionChanged },
...
'#savePairsButton' : { click : this.onSavePairs },
...
'#mail' : { click : this.onMail },
});
}
});
還未有任何顯示!
我的問題是:如何將商店中的數據轉換爲供應視圖?
lookup vs create:我已經在另一個視圖中使用了這個商店。我不想有多個請求到服務器。 – Aubin
感謝'name'和'id'之間的錯誤。 – Aubin