ExtJS 4 dataview问题

时间:2014-03-16 19:03:33

标签: javascript extjs extjs4 extjs4.1 extjs4.2

下面的代码没有执行项目点击.i无法找出问题。是否与tpl相关? 请帮我解决这个问题。我已经尝试了所有可能性。

Ext.define('MyApp.view.MyView', {
    extend: 'Ext.view.View',

    requires: [
        'Ext.XTemplate'],

    id: 'MyView',
    itemId: 'MyView',
    width: 400,
    itemSelector: '.product',

    initComponent: function () {
        var me = this;

        Ext.applyIf(me, {
            data: [{
                name: 'ATM',
                url: './icons/atm.png'
            }, {
                name: 'BAR',
                url: './icons/bar.png'
            }, {
                name: 'GAS STATION',
                url: './icons/gas_filling_station.png'
            }, {
                name: 'GYM',
                url: './icons/gym.png'
            }, {
                name: 'HOSPITAL',
                url: './icons/hospital.png'
            }, {
                name: 'PARK',
                url: './icons/park.png'
            }, {
                name: 'SALOON',
                url: './icons/saloon.png'
            }, {
                name: 'SCHOOL',
                url: './icons/school.png'
            }, {
                name: 'SHOPPING MALL',
                url: './icons/shoppin_mall.png'
            }, {
                name: 'SUPERMARKET',
                url: './icons/supermarket.png'
            }],
            itemTpl: [
                '<tpl for=".">',
                '    <div class="product"> <img width="120" height="120" src="{url}"/> ',
                '    <strong>{name}</strong></div>',
                '</tpl>'],
            listeners: {
                itemclick: {
                    fn: me.onDataviewItemClick,
                    scope: me
                }
            }
        });

        me.callParent(arguments);
    },

    onDataviewItemClick: function (dataview, record, item, index, e, eOpts) {
        console.log('click');
        alert('working');
    }

});

2 个答案:

答案 0 :(得分:1)

我认为发生的事情是你没有正确使用data。根据API,它只能是一个对象,所以我想它的意思是其他的东西?如果您添加store,一切正常:

Ext.define('MyView', {
  extend: 'Ext.view.View',
  requires: [
    'Ext.XTemplate',
    'Ext.data.Store'
  ],

  id: 'MyView',
  itemId: 'MyView',
  width: 400,
  itemSelector: '.product',

  initComponent: function () {
    var me = this;

    // Take note of the store that's being created with your initial data
    Ext.applyIf(me, {
      store: Ext.create('Ext.data.Store', {
        fields: ['name', 'url'],
        data: [{
            name: 'ATM',
            url: './icons/atm.png'
        }, {
            name: 'BAR',
            url: './icons/bar.png'
        }, {
            name: 'GAS STATION',
            url: './icons/gas_filling_station.png'
        }, {
            name: 'GYM',
            url: './icons/gym.png'
        }, {
            name: 'HOSPITAL',
            url: './icons/hospital.png'
        }, {
            name: 'PARK',
            url: './icons/park.png'
        }, {
            name: 'SALOON',
            url: './icons/saloon.png'
        }, {
            name: 'SCHOOL',
            url: './icons/school.png'
        }, {
            name: 'SHOPPING MALL',
            url: './icons/shoppin_mall.png'
        }, {
            name: 'SUPERMARKET',
            url: './icons/supermarket.png'
        }]
      }),
      itemTpl: [
        '<tpl for=".">',
          '<div class="product">',
            '<img width="120" height="120" src="{url}"/>',
            '<strong>{name}</strong>',
          '</div>',
        '</tpl>'
      ],
      listeners: {
        itemclick: {
          fn: me.onDataviewItemClick,
          scope: me
        }
      }
    });

    me.callParent();
  },

  onDataviewItemClick: function (dataview, record, item, index, e, eOpts) {
    console.log('click');
    alert('working');
  }
});

答案 1 :(得分:1)

好像你必须使用商店而不是使用'数据'数组..

这是一个有效的例子:

http://jsfiddle.net/Vandeplas/57mNy/

var data =  [{
                name: 'ATM',
                url: './icons/atm.png'
            }, {
                name: 'BAR',
                url: './icons/bar.png'
            }, {
                name: 'GAS STATION',
                url: './icons/gas_filling_station.png'
            }, {
                name: 'GYM',
                url: './icons/gym.png'
            }, {
                name: 'HOSPITAL',
                url: './icons/hospital.png'
            }, {
                name: 'PARK',
                url: './icons/park.png'
            }, {
                name: 'SALOON',
                url: './icons/saloon.png'
            }, {
                name: 'SCHOOL',
                url: './icons/school.png'
            }, {
                name: 'SHOPPING MALL',
                url: './icons/shoppin_mall.png'
            }, {
                name: 'SUPERMARKET',
                url: './icons/supermarket.png'
            }];

Ext.define('Image', {
    extend: 'Ext.data.Model',
    fields: [
        { name:'name', type:'string' },
        { name:'url', type:'string' }
    ]
});

var store = Ext.create('Ext.data.Store', {
    id:'imagesStore',
    model: 'Image',
    data: data
});


Ext.define('MyApp.view.MyView', {
    extend: 'Ext.view.View',

    width: 400,
    itemSelector: '.product',

    initComponent: function () {
        var me = this;

        Ext.applyIf(me, {
            store: store,
            itemTpl: [
                '<tpl for=".">',
                '    <div class="product"> <img width="120" height="120" src="{url}"/> ',
                '    <strong>{name}</strong></div>',
                '</tpl>'],
            listeners: {
                itemclick: {
                    fn: me.onDataviewItemClick,
                    scope: me
                }
            }
        });

        me.callParent(arguments);
    },

    onDataviewItemClick: function (dataview, record, item, index, e, eOpts) {
        console.log('click');
        alert('working');
    }

});

Ext.create('MyApp.view.MyView', {
    renderTo: Ext.getBody()
});