ext js 6将动态id从网格面板传递给模型

时间:2015-11-19 01:33:06

标签: javascript extjs parameters model

如何将id从gridpanel传递给模型? 这里是我的网格面板代码:

{
     text     : 'Tindakan',
     xtype: 'actioncolumn',
     minWidth: 130,
     sortable: false,
     menuDisabled: true,
     items: [{
                icon: 'images/view.png',  // Use a URL in the icon config
                tooltip: 'Lihat',

                handler : function(grid, rowIndex, colIndex) {
                var rec = grid.getStore().getAt(rowIndex);
                alert("Lihat -  " + rec.get('id'));

     }]
}

这是我的型号代码:

Ext.define('Kds.model.ProfilView', {
    extend: 'Ext.data.Store',

    alias: 'model.profilView',

    fields: [
        'name', 
        'ic_no',
        'address',
    ],

    pageSize : 20,
    proxy: {
        type: 'rest',
        url : 'http://localhost/kds-rest/web/index.php/people/view/'+id,
        useDefaultXhrHeader : false,
        withCredentials: false,
        reader: {
            type: 'json',
            rootProperty: 'data',
            //totalProperty: 'totalItems'

        }
    },
    autoLoad: 'true',
});

1 个答案:

答案 0 :(得分:0)

你如何使用该模型?如果您每次都创建或使用它,您可以尝试:

handler : function(grid, rowIndex, colIndex) {
    var rec = grid.getStore().getAt(rowIndex);
    //Create or get model to use 
    var model = Ext.create('Kds.model.ProfilView', {
      // then give the record id to model 
      recordId: rec.get('id') // edited
    }); 
}



 // to use in model
   Ext.define('Kds.model.ProfilView', {
     extend: 'Ext.data.Store',
     alias: 'model.profilView',
fields: [
    'name', 
    'ic_no',
    'address'
],
pageSize : 20,
autoLoad: 'true',
     initComponent: function() {
     var me = this;
     me.proxy = {
        type: 'rest',
        url :      'http://localhost/kdsrest/web/index.php/people/view/'+me.recordId, // or this.up().recordId,
         ................
     } // iniComponent
   me.callParent(); 

编辑:如何动态加载模型:小提琴:https://fiddle.sencha.com/#fiddle/11g9

Ext.define('model.instance', {
    extend: 'Ext.data.Model',
    fields: ['name', 'city', 'country'],
    proxy: {
        type: 'ajax', 
        url: 'info',
        reader: {
            type: 'json',
            rootProperty: 'records'
        }
    }
});

var fn = function getSelectedRow(gridView, rowIndex, colIndex, column, e,direction) {
        var me = this;
        var store = gridView.getStore();
        var record = store.getAt(rowIndex);
        var inst = Ext.create('model.instance', {
            name: record.get('name')
        });
        inst.load({
            scope: this,
            success: function(rec) {
                console.log(rec);
            }
        });
}

var store1 = Ext.create('Ext.data.Store', {
    fields: ['name', 'surname'],
    data: [{
        name: 'Rick',
        surname: 'Donohoe'
    }, {
        name: 'Jane',
        surname: 'Cat'
    }]
});
var grid = Ext.create('Ext.grid.Panel', {
    columns: [{
        dataIndex: 'name'
    }, {
        dataIndex: 'surname'
    }, {
      xtype: 'actioncolumn',
      text: 'Select',
      icon: '/image',
      handler: Ext.bind(fn, this)
    }],
    store: store1,
    renderTo: Ext.getBody()
});