代码之家  ›  专栏  ›  技术社区  ›  Michael Wales

Ext.data.Store、Javascript数组和Ext.grid.ColumnModel

  •  1
  • Michael Wales  · 技术社区  · 17 年前

    this_column ),将该阵列推到另一个阵列的末端( columns ),并最终将其传递给Ext.grid.ColumnModel对象。

    我遇到的问题是——无论我测试的是哪个查询(我有很多查询,大小和复杂度各不相同),columns数组始终按预期工作到 columns[15] columns[16] ,则从该点到上一个点的所有索引都将填充 栏目[15]

    下面是一些代码:

    columns = [];
    this_column = [];
    
    var MetaData = Ext.data.Record.create([
        {name: 'id'},
        {name: 'table'},
        {name: 'field'},
        {name: 'title'}
    ]);
    // Query the server for metadata for the query we're about to run
    metaDataStore = new Ext.data.Store({
        autoLoad: true,
        reader: new Ext.data.JsonReader({
            totalProperty: 'results',
            root: 'fields',
            id: 'id'
        }, MetaData),
        proxy: new Ext.data.HttpProxy({
            url: 'index.php/' + type + '/' + slug
        }),
        listeners: {
            'load': function () {
                metaDataStore.each(function(r) {
                    this_column['id'] = r.data['id'];
                    this_column['header'] = r.data['title'];
                    this_column['sortable'] = true;
                    this_column['dataIndex'] = r.data['table'] + '.' + r.data['field'];
                    // This display valid information, through the entire process
                    console.info(this_column['id'] + ' : ' + this_column['header'] + ' : ' + this_column['sortable'] + ' : ' + this_column['dataIndex']);
                    columns.push(this_column);
                });
    
                // This goes nuts at columns[15]
                console.info(columns);
    
                gridColModel = new Ext.grid.ColumnModel({
                    columns: columns
                });
    
    3 回复  |  直到 17 年前
        1
  •  0
  •   Michael Wales    17 年前

    好的,因为this_列数组在每次运行时都能正确响应,但列数组没有,所以我想这一定是push()的问题。

    在稍微玩弄了一下之后,我改变了代码,在循环的每次迭代中重置this_列数组-似乎已经解决了这个问题。。。

    metaDataStore.each(function(r) {
        this_column = [];
        this_column['id'] = r.data['id'];
        this_column['header'] = r.data['title'];
        this_column['sortable'] = true;
        this_column['dataIndex'] = r.data['table'] + '.' + r.data['field'];
        columns.push(this_column);
    });
    
        2
  •  0
  •   dmd dmd    17 年前

    我不确定您使用的是网格还是数据视图,但两者的概念基本相同。如果您必须进行一点数据定制,但实际上可以在prepareData回调函数中进行,而不是手动进行。

        3
  •  0
  •   Daniel Beardsley    16 年前

    因为您首先使用变量 this_column 在全局上下文中(在示例的顶部),它成为一个 . 相反,您应该将每个列定义实例化为一个对象文本(分成多行以便于读取)。

    metaDataStore.each(function(r) {
      columns.push({
        id: r.data['id'],
        header: r.data['title'],
        sortable: true,
        dataIndex: r.data['table'] + '.' + r.data['field']
      });
    });
    

    局部变量

    metaDataStore.each(function(r) {
        var this_column = {};
        ...