jqGrid 多行数据修改并一起提交

 var lastsel;var list=[];
    $(function () {
        $("#gridTable").jqGrid({
            url: '@Url.Action("GetQuotesById", "MaterialGather")',
            postData: { id: getId() },
            datatype: 'json',
            mtype: 'get',
            rowNum: 10,
            rowList: [10, 20, 30],
            colNames: ["ID", "序号", "材料编码", "材料名称", "单位", "规格", "报价", "备注"],
            colModel: [
                        { name: 'mgtd_ID', align: 'center', index: 'mgtd_ID', hidden: true },
                        { name: '', index: '', width: 50, align: 'center', formatter: function (cellvalue, options, rowObject) {
                            return options.rowId;
                        }
                        },
                        { name: "mltp_Code", align: 'center', index: "mltp_Code", width: 150 },
                        { name: "mltp_Name", align: 'center', index: "mltp_Name", width: 150 },
                        { name: "mltp_Unit", align: 'center', index: "mltp_Unit", width: 80 },
                        { name: "mltp_Norm", align: 'center', index: "mltp_Norm", width: 80 },
                         { name: "mgtd_Price", align: 'center', index: "mgtd_Price", width: 80,editable: true },
                         { name: "mltp_Desc", align: 'center', index: "mltp_Desc", width: 80 }
                       ],
            jsonReader: {
                root: "rows",
                repeatitems: false
            },
               onSelectRow: function(id) {
               loadList();
    if (id && id !== lastsel) {
        jQuery('#gridTable').jqGrid('editRow', id, true);
        lastsel = id;
    }
},
            autowidth: true,
            loadonce: false, //如果为ture则数据只从服务器端抓取一次,之后所有操作都是在客户端执行,翻页功能会被禁用
            //            sortname: 'mgtk_CreateTime', //默认的排序列
            //            sortorder: 'desc',
            pager: "#gridPager", //定义翻页用的导航栏,必须是有效的html元素
            viewrecords: true, //定义是否要显示总记录数
            caption: "价格回复", //表格名称
            cellEdit:false
            //multiselect: false

        }); //jqGrid End

        //列表样式调整
        resizeGrid();
        tabState();
        $.ajax({
            type: 'get',
            url: '@Url.Action("GetGatherTaskDetailsByIdt", "MaterialGather")/' + getId(),
            success: function (data) {
                $("#proName").val(data.task.mgtk_Name);
            }
        });
        function getId() {
            var a = String(location.href);
            var index = a.lastIndexOf('/');
            var id = a.substring(index + 1);
            return id;
        }

        function resizeGrid() {
            var windowHeight = document.documentElement.clientHeight;
            var windowWidth = document.documentElement.clientWidth;

            var gridTableTopHeight = $("#gridTable").offset().top; //列表距离顶部的距离
            $("#gridTable").jqGrid().setGridHeight(parseInt(windowHeight) - parseInt(gridTableTopHeight) - 30);
            $("#gridTable").jqGrid().setGridWidth(parseInt(windowWidth) - 2);
        }

        window.onresize = function () {
            //1159 1352 1142
            resizeGrid();
        }
        $("#btnPriceDet").click(function () {
           window.location.href="@Url.Action("Detection", "MaterialGather")/"+getId();
        });
        $(":radio").change(function(){
              tabState();
        });
        
        $("#btnExport").click(function(){
        });
        $("#fileGuide").click(function(){
           progressbar();
        });

       $("#btnSaveDraft").click(function() {
    var che = $("#radSys").attr("checked");
    if (che == "checked") {
        var str = getStr();
        $.ajax({
            type: 'get',
            url: '@Url.Action("SaveDraft", "MaterialGather")',
            data:{"str":str},
            success: function(data) {
                   if(data=="Success"){
                     alert("提交成功!");
                   }else if(data=="Fail"){
                      alert("请填写报价!");
                   }else{
                     alert("提交失败!");
                   }
            }
        });
    }
});

       $("#btnSubmit").click(function() {

    var che = $("#radSys").attr("checked");
    if (che == "checked") {
        var rowid = $("#gridTable").getGridParam("selrow");  
        lastsel=rowid;
        loadList();
        console.dir(list);
        return false;
        var str = getStr();
        $.ajax({
            type: 'get',
            url: '@Url.Action("SaveSubmit", "MaterialGather")',
            data:{"str":str},
            success: function(data) {
                if (data == "Success") {
                    alert("提交成功!");
                } else if (data == "Fail") {
                    alert("请填写报价!");
                } else {
                    alert("提交失败!");
                }
            }
        });
    }
});

    });

    function getStr(){
       var str="";
       for(var i=0;i<list.length;i++){
          var id=list[i].mid;
          var price=list[i].price;
          str+=id+"$"+price+",";
       }
        str=str.substring(0,str.length-1);
        return str;
    }
    function loadList(){
            $("#gridTable").saveRow(lastsel);
    var rowData = $("#gridTable").getRowData(lastsel);
    var mgtd_ID = rowData.mgtd_ID;
    var mgtd_Price = rowData.mgtd_Price;
    if (isNaN(mgtd_Price)) {
        jQuery('#gridTable').jqGrid('setRowData', lastsel,{"mgtd_Price":""});
        mgtd_Price = "";
    }
    $("#gridTable").saveRow(lastsel);

    if (mgtd_Price) {
        if (mgtd_Price != "") {
            for (var i = 0; i < list.length; i++) {
                var mid = list[i].mid;
                if (mgtd_ID == mid) {
                    list.splice(i, 1);
                }
            }
            var obj = {
                mid: mgtd_ID,
                price: mgtd_Price
            };
            list.push(obj);
        }
    }
    }

你可能感兴趣的:(jqGrid)