将Ext表单数据绑定到ExtJS中的GridPanel

时间:2014-01-24 08:04:22

标签: javascript jquery extjs

我第一次使用ExtJS创建了一个演示页。

我已经创建了.JS文件,如下所示。

Ext.require([
//'Ext.form.*',
//'Ext.layout.container.Column',
//'Ext.tab.Panel'

    '*'
]);

Ext.onReady(function() {
    Ext.QuickTips.init();

    var bd = Ext.getBody();
    var required = '<span style="color:red;font-weight:bold" data-qtip="Required">*</span>';
    var simple = Ext.widget({
        xtype: 'form',
        layout: 'form',
        collapsible: true,
        id: 'userForm',
        frame: true,
        title: 'User Details',
        bodyPadding: '5 5 0',
        align: 'center',
        width: 350,
        buttonAlign: 'center',
        fieldDefaults: {
            msgTarget: 'side',
            labelWidth: 75
        },
        defaultType: 'textfield',
        items: [{
            id: 'txtName',
            fieldLabel: 'Name',
            name: 'name',
            afterLabelTextTpl: required,
            allowBlank: false
        }, {
            id: 'dDOJ',
            fieldLabel: 'Date Of Joining',
            name: 'doj',
            xtype: 'datefield',
            format: 'd/m/Y',
            afterLabelTextTpl: required,
            allowBlank: false
        }, {
            id: 'txtAge',
            fieldLabel: 'Age',
            name: 'age',
            xtype: 'numberfield',
            minValue: 0,
            maxValue: 100,
            afterLabelTextTpl: required,
            allowBlank: false
        }, {
            id: 'chkActive',
            xtype: 'checkbox',
            boxLabel: 'Active',
            name: 'cb'
}],

            buttons: [{
                text: 'ADD',
                listeners: {
                    click: {

                        fn: function() {
                            debugger;
                            if (Ext.getCmp('txtName').getValue() == "" || Ext.getCmp('dDOJ').getValue() == "" || Ext.getCmp('txtAge').getValue() == "") {
                                alert("Please Enter All Required Details!");
                                return false;
                            }
                            var reply = confirm("Are You Sure You Want To Save?")
                            if (reply != false) {
                                fnShowGrid();
                            }
                        }
                    }
                }
}]
            });
            simple.render(document.body);
        });
        function fnShowGrid() {
            debugger;
            var vName = Ext.getCmp('txtName').getValue();
            var vDOJ = Ext.Date.format(Ext.getCmp('dDOJ').getValue(), 'd/m/Y');
            var vAge = Ext.getCmp('txtAge').getValue();
            var vIsActive = "InActive";
            if (Ext.getCmp('chkActive').getValue() == true) {
                vIsActive = "Active";
            }
            var store = Ext.create('Ext.data.ArrayStore', {
                storeId: 'myStore',
                idIndex: 0,
                fields: [
                           { name: 'name' },
                           { name: 'doj' },
                           { name: 'age' },
                           { name: 'active' }
                        ],
                //data: { name: vName, doj: vDOJ, age: vAge, active: vIsActive}
                data: [[vName, vDOJ, vAge, vIsActive]]
            });

            store.load({
                params: {
                    start: 1,
                    limit: 3
                }
            });

            Ext.create('Ext.grid.Panel', {
                title: 'User Details',
                store: store,
                columns: [
            {
                header: 'Name',
                width: 160,
                sortable: true,
                dataIndex: 'name'
            },
            {
                header: 'Date Of Join',
                width: 75,
                sortable: true,
                dataIndex: 'doj'
            },
            {
                header: 'Age',
                width: 75,
                sortable: true,
                dataIndex: 'age'
            },
            {
                header: 'Active',
                width: 75,
                sortable: true,
                dataIndex: 'active'
}],

                height: 200,
                width: 400,
                renderTo: Ext.getBody()
            });
            //detailsGrid.render(document.body);
        }

正在显示gridPanel。但每次添加一个新数据,它创建一个新的网格!

我想只显示GridPanel一次,包括所有添加数据之前的所有内容。

这是小提琴:http://jsfiddle.net/pratikhsoni/wc9mD/1/

2 个答案:

答案 0 :(得分:1)

首先。

很高兴看到你接触到EXT js。错误我想在你的代码中突出显示。

1.   if (reply != false) {
            fnShowGrid();
      }

在此处,您正在调用要创建的网格,该网格正在调用您创建新网格的原因。你只需要更新商店。

