简体   繁体   English

Extjs 4.2自动同步动态网格存储

[英]Extjs 4.2 autosync dynamic grid store

I have a grid with dynamic columns: 我有一个带有动态列的网格:

MODEL 模型

Ext.define('App.mdlCriteriosConcurso', {
    extend: 'Ext.data.Model',
    fields: [
    ]   
});

STORE 商店

Ext.define('App.strCriteriosConcurso', {
    extend: 'Ext.data.Store',
    model:  'App.mdlCriteriosConcurso',
    autoLoad: false,
    proxy: {
        type: 'ajax', 
        api: {
            read: 'some url',
            update: 'some url',
        },
        reader: {
                type: 'json',
                root: 'data',
                totalProperty: 'total'
        },
        writer: {
            root: 'records',
            encode: true,
            writeAllFields: true
        }
    }
});

GRID GRID

var almacenCriteriosConcurso = Ext.create('App.strCriteriosConcurso');
//Some code ...
{
    xtype:'grid',
    itemId:'gridCriteriosConcursoV4',
    store:almacenCriteriosConcurso,
    plugins: [Ext.create('Ext.grid.plugin.CellEditing', {clicksToEdit: 2})],
    columns:[]
}
//Some code...

CONTROLLER CONTROLLER

Here in the controller I have the next piece of code: 在控制器中,这里有下一段代码:

    Ext.ComponentQuery.query('viewFichaDetalle #tabpanelsecundario4_1 #gridCriteriosConcursoV4')[0].getStore().addListener('metachange',function(store,meta){
        var columnas=0;
        var renderer1 = function(v,params,data){
            if(v==''){
                return '<div style="background-color:#F5FAC3;color:blue;">'+Ext.util.Format.number(0,'0.000,00/i')+'</div>';
            }
            else{
                return '<div style="background-color:#F5FAC3;color:blue;">'+Ext.util.Format.number(v,'0.000,00/i')+'</div>';
            }
        };
        var renderer2 = function(v,params,data){
            if(v=='' || v==0){
                return '<div style="background-color:#F5FAC3;color:green;">'+Ext.util.Format.number(0,'0.000,00/i')+'</div>';
                //return '';
            }
            else{
                return '<div style="background-color:#F5FAC3;color:green;">'+Ext.util.Format.number(v,'0.000,00/i')+'</div>';
            }
        };

        Ext.each(meta.columns,function(col){
            if(columnas==2){
                meta.columns[columnas].renderer = renderer1;
            }
            if(columnas>=3){
                meta.columns[columnas].renderer = renderer2;
            }
            columnas++;
        },this);
        Ext.suspendLayouts();
        Ext.ComponentQuery.query('viewFichaDetalle #tabpanelsecundario4_1 #gridCriteriosConcursoV4')[0].reconfigure(store, meta.columns);
        Ext.ComponentQuery.query('viewFichaDetalle #tabpanelsecundario4_1 #gridCriteriosConcursoV4')[0].setTitle("<span style='color:red;font-weight:bold;font-size: 12pt'>Criterios del Concurso con ID:</span> "+"<span style='color:black;font-weight:bold;font-size: 12pt'>"+this.IdConcurso+"</span>");
        Ext.resumeLayouts(true);
    },this);

I create the columns in the php, using the metadata. 我使用元数据在php中创建列。

With this code I add some renderers to the grid columns. 通过此代码,我向网格列添加了一些渲染器。 And I see all the data perfect, and can edit the data. 而且我看到所有数据都很完美,并且可以编辑数据。

In the php y generate the column and the field like this: 在php y中,生成列和字段,如下所示:

$array_metadata['columns'][]=array("header"=>"<span style='color:blue;'>Resultado</span>","dataIndex"=>"puntos_resultado","width"=>82,"align"=>"right","editor"=>"textfield");
$array_metadata['fields'][]=array("name"=>"puntos_resultado","type"=>"float");

And then pass $array_metadata to 'metaData' response. 然后将$ array_metadata传递给“ metaData”响应。

But when I try to sync or autosync the store I get this error: 但是,当我尝试同步或自动同步商店时,出现此错误:

Uncaught TypeError: Cannot read property 'name' of undefined
    at constructor.getRecordData (ext-all-dev.js:62247)
    at constructor.write (ext-all-dev.js:62192)
    at constructor.doRequest (ext-all-dev.js:102306)
    at constructor.update (ext-all-dev.js:101753)
    at constructor.runOperation (ext-all-dev.js:106842)
    at constructor.start (ext-all-dev.js:106769)
    at constructor.batch (ext-all-dev.js:62869)
    at constructor.sync (ext-all-dev.js:64066)
    at constructor.afterEdit (ext-all-dev.js:64162)
    at constructor.callStore (ext-all-dev.js:101428)

UPDATE 1 更新1

I have fount this thread in Sencha Forums link , and I have tried all posibles solutions and Im getting allways the same error. 我在Sencha Forums 链接中找到了该线程,并且尝试了所有可能的解决方案,而Im始终遇到相同的错误。

The error tells us that you don't fill the model's fields array properly, because that is where name is a required config. 该错误告诉我们您没有正确填充模型的fields数组,因为那是name是必需配置的地方。 In ExtJS 4, you have to add all fields to the model for the sync to work properly. 在ExtJS 4中,必须将所有字段添加到模型中,同步才能正常工作。

To be exact, the Model prototype has to be filled with the correct fields before the instances are created. 确切地说,在创建实例之前,必须为Model原型填充正确的字段。

This means that you will have to override the reader's getResponseData method, because between Ext.decode and readRecords you will have to prepare the model prototype by setting the fields as returned from the server; 这意味着您将必须重写读取器的getResponseData方法,因为在Ext.decodereadRecords之间,您将必须通过设置服务器返回的字段来准备模型原型。 something like this: 像这样的东西:

App.mdlCriteriosConcurso.prototype.fields = data.fields;

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

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