数据视图中包含3列的垂直列表视图

时间:2013-10-18 08:24:19

标签: extjs extjs4.2

我正在尝试创建一个数据视图,使其具有三个表格列,并按此顺序显示项目:

  • 1,7,13
  • 2,8,14
  • 3,9,15
  • 4,10,16
  • 5,11
  • 6,12

我的商店包含16个如上所列的数据模型(上面的数字是模型中的'name'字段)。我试图使用带有成员函数的xtemplate来完成此任务,该成员函数返回任何给定行的第二列和第三列的值。我每列最多允许6个项目。代码是

Ext.define('TestExtJs.view.ColumnView', {
extend: 'Ext.view.View',
alias: 'widget.columnview',

itemSelector: 'div.thumb-wrap',
emptyText: 'No items to show',
config: {
    store: null,
    tpl: null,
},
initComponent: function() {

    var itemData = [
        {id: 1, name: '1'},
        {id: 2, name: '2'},
        {id: 3, name: '3'},
        {id: 4, name: '4'},
        {id: 5, name: '5'},
        {id: 6, name: '6'},
        {id: 7, name: '7'},
        {id: 8, name: '8'},
        {id: 9, name: '9'},
        {id: 10, name: '10'},
        {id: 11, name: '11'},
        {id: 12, name: '12'},
        {id: 13, name: '13'},
        {id: 14, name: '14'},
        {id: 15, name: '15'},
        {id: 16, name: '16'}
    ];

    var theStore = new Ext.data.Store({
        //fields:['id', 'name'],
        model: 'TestExtJs.model.Item',
        data: itemData
        //requires: ['TestExtJs.model.Item']
    });
    this.store = theStore;
    /*var columntpl = new Ext.XTemplate(
            '<tpl for=".">',
            '{name}',
            '</tpl>'
            );*/
    var columntpl = new Ext.XTemplate(
'<tpl for=".">',
    '<tpl if="(xindex - 1) &lt; (xcount / 3)">',
    '<div style="width:100%;">',

        '<div style="float: left;width: 33%" class="thumb-wrap-first">',
        '{name} ',
        '</div>',

        '<tpl if="this.getVal(xindex, 2)">',
            '<div style="float: left;width: 34%" class="thumb-wrap-second">',
                '{[this.getVal(xindex, 2)]}',
            '</div>',
        '</tpl>',

        '<tpl if="this.getVal(xindex, 2)">',
            '<div style="float: left;width: 33%" class="thumb-wrap-third">',
                '{[this.getVal(xindex, 3)]}',
            '</div>',
        '</tpl>',

    '</div>',
    '</tpl>',
'</tpl>',
{
    getVal : function(xin, cin){
        if(cin === 1){

        }
        if(cin === 2){
            var v = this.getStore.getAt(xin + 6 - 1);
            if(v){
                return v.get('name');
            }
            return null;
        }
        if(cin === 3){
            var v = this.getStore.getAt(xin + 2*6 - 1);
            if(v){
                return v.get('name');
            }
            return null;
        }
    }

}
);

    this.tpl = columntpl;//debugger;
    this.callParent(arguments);

}

});

但是,在控制台中我得到一个错误“XTemplate错误:无法调用未定义的方法'getAt'”。如何在xtemplate成员函数中访问此数据视图的存储?

1 个答案:

答案 0 :(得分:0)

您可以将商店添加到XTemplate的definitions配置中,以便在模板中访问它:

var columntpl = new Ext.XTemplate(
    '<tpl for=".">',
        // [...]
    '</tpl>',
    {
        store: theStore,
        getVal : function(xin, cin){
            var v = this.store.getAt(xin + 6 - 1);
            // [...]
        }
    }
);