2012-09-27 45 views
0

我正在嘗試創建網格構建器對象。該網格生成器有一個方法,buildGrid,我已經設計期望定義一堆PARAMATERS的一格的對象:使用必需和可選參數構建對象

buildOrdersGrid: function() { 
    var ordersGrid = buildGrid({ 
     gridElementID: 'OrdersGrid', 
     gridPagerElementID: 'OrdersGridPager', 
     colNames: ['Order ID', 'Project Subcode', 'Incident Number', 'Cost Center', 'Name', 'Customer'], 
     colModel: [ 
      { name: 'ID', hidden: true }, 
      { name: 'ProjectSubcode' }, 
      { name: 'IncidentNumber' }, 
      { name: 'CostCenter' }, 
      { name: 'Name' }, 
      { name: 'Customer' } 
     ], 
     defaultCaption:'Orders: no filter applied', 
    }); 
    return ordersGrid; 
} 

function buildGrid(data) { 
    var grid = $('#' + data.gridElementID); 
    var gridPager = $('#' + data.gridPagerElementID); 

    grid.jqGrid({ 
     datatype: 'local', 
     colNames: data.colNames, 
     colModel: data.colModel, 
     gridview: true, 
     height: 'auto', 
     pager: gridPager, 
     viewrecords: true, 
     multiselect: true, 
     defaultCaption: data.defaultCaption, 
     caption: data.defaultCaption, 
     shrinkToFit: false 
    }); 

    return grid; 
} 

類似的東西,但它是真正的新代碼,就如何使開放的意見改善。

現在,我想擴展此buildGrid方法,以便它可以採取非預定義的屬性並將它們提供給jqGrid。例如:

buildTaskGrid: function() { 
    var tasksGrid = buildGrid({ 
     gridElementID: 'TasksGrid', 
     gridPagerElementID: 'TasksGridPager', 
     colNames: ['Order', 'Task ID', 'Task #', 'Type', 'Status', 'Assignee', 'Current Location', 'Dest Location', 'Change No', 'Net Patched', 'SAN Patched'], 
     colModel: [ 
      { name: 'Order' }, 
      { name: 'TaskID', hidden: true }, 
      { name: 'TaskNo' }, 
      { name: 'Type' }, 
      { name: 'Status' }, 
      { name: 'Assignee' }, 
      { name: 'CurrentLocation' }, 
      { name: 'DestLocation' }, 
      { name: 'ChangeNo' }, 
      { name: 'NetPatched' }, 
      { name: 'SANPatched' } 
     ], 
     defaultCaption:'Tasks: no filter applied', 
     //Decorate with task-specific properties. 
     grouping: true, 
     groupingView: { 
      groupField: ['Order'], 
      groupColumnShow: [false] 
     }, 
     ondblClickRow: function (rowid) { 
      $(this).trigger('taskDoubleClicked', selector.getRowData(rowid)); 
     } 
    }); 

    return tasksGrid; 
} 

我不知道我應該如何最好地「發現」無法預料的屬性並將它們提供給網格。有任何想法嗎?

回答

相關問題