Devexpress MVC Gridview 获取到增删改的所有行数据(JSON) 并使用SQL事物保存数据
//ModalChargeGridView Gridview的名字
//Con_Shp_Chg 数据库表名
//ConShpChgUID UID或者是标识列
//gs_Language 语言(中英文)
//ljs_HeaderWhereJson where的条件. 一般用于主表跟子表
GetChargeUpdateDataJson(ModalChargeGridView,"Con_Shp_Chg","ConShpChgUID", gs_UsrID, gs_Language, ljs_HeaderWhereJson);
//Get Update Data function GetChargeUpdateDataJson(agv_GridView, as_KeyTable, as_KeyField, as_UsrID, as_Language, ajs_HeaderUpdateWhereJson) { var ls_json = ""; //var la_array = []; var ls_jsonResult = ""; var ls_Wherejson = ""; var ls_Option = ""; var ls_Value =""; var ls_Type = ""; var la_Indicies = agv_GridView.batchEditHelper.GetDataItemVisibleIndices(); var li_ColumnCount = agv_GridView.GetColumnsCount(); var ls_FieldName = ""; var la_NotUpdateField = []; for (var i = 0,gi_len = la_Indicies.length; i < gi_len ; i++) {
//Delete if(agv_GridView.batchEditApi.IsDeletedRow(la_Indicies[i]) && agv_GridView.batchEditApi.GetCellValue(la_Indicies[i], as_KeyField) != null) { ls_json += GenerateJsonStringIfy("ModifyDate","-1","DateTime"); ls_json += GenerateJsonStringIfy("ModifyBy",as_UsrID,"String"); ls_json += GenerateJsonStringIfy("Status","-1","String"); ls_json = "["+ ls_json.substring(0, ls_json.length - 1)+"]"; ls_Option = "UPDATE"; ls_Wherejson = GenerateJsonStringIfy(as_KeyField, agv_GridView.batchEditApi.GetCellValue(la_Indicies[i],as_KeyField),"Int"); ls_Wherejson = "["+ ls_Wherejson.substring(0, ls_Wherejson.length - 1)+"]"; }
//Update else if (agv_GridView.batchEditApi.HasChanges(la_Indicies[i]) && !(agv_GridView.batchEditApi.IsNewRow(la_Indicies[i])) && agv_GridView.batchEditApi.GetCellValue(la_Indicies[i], as_KeyField) != null) { if (agv_GridView.batchEditApi.ValidateRow(la_Indicies[i])) { for (var j = 0; j < li_ColumnCount; j++) { ls_FieldName = agv_GridView.GetColumn(j).fieldName; if (ls_FieldName === "" ||la_NotUpdateField.indexOf(ls_FieldName) !== -1) { continue; } else { if (ls_FieldName === "ModifyBy") { ls_Value = as_UsrID ; } else if (ls_FieldName === "RowNo") ls_Value = (la_Indicies[i] + 1).toString() ; else { if (agv_GridView.batchEditApi.GetCellValue(la_Indicies[i], ls_FieldName) === null) ls_Value = ""; else ls_Value = agv_GridView.batchEditApi.GetCellValue(la_Indicies[i], ls_FieldName).toString().trim(); } switch (ls_FieldName) { case 'ts': ls_Type = 'TimeStamp'; break; case as_KeyField: ls_Type = 'UID'; break; default: ls_Type = 'String'; } } ls_json += GenerateJsonStringIfy(ls_FieldName,ls_Value,ls_Type); } ls_json += GenerateJsonStringIfy("ModifyDate","-1","DateTime"); ls_json += GenerateJsonStringIfy("Language",as_Language,"String"); ls_json = "["+ ls_json.substring(0, ls_json.length - 1)+"]"; ls_Option = "UPDATE"; ls_Wherejson = GenerateJsonStringIfy(as_KeyField, agv_GridView.batchEditApi.GetCellValue(la_Indicies[i],as_KeyField),"Int"); ls_Wherejson = "["+ ls_Wherejson.substring(0, ls_Wherejson.length - 1)+"]"; } } //Insert else if ((!agv_GridView.batchEditApi.IsDeletedRow(la_Indicies[i]) && agv_GridView.batchEditApi.IsNewRow(la_Indicies[i])) || agv_GridView.batchEditApi.GetCellValue(la_Indicies[i], as_KeyField) == null) { if (agv_GridView.batchEditApi.ValidateRow(la_Indicies[i]) && !agv_GridView.batchEditApi.IsDeletedRow(la_Indicies[i])) { ls_json += ''; for (var j = 0; j < li_ColumnCount; j++) { ls_FieldName = agv_GridView.GetColumn(j).fieldName; if (ls_FieldName === "" || ls_FieldName === "ModifyDate" || ls_FieldName === "ModifyBy" || la_NotUpdateField.indexOf(ls_FieldName) !== -1) { continue; } else { if (ls_FieldName === "CreateBy") ls_Value = as_UsrID ; else if (ls_FieldName === "RowNo") ls_Value = (i+1).toString(); else if (ls_FieldName === "QVSHPTUID") ls_Value = 'QVSHPTUID'; else { if (agv_GridView.batchEditApi.GetCellValue(la_Indicies[i], ls_FieldName) === null) ls_Value = null; else ls_Value = agv_GridView.batchEditApi.GetCellValue(la_Indicies[i], ls_FieldName).toString().trim(); } switch (ls_FieldName) { case "QVSHPTUID": ls_Type = 'Custom'; break; case 'CreateDate': ls_Type = 'DateTime'; break; case 'ModifyDate': ls_Type = 'DateTime'; break; case 'ts': ls_Type = 'TimeStamp'; break; case as_KeyField: ls_Type = 'UID'; break; default: ls_Type = 'String'; break; } } ls_json += GenerateJsonStringIfy(ls_FieldName,ls_Value,ls_Type); } ls_json += GenerateJsonStringIfy("Language",as_Language,"String"); ls_json = "["+ ls_json.substring(0, ls_json.length - 1)+"]"; ls_Option = "INSERT"; ls_Wherejson = ajs_HeaderUpdateWhereJson; } } ls_jsonResult += GenerateSQLJsonStringIfy(as_KeyTable, ls_json, ls_Wherejson, ls_Option); ls_json = ""; ls_Wherejson = ""; } return ls_jsonResult; }
function GenerateSQLJsonStringIfy(as_Label, as_Data, as_Where, as_Option) { var ljs_json = ""; //ac_quote = ac_quote == null ? "" : ac_quote; if (as_Data !== null && as_Data !== "") { if (typeof as_Data == "object" && as_Data.constructor == Array) { if (as_Data.length == 0 || as_Data[0] == "") return ljs_json; } ljs_json = '{'; ljs_json += '"Label":"' + as_Label + '",'; ljs_json += '"Data":' + JSON.stringify(as_Data) + ','; ljs_json += '"Where":' + JSON.stringify(as_Where) + ','; ljs_json += '"Option":"' + as_Option + '"'; ljs_json += '},'; } return ljs_json; } function GenerateJsonStringIfy(as_Label, as_Value, as_Type) { var ljs_json = ""; if (as_Value !== null && as_Value !== "") { if (typeof as_Value == "object" && as_Value.constructor == Array) { if (as_Value.length == 0 || as_Value[0] == "") return ljs_json; } if (as_Label !== null && as_Label !== "") { var lo_nap = { Label: as_Label, Value: as_Value.toString(), Type: as_Type }; ljs_json += JSON.stringify(lo_nap) + ','; } } return ljs_json; }