简体   繁体   English

使用Jquery Grid的ASP.Net MVC

[英]ASP.Net MVC using Jquery Grid

I have View which (simplified version of the real screen) has a few Drop Down controls allowing the user to select a Category, then a Sub Category - enter an amount, and then click an 'Add' button. 我有一个View(它是实际屏幕的简化版本),它具有几个下拉控件,允许用户选择一个类别,然后选择一个子类别-输入金额,然后单击“添加”按钮。 The Add button then adds a new row to a JQuery Grid ( enter link description here ). 然后,添加按钮将新行添加到JQuery网格中( 在此处输入链接描述 )。

Then, the controls reset, and allow the user to select another category, sub category and amount, and then click add again, adding the data to the grid. 然后,控件将重置,并允许用户选择其他类别,子类别和数量,然后再次单击“添加”,以将数据添加到网格中。

        $(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]);
        });
    });

The rows are being added well. 行添加得很好。 (I need to add hidden columns to store the ids somehow). (我需要添加隐藏列以某种方式存储ID)。

Then, the user will click 'Save'. 然后,用户将单击“保存”。 I'd like to somehow itterate through the grid, grab the (to be added) ids, and somehow return that with the model, back to my MVC controller to store. 我想以某种方式遍历网格,获取(要添加的)ID,然后以某种方式将其返回模型,回到我的MVC控制器进行存储。

Can this be done? 能做到吗? Or is there a better idea for what I am trying to do? 还是我想做什么更好的主意?

You have to define your colModel. 您必须定义您的colModel。 I would define a function which receives an array of data, and binds it (data: DataToLoad) 我将定义一个函数,该函数接收数据数组并将其绑定(数据: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"
    });
}

As you can see the first column is hidden:true 如您所见,第一列是隐藏的:true

Then you can call your function with the array loaded, instead of adding the rows to the grid: 然后,您可以在加载数组的情况下调用函数,而不是将行添加到网格中:

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

Remember to unload the grid jQuery("#list4").GridUnload(); 记住要卸载网格jQuery("#list4").GridUnload();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM