javascript - ExtJs Grid How to set selection - Stack Overflow

I have this sample codeExt.require(['Ext.data.*','Ext.grid.*']);function getRan

I have this sample code /

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. Can anyone tell me why?

I have this sample code http://jsfiddle/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. Can anyone tell me why?

Share Improve this question asked Jul 24, 2014 at 8:09 gefeigefei 19.9k9 gold badges54 silver badges70 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

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/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(ponent) {
        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);
        }
      }
    }
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745256901a4619012.html

相关推荐

  • javascript - ExtJs Grid How to set selection - Stack Overflow

    I have this sample codeExt.require(['Ext.data.*','Ext.grid.*']);function getRan

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信