简体   繁体   English

ExtJs Grid如何设置选择

[英]ExtJs Grid How to set selection

I have this sample code http://jsfiddle.net/Xpe9V/414/ 我有此示例代码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);

});

On line 61 I want to make a selection in my grid, but it does not seems to have any effect. 在第61行,我想在网格中进行选择,但是似乎没有任何效果。 Can anyone tell me why? 谁能告诉我为什么?

The grid is still loading. 网格仍在加载。 You have to add a listener for the render event, then you can drop line 61. I updated your jsfiddle, see here: http://jsfiddle.net/Xpe9V/417/ 您必须为render事件添加一个侦听器,然后可以删除第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);
        }
      }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM