ExtJs Grid如何设置选择

时间:2014-07-24 08:09:59

标签: javascript extjs

我有这个示例代码http://jsfiddle.net/Xpe9V/414/

Ext.require([
    'Ext.data.*',
    'Ext.grid.*'
]);

function getRandomDate() {
    var from = new Date(1900, 0, 1).getTime();
    var to = new Date().getTime();
    return new Date(from + Math.random() * (to - from));
}

function createFakeData(count) {
        var firstNames   = ['Ed', 'Tommy', 'Aaron', 'Abe'];
        var lastNames    = ['Spencer', 'Maintz', 'Conran', 'Elias'];

        var data = [];
        for (var i = 0; i < count ; i++) {
            var dob = getRandomDate();           
            var firstNameId = Math.floor(Math.random() * firstNames.length);
            var lastNameId  = Math.floor(Math.random() * lastNames.length);
            var name        = Ext.String.format("{0} {1}", firstNames[firstNameId], lastNames[lastNameId]);

            data.push([name, dob]);
        }
        return data;
    }

Ext.onReady(function(){
    Ext.define('Person',{
        extend: 'Ext.data.Model',
        fields: [
            'Name', 'dob'
        ]
    });

    // create the Data Store
    var store = Ext.create('Ext.data.Store', {
        model: 'Person',
        autoLoad: true,
        proxy: {
            type: 'memory',
                data: createFakeData(10),
                reader: {
                    type: 'array'
                }
        }
    });

    // create the grid
    var x = Ext.create('Ext.grid.Panel', {
        store: store,
        columns: [
            {text: "Name", width:120, dataIndex: 'Name'},
            {text: "dob", width: 380, dataIndex: 'dob'}
        ],
        renderTo:'example-grid',
        width: 500,
        height: 280
    });

    x.getSelectionModel().select(4);

});

在第61行,我想在我的网格中进行选择,但似乎没有任何效果。谁能告诉我为什么?

1 个答案:

答案 0 :(得分:2)

网格仍在加载。你必须为渲染事件添加一个监听器,然后你可以放弃第61行。我更新了你的jsfiddle,见这里:http://jsfiddle.net/Xpe9V/417/

var x = Ext.create('Ext.grid.Panel', {
    store: store,
    columns: [
        {text: "Name", width:120, dataIndex: 'Name'},
        {text: "dob", width: 380, dataIndex: 'dob'}
    ],
    renderTo:'example-grid',
    width: 500,
    height: 280,    
    listeners: {
      'render': function(component) {
        if (this.store.isLoading() || this.store.getCount() == 0) {
            this.store.on('load', function() {
                this.getSelectionModel().select(4);
            }, this, {single: true});
        } else {
            this.getSelectionModel().select(4);
        }
      }
    }
});