簡體   English   中英

如何在Sencha Touch列表中顯示嵌套項目

[英]How to show nested items in Sencha Touch list

我有以下json數據。

{
"pendingTasksVOs" : [{
        "groupName" : "LAST_MONTH",
        "documents" : [{
                "description" : "kvk1",
                "uploadDate" : "1-12-2012"
            }
        ]
    }, {
        "groupName" : "OLDER",
        "documents" : [{
                "description" : "kvk2",
                "uploadDate" : "1-11-2012"
            }, {
                "description" : "kvk3",
                "uploadDate" : "1-10-2012"
            }, {
                "description" : "kvk4",
                "uploadDate" : "1-01-2012"
            }
        ]
    }
]
}

我想在按GroupName分組的Sencha Touch列表中顯示它。

我希望列表采用以下格式:

GoupName : Group1
-----------------------------
Description11 , UploadDate11   This should be separate clickable list row
-----------------------------
Description12 , UploadDate12   This should be separate clickable list row
----------------------------- 
Description13 , UploadDate13   This should be separate clickable list row
-----------------------------
******************************
GoupName : Group2
-----------------------------
Description21 , UploadDate21   This should be separate clickable list row
-----------------------------
Description22 , UploadDate22   This should be separate clickable list row
-----------------------------
******************************

我正在嘗試使用Sencha觸摸列表組件。 但我沒有得到上述要求的清單

//This is my store 
var store = Ext.create('Ext.data.Store', {
        model: 'Demo.model.DocumentList',
        rootProperty: 'pendingTasksVOs',                                          
        autoLoad: true,                                                                             
        grouper: {
        groupFn: function(record) {
            if (record && record.data.title) {
                    return record.get('groupName');
                } else {
                return '';
                }
            }
        }           
    });

//template for list item
var listTemplate = new Ext.XTemplate(                                           
        '<tpl for=".">',
        '{groupName}</br>',
        '<ul>',
        '<tpl for="documents">',
        '{description}  {uploadDate} </br>',
        '</tpl>',
        '</ul>',
        '</tpl>'                                            
        );


// Initialize the main view
Ext.getCmp("pendingTasksListId").add(Ext.create('Ext.List', {
  fullscreen: true,
  itemTpl:  listTemplate,     
  store: store,
  groupField:'description',
  grouped: true
}));

//DocumentListModel is defined under /app/model/ 
Ext.define('Demo.model.DocumentList', {
extend: 'Ext.data.Model',
requires : ['Demo.model.Doc'],
config: {
     fields: ['groupName', 'documents'],
     hasMany : {

        model : "Demo.model.Doc", 
        associationKey: 'documents'
    },
    proxy: {
        type: 'rest',
        url : "/getPendingTasks",
        reader: {
            type: 'json',           
            rootProperty : 'pendingTasksVOs'
        }
    }       
}
}

//Document Model is defined under /app/model/ 
Ext.define('Demo.model.Doc', {
extend: 'Ext.data.Model',    
config: {
     fields: ['description', 'uploadDate'],
     belongsTo: "Demo.model.DocumentList"
}   
}); 
});

我能夠獲得如下列表項:

GroupName,
Description11,UploadDate11 
Description12,UploadDate12

但以上全部內容都是可點擊的。 我希望每個“描述和上載日期”對都可以單擊。

誰能指導我如何達到上述要求?

我為我的問題找到了解決方案。

在/ app / reader /文件夾下創建以下自定義json閱讀器作為CustomReader.js

Ext.define('Demo.reader.CustomReader', {
extend: 'Ext.data.reader.Json',
alias: 'reader.customReader',
getData: function(data) {       // overriding
    data = this.callParent(arguments);
    var responseString = Ext.encode(data);
    var json = JSON.parse(responseString);
    var result = [];
    Ext.each(json.pendingTasksVOs, function(entry) {

        var groupName = entry.groupName;
        Ext.each(entry.documents || [], function(document) {          
            result.push({
                description : document.description,
                uploadDate: document.uploadDate,
                groupName: groupName
            });
        });
    });
    return result;
}
});

Doc模型應在Doc.js中的/ app / model中創建

Ext.define('Demo.model.Doc', {
extend: 'Ext.data.Model',    
config: {
     fields: ['description','uploadDate','groupName']       
}
});

可以在/ app / store文件夾中創建商店,商店應使用此自定義閱讀器,如下所示:

Ext.define("Demo.store.DocumentStore", {
extend:'Ext.data.Store',
requires:['Demo.model.Doc' , 'Ext.data.proxy.Rest', 'Demo.reader.CustomReader'],
id:'DocumentStore1',
config:{
    model:'Demo.model.Doc',
    autoLoad:true,
    grouper:{
        groupFn:function (record) {
            if (record && record.data.groupName) {
                return record.get('groupName');
            } else {
                return '';
            }
        }
    },
    proxy:{
        type:'rest',
        url:"/getPendingTasks",
        reader:{
            type:'customReader'     //This is our custom reader         
        }
    }
}
});

//最后可以如下創建列表

Ext.create('Demo.store.DocumentStore');
// Initialize the main view
Ext.getCmp("pendingTasksListId").add(Ext.create('Ext.List', {
fullscreen:true,
itemTpl:'<div class="contact">{description} {uploadDate} {groupName}  </div>',
store: 'DocumentStore1',
grouped:true
}));    

上面列出了我從接收到的有問題的json中獲取每個{description} {uploadDate}對的單獨行的列表

嘗試這個,

itemTpl: [
'<div>GroupName : {groupName}</div>',
'<div>',
'<ul>',
'<tpl for="documents">',
'<li>{description} , {uploadDate}</li>',
'</tpl>',
'</ul>',
'</div>',
].join('')

或這個,

itemTpl: [
'<div>GroupName : {groupName}</div>',
'<div>',
'<ul>',
'<tpl for="documents">',
'<li><div onclick="whateverYouWantToDo()">{description} , {uploadDate}</div></li>',
'</tpl>',
'</ul>',
'</div>',
].join('')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM