2011-11-23 3 views
3

복잡한 json 객체에서 레코드를 가져올 수 없지만 TPL을 사용할 때 데이터를 가져올 수 있습니다.Sencha Touch : 복잡한 JSON 객체에서 데이터를 가져 오는 방법

참조하십시오 아래 예제 코드 :

JSON :

{ 
"lists": [ 
    { 
     "title": "Groceries", 
     "id": "1", 
     "items": [ 
      { 
       "text": "contact solution - COUPON", 
       "listId": "1", 
       "id": "4", 
       "leaf": "true" 
      }, 
      { 
       "text": "Falafel (bulk)", 
       "listId": "1", 
       "id": "161", 
       "leaf": "true" 
      }, 
      { 
       "text": "brita filters", 
       "listId": "1", 
       "id": "166", 
       "leaf": "false" 
      } 
     ] 
    } 
] 

모델 :

내보기 파일에서
Ext.regModel("TopModel", 
{ 
    fields: [ 
     { name: 'title', type: 'string' }, 
     { name: 'id', type: 'string' }, 
    ], 
    hasMany: [ 
     { model: 'SubModel', name: 'items' } 
    ], 
    proxy: { 
    type: 'ajax', 
    url : 'test/lists.json', 
     actionMethods: { 
      create: 'POST', 
      destroy: 'POST', 
      read: 'POST', 
      update: 'POST' 
    }, 
    reader: { 
     type: 'json', 
     root: function(data) { 
        return data.lists || []; 
       } 
    } 
    } 
}); 

Ext.regModel("SubModel", 
{ 
    fields: [ 
      { name: 'text', type: 'string'}, 
     { name: 'listId', type: 'string'}, 
     { name: 'id', type: 'string'}, 
     { name: 'leaf', type: 'string'} 
    ] 
}); 

, 내가 가게로 정의한 이하.

this.stores = new Ext.data.Store({ 
      clearOnPageLoad: false, 
     autoLoad:true, 
      model:'TopModel', 
     getGroupString: function(record) { 
     return record.get('leaf'); 
      } 
}); 
}); 

나는이 아이 모델 항목을 참조로 record.get ('잎')에 대한 값을 검색 할 수 없습니다입니다. 인쇄를 시도 할 때 정의되지 않은 것으로 인쇄됩니다.

하위 속성에 액세스하는 방법은 무엇입니까? 여기서 'items'는 배열로 나열됩니다.

다음과 같이 목록을 사용하여 데이터를 표시하려고했습니다. 모든 레코드가 표시되지만 문제는 각 항목에 대해 별도의 목록 대신 하나의 전체 항목으로 선택된다는 것입니다.

var list = new Ext.List({ 
    cls: 'big-list', 
    grouped : true, 
    emptyText: '<div>No data found</div>', 
    itemTpl: ['<div><tpl for="items">', 
     '<div>', 
     '{id} {text}', 
     '</div>', 
     '</tpl></div>', 
     ], 
    store: this.stores, 
    listeners: { 
     itemtap: this.onListItemTap, 
     scope: this 
    } 
}); 

목록 항목을 개별 레코드로 표시하는 방법을 알려주세요.

+0

당신이 원하는 마십시오이 도움말에 대한 희망에 필요한 모든 데이터 모든 TopModel 또는 그 모눈에있는 모든 SubModel을 "leaf"로 그룹화하여 표시 하시겠습니까? this.stores는 코드에 따라 "TopModel"저장소이지만 SubModel을 표시하려고합니다. 감사합니다. – Philipp

+0

전체 템플릿이 귀하의 경우 그룹의 '항목'으로 표시됩니다. 또한 함수 대신 '목록'으로 루트를 설정할 수 있습니다. – Dawesi

답변

4

당신은 파서 데이터에서 JSON XML을 구문 분석 온라인 JSON 파서 (http://json.parser.online.fr/)를 사용하면 쉽게 seprate 객체와 배열하고 얻을 you..i 당신이

관련 문제