将JSON响应元素放入存储 - Ext JS [英] Placing JSON response elements into a store- Ext JS
问题描述
我想知道最好的方法是将JSON响应的元素加载到商店中。
I'm wondering what the best way would be to load elements of a JSON response into a store.
我有什么:
jQuery ajax请求
jQuery ajax request
var foodRequest = $.ajax({
url: "MyServlet",
type: "post",
cache: false,
dataType: "json",
data: JSON.stringify(searchquery),
error : function() {
alert ('ERROR!! Please re-enter the search criteria and try again.');
}
});
ajax响应
{
"Foods":[
{
"Food":{
"name":"Spaghetti",
"id":"001",
"attributes":[
{
"attrname":"Price",
"attrvalue":"18.99"
},
{
"attrname":"Description",
"attrvalue":"Spaghetti is delicious, don't argue with me"
},
etc...
我想要的:
(每个元素有4个属性)
(each element has 4 attributes)
var grid = Ext.define('Myapp.view.Grid' ,{
extend: 'Ext.grid.Panel',
id: 'mygrid',',
initComponent: function(columns) {
this.columns = [
{header: 'attrname', dataIndex: 'attrvalue', flex: 2 },
{header: 'attrname', dataIndex: 'attrvalue', flex: 2 },
{header: 'attrname', dataIndex: 'attrvalue', flex: 2 },
{header: 'attrname', dataIndex: 'attrvalue', flex: 2 },
etc...
如何从json响应中获取属性并将其放入我的网格可以使用的商店,如上所示?
How can I take the attributes from the json response and place them into a store my grid can use, as seen above?
干杯!
推荐答案
您正在返回的数据看起来像一棵树。普通网格使用固定列。简而言之:
The data you are returning looks like a tree. A normal grid works with fixed columns. In short:
- 网格使用商店
- 商店使用多个字段或模型。
- 网格具有多个预定义的列,每个列通过dataIndex引用store(model)字段。
现在,当商店加载数据(通过代理或通过手动转储记录)。它在内部创建一个记录。记录中可用的字段是您在商店中定义的字段或您在商店使用的模型上定义的字段。
Now when the store loads data (either through a proxy or by manually dumping records in it). it creates a record internally. The fields on the record that are available are the fields you defined on the store or the fields you defined on the model the store uses.
长篇小说,网格和商店的正常设置是固定数量的列和字段。在你的情况下,最好使用TreePanel和存储。看看这个treepanel的例子。
Long story short, the normal setup of a grid and store is with a fixed number of columns and fields. In your case it would probably be better to use a TreePanel and store. Look at the treepanel example for this.
http://docs.sencha.com/extjs/4.0.7/extjs-build/examples/tree/treegrid.html
祝你好运
这篇关于将JSON响应元素放入存储 - Ext JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!