从嵌套的json加载并保存到extjs中的网格

时间:2013-03-19 06:26:34

标签: json extjs grid nested associations

  

我有一个嵌套的json用于员工离职,在里面我留下了历史并留下了资格。我需要用json填充两个网格。我无法从json读取或写入它...请帮助...

     

我的Json是......

          {

    "EmployeeLeaves": {
        "ID": 37,
        "LeaveEligibility": [
            {
                "Availed": 4,
                "Fullpaydays": 6,
                "Halfpaydays": 0,
                "ID": 1,
                "LeaveTypeId": 60,
                "PersonId": 37,
                "YearOfLeave": 2013
            },
            {
                "Availed": 2,
                "Fullpaydays": 6,
                "Halfpaydays": 0,
                "ID": 3,
                "LeaveTypeId": 61,
                "PersonId": 37,
                "YearOfLeave": 2013
            },
            {
                "Availed": 10,
                "Fullpaydays": 10,
                "Halfpaydays": 0,
                "ID": 4,
                "LeaveTypeId": 62,
                "PersonId": 37,
                "YearOfLeave": 2013
            },
            {
                "Availed": 0,
                "Fullpaydays": 8,
                "Halfpaydays": 0,
                "ID": 6,
                "LeaveTypeId": 63,
                "PersonId": 37,
                "YearOfLeave": 2013
            }
        ],
        "LeaveHistory": [
            {
                "AppliedTo": "Vibha",
                "ApprovalDate": "3/12/2013 12:00:00 AM",
                "ApprovedBy": 12,
                "Duration": 3,
                "FromDate": "3/24/2013 12:00:00 AM",
                "FullPayDays": 3,
                "HalfPayDays": 0,
                "ID": 1,
                "LeaveTypeId": 60,
                "LossOfPayDays": 0,
                "PersonId": 37,
                "Remarks": "onnulla",
                "StatusId": 79,
                "SubstitutePersonId": 13,
                "ToDate": "3/26/2013 12:00:00 AM"
            },
            {
                "AppliedTo": "Vibha",
                "ApprovalDate": "3/12/2013 12:00:00 AM",
                "ApprovedBy": 12,
                "Duration": 1,
                "FromDate": "3/30/2013 12:00:00 AM",
                "FullPayDays": 1,
                "HalfPayDays": 0,
                "ID": 2,
                "LeaveTypeId": 60,
                "LossOfPayDays": 0,
                "PersonId": 37,
                "Remarks": "onnulla",
                "StatusId": 79,
                "SubstitutePersonId": 13,
                "ToDate": "3/31/2013 12:00:00 AM"
            },
            {
                "AppliedTo": "Shyam",
                "ApprovalDate": "",
                "ApprovedBy": 0,
                "Duration": 2,
                "FromDate": "2/13/2013 12:00:00 AM",
                "FullPayDays": 2,
                "HalfPayDays": 0,
                "ID": 4,
                "LeaveTypeId": 61,
                "LossOfPayDays": 0,
                "PersonId": 37,
                "Remarks": "blha blah",
                "StatusId": 70,
                "SubstitutePersonId": 1,
                "ToDate": "2/14/2013 12:00:00 AM"
            },
            {
                "AppliedTo": "Shyam",
                "ApprovalDate": "2/13/2013 4:35:16 PM",
                "ApprovedBy": 2,
                "Duration": 5,
                "FromDate": "3/23/2013 12:00:00 AM",
                "FullPayDays": 5,
                "HalfPayDays": 0,
                "ID": 5,
                "LeaveTypeId": 62,
                "LossOfPayDays": 0,
                "PersonId": 37,
                "Remarks": "oh..",
                "StatusId": 71,
                "SubstitutePersonId": 1,
                "ToDate": "3/27/2013 12:00:00 AM"
            },
            {
                "AppliedTo": "Shyam",
                "ApprovalDate": "2/13/2013 4:35:16 PM",
                "ApprovedBy": 2,
                "Duration": 5,
                "FromDate": "3/28/2013 12:00:00 AM",
                "FullPayDays": 5,
                "HalfPayDays": 0,
                "ID": 6,
                "LeaveTypeId": 62,
                "LossOfPayDays": 0,
                "PersonId": 37,
                "Remarks": "oh..",
                "StatusId": 71,
                "SubstitutePersonId": 1,
                "ToDate": "3/29/2013 12:00:00 AM"
            }
        ],
        "PLavailedcount": 2
    }
   }
  

