如何在ExtJS中的表单中发布网格

时间:2015-08-17 12:23:47

标签: javascript extjs

我有一个包含组合框,标记字段,日期选择器等的表单和一个网格。这些元素中的每一个都有不同的商店。用户将从组合框等中进行选择。并在网格中输入值。然后,来自网格和其他表单元素的值都在POST上发送到服务器。我知道如何从组合框,标记字段和日期选择器发布数据。但是,我不知道如何使用表单在网格中发送数据。这是表单视图:

Ext.define('ExtTestApp.view.main.List', {
extend: 'Ext.form.Panel',
xtype: 'cell-editing',
frame: true,
autoScroll: true,
title: 'Record Event',
bodyPadding: 5,
requires: [
    'Ext.selection.CellModel',
    'ExtTestApp.store.Personnel'
],
layout: 'column',

initComponent: function(){

    this.cellEditing = new Ext.grid.plugin.CellEditing({
        clicksToEdit: 1
    });

    Ext.apply(this, {
        //width: 550,
        fieldDefaults: {
            labelAlign: 'left',
            labelWidth: 90,
            anchor: '100%',
            msgTarget: 'side'
        },

        items: [{
            xtype: 'fieldset',
            //width: 400,
            title: 'Event Information',
            //height: 460,
            //defaultType: 'textfield',
            layout: 'anchor',
            defaults: {
                anchor: '100%'
            },
            items: [{
                xtype: 'fieldcontainer',
                fieldLabel: 'Event',
                layout: 'hbox',
                defaults: {
                    hideLabel: 'true'
                },
                items: [{
                    xtype: 'combobox',
                    name: 'eventTypeId',
                    width: 300,
                    //flex: 1,
                    store: {
                        type: 'events'
                    },
                    valueField: 'eventTypeId',
                    // Template for the dropdown menu.
                    // Note the use of the "x-list-plain" and "x-boundlist-item" class,
                    // this is required to make the items selectable.
                    allowBlank: false
                }
               ]
            },
            {
                xtype: 'container',
                layout: 'hbox',
                margin: '0 0 5 0',
                items: [
                    {
                        xtype: 'datefield',
                        fieldLabel: 'Date',
                        format: 'Y-m-d',
                        name: 'startDate',
                        //msgTarget: 'under', //location of error message, default is tooltip
                        invalidText: '"{0}" is not a valid date. "{1}" would be a valid date.',
                        //flex: 1,
                        emptyText: 'Start',
                        allowBlank: false
                    },
                    {
                        xtype: 'datefield',
                        format: 'Y-m-d',
                        name: 'endDate',
                        //msgTarget: 'under', //location of error message
                        invalidText: '"{0}" is not a valid date. "{1}" would be a valid date.',
                        //flex: 1,
                        margin: '0 0 0 6',
                        emptyText: 'End',
                        allowBlank: false
                    }
                ]
            }]
        },
        {
            xtype: 'fieldset',
            //height: 460,
            title: 'Platform Information',
            //defaultType: 'textfield',
            layout: 'anchor',
            defaults: {
                anchor: '100%'
            },
            items: [
                {
                    xtype: 'fieldcontainer',
                    layout: 'hbox',
                    fieldLabel: 'Platform',
                    //combineErrors: true,
                    defaults: {
                        hideLabel: 'true'
                    },
                    items: [
                        {
                            xtype: 'combobox',
                            name: 'platformId',
                            store: {
                                type: 'platforms'
                            },
                            valueField: 'platformId',
                            allowBlank: false
                        }
                    ]
                }
            ]
        },  
        {
            xtype: 'fieldcontainer',
                layout: 'hbox',
                fieldLabel: 'Software Type(s)',
                //combineErrors: true,
                defaults: {
                    hideLabel: 'true'
                },
                items: [
                    {
                        xtype: 'tagfield',
                        width: 400,
                        //height: 50,
                        fieldLabel: 'Software Type(s)',
                        name: 'softwareTypeIds',
                        store: {
                            type: 'softwareTypes'
                        },
                        labelTpl: '{softwareName} - {manufacturer}',
                        valueField: 'softwareTypeId',
                        allowBlank: false
                    }
                ]
        },
        {
            xtype: 'gridpanel',
            layout: 'anchor',
            defaults: {
                anchor: '100%'
            },
            width: 1300,
            //columnWidth: 0.78,
            //title: 'Metrics',
            plugins: [this.cellEditing],

            title: 'Personnel',

            store: {
                type: 'personnel'
            },

            columns: [
                { text: 'Name',  dataIndex: 'name', editor: 'textfield' },
                { text: 'Email', dataIndex: 'email', flex: 1 },
                { text: 'Phone', dataIndex: 'phone', flex: 1 }
            ]
        }

        ],
        buttons: [

            {
                text: 'Save Event',
                handler: function() {
                    var form = this.up('form'); // get the form panel

                    // if (form.isValid()) { // make sure the form contains valid data before submitting
                        Ext.Ajax.request({
                            url: 'api/events/create',
                            method:'POST',
                            headers: { 'Content-Type': 'application/json' },
                            params : Ext.JSON.encode(form.getValues()),
                            success: function(form, action) {
                                Ext.Msg.alert('Success', action.result);
                            },
                            failure: function(form, action) {
                                //console.log(form.getForm().getValues());
                                Ext.Msg.alert('Submission failed', 'Please make sure you selected an item for each required field.', action.result);
                            }
                        });
                    // } else { // display error alert if the data is invalid
                    //     Ext.Msg.alert('Submit Failed', 'Please make sure you have made selections for each required field.')
                    // }
                }
            }
        ]
    });
    this.callParent();
}
});

var grid = Ext.ComponentQuery.query('grid')[0];

这是网格的商店:

Ext.define('ExtTestApp.store.Personnel', {
extend: 'Ext.data.Store',

alias: 'store.personnel',

fields: [
    'name', 'email', 'phone'
],

data: { items: [
    { name: 'Jean Luc', email: "jeanluc.picard@enterprise.com", phone: "555-111-1111" },
    { name: 'Worf',     email: "worf.moghsson@enterprise.com",  phone: "555-222-2222" },
    { name: 'Deanna',   email: "deanna.troi@enterprise.com",    phone: "555-333-3333" },
    { name: 'Data',     email: "mr.data@enterprise.com",        phone: "555-444-4444" }
]},

autoLoad: true,

proxy: {
    type: 'memory',
    api: {
        update: 'api/update'
    },
    reader: {
        type: 'json',
        rootProperty: 'items'
    },
    writer: {
        type: 'json',
        writeAllFields: true,
        rootProperty: 'items'
    }
}
});

1 个答案:

答案 0 :(得分:2)

理想情况下,您需要创建一个自定义的“网格字段”,以便在表单提交时选择网格数据,就像任何其他字段一样。

您也可以使用此解决方法:在“保存事件”按钮处理程序中,挖掘网格存储并从中捕获数据:

    var gridData = [];
    form.down('gridpanel').store.each(function(r) {
        gridData.push(r.getData());
    });

然后获取其余的表单数据并将网格数据放入其中:

    var formData = form.getValues();
    formData.gridData = gridData;

最后,将其全部包含在您的AJAX调用中:

params: Ext.JSON.encode(formData)