2011-02-04 44 views
0

我查看哪個(簡化版的真實屏幕)有幾個Drop Down控件,允許用戶選擇一個Category,然後是一個Sub Category - 輸入一個數量,然後點擊一個'添加「按鈕。 Add按鈕然後向JQuery Grid添加一個新行(enter link description here)。使用jquery grid的ASP.Net MVC

然後,控件重置,並允許用戶選擇另一個類別,子類別和數量,然後再次單擊添加,將數據添加到網格。

 $(function() { 
     $('#addSplit').click(function() { 
      var mydata = [ 
          { category: $('#SelectedCategoryId option:selected').text(), subcategory: $('#SelectedSubCategoryId option:selected').text(), costcenter: $('#SelectedCostCenterId option:selected').text(), budget: $('#SelectedBudgetId option:selected').text(), amount: $('#amount').val() } 
         ]; 

      for (var i = 0; i <= mydata.length; i++) 
       jQuery("#list4").jqGrid('addRowData', i + 1, mydata[i]); 
     }); 
    }); 

行正在被添加好。 (我需要添加隱藏的列來以某種方式存儲ID)。

然後,用戶將點擊'保存'。我想以某種方式在網格中循環訪問,獲取(將被添加的)id,並以某種方式將它返回到我的MVC控制器中進行存儲。

可以這樣做嗎?或者有什麼我想要做的更好的主意?

回答

1

你必須定義你的colModel。 我會定義接收數據的陣列功能,並結合它(數據:DataToLoad)

function LoadGrid(DataToLoad)  { 
    jQuery("#list4").jqGrid({ 
     data: DataToLoad, 
     datatype: "local", 
     width: 790, 
      height: 250, 
     rowNum: 999999, 
     colNames:['Inv No','Date', 'Client', 'Amount','Tax','Total','Notes'], 
     colModel:[ 
      {name:'id',index:'id', hidden:true, sorttype:"int"}, 
      {name:'invdate',index:'invdate', width:90, sorttype:"date", formatter:"date"}, 
      {name:'name',index:'name', width:100}, 
      {name:'amount',index:'amount', width:80, align:"right",sorttype:"float", formatter:"number"}, 
      {name:'tax',index:'tax', width:80, editable: true, align:"right",sorttype:"float"},  
      {name:'total',index:'total', width:80,align:"right",sorttype:"float"},  
      {name:'note',index:'note', width:150, sortable:false}  
     ], 
     emptyrecords: "No records to view", 
     cellEdit: true, 
     cellsubmit: 'clientArray', 
     viewrecords: true, 
     shrinkToFit: false, 
     scroll: false, 
     rownumbers: true, 
     hidegrid: false, 
     pager: "#plist47", 
     caption: "Manipulating Array Data" 
    }); 
} 

正如你所看到的第一列是隱藏的:真正的

然後,你可以打電話給你的函數數組加載,而不是添加行到網格:

$("#addSplit").bind('click', function(){ 
    jQuery("#list4").GridUnload(); 
    // LOAD the ARRAY here. 
    LoadGrid(mydata); 
}); 

記住@vandalo卸載電網jQuery("#list4").GridUnload();

+0

謝謝 - 請問當我點擊我的保存按鈕(這是提交模型返回到控制器的提交按鈕)時,我可以使用數組嗎? – Craig 2011-02-05 23:05:41