我的模特和商店如下......

    Ext.define('custom.writer.Json', {
extend: 'Ext.data.writer.Json',
    getRecordData: function(record) {
        Ext.apply(record.data, record.getAssociatedData());
        return record.data;
    }
});
Ext.define('AM.store.LeaveRequest', {
extend: 'Ext.data.Store',
model: 'AM.model.leaverequest',
    autoLoad:true,
    proxy: {
    type: 'ajax',
    url:'ARSENAWeb/employeeleave.json?id=37',
    reader: {
        type: 'json',
        root: 'EmployeeLeaves'

    },
    writer:new custom.writer.Json({writeAllFields: true}),

  api: {
  read: 'ARSENAWeb/employeeleave.json?id=37',
  update: 'ARSENAWeb/employeeleave.json?id=37',
  create:'ARSENAWeb/employeeleave.json?id=37'
}}

});


Ext.define('AM.model.leaverequest', {
    extend: 'Ext.data.Model',
    fields: ['ID' ],
    idProperty: 'ID',
    autoSync:true,
    uses: ['AM.model.MLeaveHistory','AM.model.MLeaveStatus'],
    hasMany: {model: 'MLeaveHistory',model:'MleaveStatus'},

         save: function() { 
        var me = this; 

        if (me.persistAssociations) { 
            me.set( me.getAssociatedData() ); 
        } 
        me.callParent(arguments); 
    } 
});




Ext.define('AM.model.MLeaveHistory', {
    extend: 'Ext.data.Model',
   fields: [
               {name:'ApprovalDate'},
               {name:'ApprovedBy'},
               {name:'Duration'},
               {name:'FromDate'},
               {name:'FullPayDays'},
               {name:'HalfPayDays'},
               {name:'ID'},
               {name:'LeaveTypeId'},
               {name:'LossOfPayDays'},
               {name:'PersonId'},
               {name:'Remarks'},
               {name:'StatusId'},
               {name:'SubstitutePersonId'},
               {name:'ToDate'}
    ],
    belongsTo: 'AM.model.leaverequest'
});




Ext.define('AM.model.MLeaveStatus', {
    extend: 'Ext.data.Model',
   fields: [    {name:'Availed'},
                {name:'Fullpaydays'},
                {name:'Halfpaydays'},
                {name:'ID'},
                {name:'LeaveTypeId'},
                {name:'PersonId'},
                {name:'YearOfLeave'}
    ],
    belongsTo: 'AM.model.leaverequest'
});
  

我的控制器

Ext.define('AM.controller.Users', {
    extend: 'Ext.app.Controller',
    stores: ['LType','SleaveHistory','SLeaveStatus',
             ],
    models: [
             'LeaveType','MLeaveHistory','MLeaveStatus'

            ],
 views: [

        'Leave.LeaveRequest','Leave.LeaveHistoryGrid','Leave.LeaveStatusGrid','Leave.HolidayList'
        ],

    init:  function() {
        this.control({
           'viewport >
            'LeaveLeaveRequest button[action=reset]': {
                click: this.resetForm
            },
            'LeaveLeaveRequest button[action=save]': {
                click: this.saveForm
            },
            'leaveleavehistorygrid actioncolumn': {
            itemclick: this.cancelLeave
            },
            'leaveleavehistorygrid': {
            itemdblclick: this.editLeave
            }

        });
    },



    resetForm: function(button){

        button.up('form').getForm().reset();
    },

    saveForm: function(button){

        var form = button.up('form').getForm();
        form.getValues(true);
        var fromDate =new Date( Ext.getCmp('startdate'));//.getValue();
        var endDate = new Date(Ext.getCmp('enddate'));
       // var nod=Ext.Date.parseDate(fromDate,'m')
       // Date.parseDate( String input, String format ) : Date
     // var nod=fromDate+endDate;
     var oneDay = 24*60*60*1000;
        var diffDays = Math.round(Math.abs((fromDate.getTime() - endDate.getTime())/(oneDay)));
        console.log(diffDays);
        if (form.isValid()){
        var record = form.getRecord();
        var values = form.getValues();

  // console.log(values);
   if(!record){
   var newRecord = new AM.model.leaverequest(values);
   this.getLeaveRequestStore().add(newRecord);
     }else {
   record.set(values);
   }}
    } ,
    cancelLeave: function (view, cell, rowIndex, colIndex, e, record, row) {

                Ext.Msg.confirm('Remove Qualification', 'Are you sure?', function (button) {
            if (button == 'yes') {
                var store = Ext.getStore('LeaveRequest');
               var newRecord = new AM.model.leaverequest(row);
               console.log(record);
               console.log(store);
               store.remove(record);
               store.sync();
            }
        }, this);



    }

      });   

