简体   繁体   English

ExtJs不会使用ext.data.store将数据加载到表单

[英]ExtJs doesn't load data to a form using ext.data.store

i need to load the data from a json object in to the form panel when the form is loaded my code is like this 加载表单时,我需要将json对象中的数据加载到表单面板中,我的代码是这样的

this is my data model 这是我的数据模型

Ext.define('Contact', {
extend : 'Ext.data.Model',
fields : [ {
    name : 'first',
    mapping : 'name.first'
}, {
    name : 'last',
    mapping : 'name.last'
}, 'company', 'email', {
    name : 'dob',
    type : 'date',
    dateFormat : 'm/d/Y'
} ]

}); });

and this is my data store 这是我的数据存储

var store = Ext
    .create(
            'Ext.data.Store',
            {
                // alert("inside")
                // id: 'store',
                model : 'Contact',
                proxy : {
                    type : 'ajax',
                    url : 'http://localhost:8090/extjs-crud-grid-spring-hibernate/contact/view.action',
                    reader : 'json',
                    root : 'contact'
                },
                autoLoad : true
            });

and this is my form panel 这是我的表单面板

Ext.onReady(function() {
var formPanel = Ext.create('Ext.form.Panel', {
    title : 'Simple Form with FieldSets',
    labelWidth : 75, // label settings here cascade unless
    // overridden
    // url : 'save-form.php',
    frame : true,
    bodyStyle : 'padding:5px 5px 0',
    width : 340,
    bodyPadding : 5,

    layout : 'anchor', // arrange fieldsets side by side
    items : [ {
        xtype : 'fieldset',
        title : 'Contact Information',
        defaultType : 'textfield',
        defaults : {
            width : 280
        },
        items : [ {
            fieldLabel : 'First Name',
            emptyText : 'First Name',
            name : 'first'
        }, {
            fieldLabel : 'Last Name',
            emptyText : 'Last Name',
            name : 'last'
        }, {
            fieldLabel : 'Company',
            name : 'company'
        }, {
            fieldLabel : 'Email',
            name : 'email',
            vtype : 'email'
        }, {
            xtype : 'datefield',
            fieldLabel : 'Date of Birth',
            name : 'dob',
            allowBlank : false,
            maxValue : new Date()
        } ]
    } ],    
    renderTo : Ext.getBody()
}); 
var record = store.getAt(0);

formPanel.getForm().loadRecord(record);

}); });

and this is my json format 这是我的json格式

{
  "contact": {
    "name": {
      "first": "Aaron",
      "last": "Conran"
    },
    "company": "Ext JS",
    "email": "support@sencha.com",
    "state": "OH",
    "dob": "04/15/2007"
  }
}

when i try to put this line "formPanel.getForm().loadRecord(record)" it gives me this error "Uncaught TypeError: Cannot call method 'getData' of undefined " as well as this doesn't load the data in to the form panel when loaded 当我尝试将这一行“ formPanel.getForm()。loadRecord(record)”放给我时,出现此错误“ Uncaught TypeError:无法调用未定义的方法'getData'”,并且这也不会将数据加载到加载时的表格面板

hope that anybody can help me 希望有人可以帮助我

Are you getting data in record variable. 您是否在记录变量中获取数据。 Please refer below example... 请参考以下示例...

Instances of this class are only created by a Form when loading. 此类的实例仅在加载时由Form创建。

Response Packet Criteria

A response packet must contain: 响应数据包必须包含:

  • success property : Boolean 成功属性:布尔值
  • data property : Object 数据属性:对象

The data property contains the values of Fields to load. data属性包含要加载的字段的值。 The individual value object for each Field is passed to the Field's setValue method. 每个字段的单个值对象将传递到字段的setValue方法。

JSON Packets

By default, response packets are assumed to be JSON, so for the following form load call: 默认情况下,假定响应数据包为JSON,因此对于以下表单加载调用:

var myFormPanel = new Ext.form.FormPanel({
    title: 'Client and routing info',
    items: [{
        fieldLabel: 'Client',
        name: 'clientName'
    }, {
        fieldLabel: 'Port of loading',
        name: 'portOfLoading'
    }, {
        fieldLabel: 'Port of discharge',
        name: 'portOfDischarge'
    }]
});
myFormPanel.getForm().load({
    url: '/getRoutingInfo.php',
    params: {
        consignmentRef: myConsignmentRef
    },
    failure: function(form, action) {
        Ext.Msg.alert("Load failed", action.result.errorMessage);
    }
});

a success response packet may look like this: 成功响应数据包可能如下所示:

{
    success: true,
    data: {
        clientName: "Fred. Olsen Lines",
        portOfLoading: "FXT",
        portOfDischarge: "OSL"
    }
}

while a failure response packet may look like this: 而故障响应数据包可能如下所示:

{
    success: false,
    errorMessage: "Consignment reference not found"
}

Other data may be placed into the response for processing the Form's callback or event handler methods. 可以将其他数据放入响应中,以处理Form的回调或事件处理程序方法。 The object decoded from this JSON is available in the result property. 从此JSON解码的对象在result属性中可用。

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

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