Placing JSON response elements into a store- Ext JS

只谈情不闲聊 提交于 2019-12-25 04:28:17

问题


I'm wondering what the best way would be to load elements of a JSON response into a store.

What I have:

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 response

{
 "Foods":[
  {
     "Food":{
        "name":"Spaghetti",
        "id":"001",
        "attributes":[
           {
              "attrname":"Price",
              "attrvalue":"18.99"
           },
           {
              "attrname":"Description",
              "attrvalue":"Spaghetti is delicious, don't argue with me"
           },
           etc...

What I want:

(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...

How can I take the attributes from the json response and place them into a store my grid can use, as seen above?

Cheers!


回答1:


The data you are returning looks like a tree. A normal grid works with fixed columns. In short:

  • a grid uses a store
  • the Store uses a number of fields or a Model.
  • a grid has a number of predefined columns, each column references a store(model) field by means of a dataIndex.

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.

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

Good luck



来源:https://stackoverflow.com/questions/22052367/placing-json-response-elements-into-a-store-ext-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!