我的网格是:

    Ext.define('AM.view.Leave.LeaveHistoryGrid' ,{
        extend: 'Ext.grid.Panel',
        alias: 'widget.leaveleavehistorygrid',
        title: 'Leave History',
        store: 'LeaveRequest',
     xtype: 'cell-editing',
    plugins: [
        Ext.create('Ext.grid.plugin.RowEditing', {
            startEditByClick: function (){},
            onEnterKey: function (){}
        })
    ],

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

     var required = '<span style="color:red;font-weight:bold" data-qtip="Required">*</span>';


    this.columns= [

                {header: 'Leave Type',  dataIndex: 'LeaveTypeId',flex:1},
                {header: 'Start Date', dataIndex: 'FromDate',renderer: Ext.util.Format.dateRenderer('m/d/y'),flex:1,editor: {
                 xtype: 'datefield',
                 forceSelection : true,

                    minValue: '01/01/06',
                    disabledDays: [0, 6],
                     allowBlank       :false,
                    disabledDaysText: 'Plants are not available on the weekends'
                }},
                {header: 'End Date', dataIndex: 'ToDate',renderer: Ext.util.Format.dateRenderer('m/d/y'),flex:1, editor: {
                    xtype: 'datefield',
                    format: 'm/d/y',
                    minValue: '01/01/06',
                    disabledDays: [0, 6],
                    allowBlank       :false,
                    disabledDaysText: 'Plants are not available on the weekends'
                }},
                {header: 'No of Days', dataIndex: 'Duration',flex:1},
               // {header: 'Approval Status', dataIndex: 'leaveapprovalstatus',flex:1},
                {header: 'Descriptions', dataIndex: 'Remarks',flex:1},
                {header: 'Cancel Leave',flex:1,
                xtype: 'actioncolumn',
                width:30,
                action:'cancel',
                sortable: false,
                items: [{
                handler: function(view, cell, rowIndex, colIndex, e, record, row) {
                                        this.addEvents('itemclick');

                                  this.fireEvent('itemclick',view, cell, rowIndex, colIndex, e, record, row);


                               },
                    icon: 'ext-4.1.1a-gpl/ext-4.1.1a/docs/extjs/resources/themes/images/default/grid/page-first-disabled.gif',
                    tooltip: 'Cancel Leave'
                       }]
            }]

            this.callParent(arguments);
        }

    });







Ext.define('AM.view.Leave.LeaveStatusGrid' ,{
    extend: 'Ext.grid.Panel',
    alias: 'widget.leaveleavestatusgrid',
    title: 'Leave History',
    store: 'LeaveRequest',


    initComponent: function() {

this.columns= [
            {header: 'Type Of Leave',  dataIndex: 'typeofleave',flex:1},
            {header: 'Available', dataIndex: 'startdate',flex:1},
            {header: 'Availed', dataIndex: 'leaveavailed',flex:1},
            {header: 'Balance', dataIndex: 'leavebalance',flex:1}];

        this.callParent(arguments);
    }
});
  

我的表格

