javascript - ExtJs Grid 如何设置选择

标签 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 行,我想在网格中进行选择,但它似乎没有任何效果。谁能告诉我为什么?

最佳答案

网格仍在加载中。您必须为渲染事件添加一个监听器,然后您可以删除第 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);
        }
      }
    }
});

关于javascript - ExtJs Grid 如何设置选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24928329/

相关文章:

javascript - 设置选定的选项 JQuery

javascript - 如何使用 Node.js 解析 JSON?

javascript - Extjs 使用 addCls() 改变绘图点的颜色,几乎搞定了

Extjs - 在通过 REST 将其发送到服务器之前,停止客户端以将任何 "id"附加到模型实例

extjs - 在 Windows 8 Metro 应用程序中使用 Sencha Touch

javascript - 在这种情况下如何将变量传递给函数? (对于 onclick 事件)

javascript - 我应该如何归因于 Twitter bootstrap 的 glyphicons 组件?

javascript - 我想让关键帧的动画从单击按钮开始

extjs - 你如何获得所有商店参数? (ExtJS 4.1)

apache-flex - RIA 框架 : Which is best free opensource RIA Framework for Java Developers?