方法:您必须只检查一次是否存在,如果是,则更新商店。

if (reply != false) {
                if(!Ext.getCmp('hello')) {
                fnShowGrid();
                } else {
                  var store=Ext.getCmp('hello').getStore();  

                   store.add ({
                              name: 'sadad',
                              doj:'25/01/2015',
                              age:'26',
                              active:'false'
                              });
                              store.reload();
                             }

                     }

所以在所有你必须更新商店添加新记录。我现在已经硬编码,你可以在创建它时获得值。

请找到更新的小提琴。

Fiddle

答案 1 :(得分:1)

以下是基于小提琴的工作示例!

Ext.require([
'*'
]);

        store = Ext.create('Ext.data.ArrayStore', {
            storeId: 'myStore',
            idIndex: 0,
            fields: [
                       { name: 'name' },
                       { name: 'doj' },
                       { name: 'age' },
                       { name: 'active' }
                    ],
            //data: { name: vName, doj: vDOJ, age: vAge, active: vIsActive}
        });

Ext.onReady(function() {
Ext.QuickTips.init();

var bd = Ext.getBody();
var required = '<span style="color:red;font-weight:bold" data-qtip="Required">*</span>';
var simple = Ext.widget({
    xtype: 'form',
    layout: 'form',
    collapsible: true,
    id: 'userForm',
    frame: true,
    title: 'User Details',
    bodyPadding: '5 5 0',
    align: 'center',
    width: 350,
    buttonAlign: 'center',
    fieldDefaults: {
        msgTarget: 'side',
        labelWidth: 75
    },
    defaultType: 'textfield',
    items: [{
        id: 'txtName',
        fieldLabel: 'Name',
        name: 'name',
        afterLabelTextTpl: required,
        allowBlank: false
    }, {
        id: 'dDOJ',
        fieldLabel: 'Date Of Joining',
        name: 'doj',
        xtype: 'datefield',
        format: 'd/m/Y',
        afterLabelTextTpl: required,
        allowBlank: false
    }, {
        id: 'txtAge',
        fieldLabel: 'Age',
        name: 'age',
        xtype: 'numberfield',
        minValue: 0,
        maxValue: 100,
        afterLabelTextTpl: required,
        allowBlank: false
    }, {
        id: 'chkActive',
        xtype: 'checkbox',
        boxLabel: 'Active',
        name: 'cb'
}],

        buttons: [{
            text: 'ADD',
            listeners: {
                click: {

                    fn: function() {
                        debugger;
                        if (Ext.getCmp('txtName').getValue() == "" || Ext.getCmp('dDOJ').getValue() == "" || Ext.getCmp('txtAge').getValue() == "") {
                            alert("Please Enter All Required Details!");
                            return false;
                        }
                        var reply = confirm("Are You Sure You Want To Save?")
                        if (reply != false) {
                            fnShowGrid();
                        }
                    }
                }
            }
}]
        });
        simple.render(document.body);
    });
    function fnShowGrid() {
        debugger;
        var vName = Ext.getCmp('txtName').getValue();
        var vDOJ = Ext.Date.format(Ext.getCmp('dDOJ').getValue(), 'd/m/Y');
        var vAge = Ext.getCmp('txtAge').getValue();
        var vIsActive = "InActive";
        if (Ext.getCmp('chkActive').getValue() == true) {
            vIsActive = "Active";
        }

        if (!Ext.getCmp('sample-grid')) {
            store.add({
                name: vName,
                doj: vDOJ,
                age: vAge,
                active: vIsActive
            });
            Ext.create('Ext.grid.Panel', {
                title: 'User Details',
                store: store,
                id: 'sample-grid',
                columns: [
                    {
                        header: 'Name',
                        width: 160,
                        sortable: true,
                        dataIndex: 'name'
                    },
                    {
                        header: 'Date Of Join',
                        width: 75,
                        sortable: true,
                        dataIndex: 'doj'                            
                    },
                    {
                        header: 'Age',
                        width: 75,
                        sortable: true,
                        dataIndex: 'age'                            
                    },
                    {
                        header: 'Active',
                        width: 75,
                        sortable: true,
                        dataIndex: 'active'                            
                    }
                ],
                height: 200,
                width: 400,
                renderTo: Ext.getBody()
            });
        } else {
            store.add({
                name: vName,
                doj: vDOJ,
                age: vAge,
                active: vIsActive
            });
        }
        }