Ext.define('AM.view.Leave.LeaveRequest', {
    extend: 'Ext.form.Panel',
    alias: 'widget.LeaveLeaveRequest',
//    requires: [
//        'Ext.ux.form.MultiSelect',
//    ],

    title: 'Leave Request',
  //  frame:true,
  dockedItems: [{
  dock: 'top',
    xtype: 'toolbar',
    items: [ {
            text: 'Reset',
            action: 'reset'
        }, {
            text: 'Save',
            action: 'save'
        }]
 }],


   initComponent: function() {


   var required = '<span style="color:red;font-weight:bold" data-qtip="Required">*</span>';

       // var value,value1;           

        this.items = [{xtype:'fieldset',
                       layout:'hbox',

        items:[{
             xtype:'container',
             layout:'anchor',

             items:[{
                        xtype:'textfield',
                        fieldLabel       : 'Type of Leave',
                        labelWidth:'130px',
                        allowBlank       :false,
                        afterLabelTextTp1:required
                    },{
//                        xtype:'checkbox',
//                        boxLabel: 'Half Day ',
//                        name: 'durationofleave',
//                        inputValue: 'halfday',
//                        uncheckedValue: 'fullday',
//                        allowBlank       :false,
//                        afterLabelTextTp1:required
//                       },{
                        xtype:'datefield',

                        //renderer: Ext.util.Format.dateRenderer('m/d/y'),
                        vtype:'daterange',
                        fieldLabel       : 'From Date',
                        labelWidth:'130px',
                        id:'FromDate',
                        afterLabelTextTpl: required,
                        name    : 'FromDate',
                        endDateField: 'ToDate',
                        //minValue: new Date(),
                        allowBlank       :false,
                        afterLabelTextTp1:required
                    },{
                        xtype:'datefield',
                        vtype:'daterange',
                        renderer: Ext.util.Format.dateRenderer('m/d/y'),
                        fieldLabel       : 'To Date',
                        id:'ToDate',
                        startDateField: 'FromDate',
                        labelWidth:'130px',
//                        onExpand: function() {
//                        var field = Ext.getCmp('startdate');
//                        value: field.getValue();
//                        value1 = this.getValue();
//                        console.log(value+value1);},
                       // minValue: new Date(getCmp('startdate')),//Ext.Date.add(new Date(), Ext.Date.DAY, 7) ,
                        afterLabelTextTpl: required,
                        name             : 'ToDate',

//                  change: function() {
//                        value = this.getValue('startdate')
//    //var value = this.getValue('startdate');
//   console.log(value);
//    //this.picker.setValue(Ext.isDate(value) ? value : new Date());
//},
                        allowBlank       :false,
                        afterLabelTextTp1:required
                    },{
                        xtype:'textfield',
                        fieldLabel       : 'Number of days',
                        labelWidth:'130px',
                        id: 'textfield-field',
                        afterLabelTextTpl: required,
                        name             : 'noofdays',
                        allowBlank       :false,
                        afterLabelTextTp1:required
                       },
                       { 
//                        xtype: 'multiselect',
//                        labelWidth:'130px',
//                        fieldLabel: 'Reporting Manager(s)',
//                        name: 'reportingmanager',
//                        id: 'multiselect-field',
//                        forceSelection : true,
//                        allowBlank: false,
//                        store: 'ReportingManager',
//                        mode: 'local',
//                        displayField: 'reportingmanager',
//                        valueField: 'value',
//                        ddReorder: true,
//                        afterLabelTextTp1:required
//                        },{
                        xtype:'textfield',
                        fieldLabel       : 'Reason',
                        labelWidth:'130px',
                        afterLabelTextTpl: required,
                        name             : 'reason',
                        allowBlank       :false,
                        afterLabelTextTp1:required
                       }]
       },{xtype:'container',width:'300px'},
               {
            xtype:'container',
            items:[{xtype:'leaveleavestatusgrid',
             store: 'LeaveRequest',
             height: 200,
             width: 500

                 }]

                       }]
       },{xtype:'fieldset',
             title:'Leave History',            
             items:[{xtype:'leaveleavehistorygrid',
             store: 'LeaveRequest',
             height: 300,
             width: '100%'

                 }]
                       }];

        this.callParent(arguments);
    }
});

1 个答案:

答案 0 :(得分:0)

  

我的json有一点变化,花括号被方括号替换

{
    "EmployeeLeaves": [
        {
            "ID": 0,
            "LeaveHistory": {
                "Duration": 2,
                "ApprovedBy": "shyam"
            },
            "PL": "'I don't care!' said the bear"
        }
    ]
}
  

我的模型已改为

Ext.define('AM.model.EMPLeaveRequest', {
    extend: 'Ext.data.Model',

        fields: [{
            name: 'ID'
        }, {
            name: 'Duration',
            mapping: 'LeaveHistory.Duration'
        }, {
            name: 'ApprovedBy',
            mapping: 'LeaveHistory.ApprovedBy'
        }],

        proxy: {
            type: 'ajax',
            batchActions: false,
            url: 'data/employeeleave.json',
            autoAppendParams: false,
            reader: {
                type: 'json',
                root: 'EmployeeLeaves',
                useSimpleAccessors: false
            }
        }

});
  

我的商店

Ext.define('AM.store.EMPLeaveRequest', {
    extend: 'Ext.data.Store',
    model: 'AM.model.EMPLeaveRequest',
        autoLoad: true//,


});

不需要使用关联....但只适用于一组数据:(