在javascript中使用一个保存按钮保存多个表行

时间:2017-12-29 21:07:07

标签: javascript jquery arrays datatables save

我使用以下代码使用保存按钮

在数据表中保存行
  $(document).on('click','.updateOrder', function(){
            var thisRow = $(this).closest('tr');
            var rejectReasons = { PersistRejectReasons :[] }
            var jsonReturnObj = new Object();

            jsonReturnObj.OrderNumber = thisRow.find('.dt_orderNo').text();
            jsonReturnObj.OrderLineNumber = thisRow.find('.dt_orderLineNo').text();
            jsonReturnObj.RejectReasonCode = thisRow.find('.dt_researchOutcome').find("option:selected").prop("value");
            jsonReturnObj.RejectReasonNotes = thisRow.find('.dt_researchNotes').find('input').val();

            if(jsonReturnObj.RejectReasonCode != "" && jsonReturnObj.RejectReasonCode != null) {
                if(jsonReturnObj.RejectReasonCode =="14. Other") {
                    if(jsonReturnObj.RejectReasonNotes == "" || jsonReturnObj.RejectReasonNotes == "null") {
                        alert(" Please add Research Notes");
                        jsonReturnObj.Processed = false;
                        $('#orderReportTable').append("call failed");
                        throw new Exception("Error message");
    }
                }
                jsonReturnObj.Processed = true;
            }else {
                jsonReturnObj.Processed = false;
            }

            if($("#movedInput"+jsonReturnObj.OrderNumber+"-"+jsonReturnObj.OrderLineNumber).is(':checked')){
                jsonReturnObj.IsAvailable = false;
            }else{
                jsonReturnObj.IsAvailable = true;
            }

            rejectReasons.PersistRejectReasons.push(jsonReturnObj);
            var jsonReturnString = JSON.stringify(rejectReasons);
            console.log("Rejecting Store Number is "+$("#storeNum").val())

            var request2 = $.ajax({
                headers: {
                    'RejectingStoreNum': $("#storeNum").val(), //thisRow.find('.dt_storeNo').text(), //thisRow.find("td").eq(16).text(),
                    'Content-Type': 'application/json'
                },
                type: "POST",

                url: "persistStoreResearchOutcome", // REST endpoint. replace url1 with REST Endpoint
                data : jsonReturnString,//json data. use data and contentType attributes for POST requests.
                contentType: "application/json",
                dataType: "text",
                /*xhrFields: {
                    'withCredentials': true //Tell browser to provide credentials
                },*/
                crossDomain: true,// this is for cross domain requests*/
                success: function (data, status, jqXHR){
                    console.log("status:"+status);
                    $('#orderReportTable').append("call successful");
                    if(jsonReturnObj.Processed){
                        thisRow.find('.dt_itemStatus').text("Researched");
                        table.draw();
                    }else{
                        thisRow.find('.dt_itemStatus').text("Active");
                        table.draw();
                    }
                },
                error: function (jqXHR, status, errorThrown){
                    //console.log("failed: status:"+status);
                    $('#orderReportTable').append("call failed, error"+errorThrown);
                    alert("There was an error while trying to save this item");
                }
            });
        });

我想进行相同的处理,但只需单击一下就可以对表中的所有行进行处理。我使用每个函数循环遍历行,但不断出现错误

$("table tr").each(function (){
                console.log(this);
                var thisRow = $(this).closest('td');
                var rejectReasons = { PersistRejectReasons :[] }
                var jsonReturnObj = new Object();

                jsonReturnObj.OrderNumber = thisRow.find('.dt_orderNo').text();
                jsonReturnObj.OrderLineNumber = thisRow.find('.dt_orderLineNo').text();
                jsonReturnObj.RejectReasonCode = thisRow.find('.dt_researchOutcome').find("option:selected").prop("value");
                jsonReturnObj.RejectReasonNotes = $(this).find('.dt_researchNotes').find('input').val();

                if(jsonReturnObj.RejectReasonCode != "" && jsonReturnObj.RejectReasonCode != null) {
                    if(jsonReturnObj.RejectReasonCode =="14. Other") {
                        if(jsonReturnObj.RejectReasonNotes == "" || jsonReturnObj.RejectReasonNotes == "null") {
                            alert(" Please add Research Notes");
                            jsonReturnObj.Processed = false;
                            $('#orderReportTable').append("call failed");
                            throw new Exception("Error message");
                        }
                    }
                    jsonReturnObj.Processed = true;
                }else {
                    jsonReturnObj.Processed = false;
                }

                if($("#movedInput"+jsonReturnObj.OrderNumber+"-"+jsonReturnObj.OrderLineNumber).is(':checked')){
                    jsonReturnObj.IsAvailable = false;
                }else{
                    jsonReturnObj.IsAvailable = true;
                }

                rejectReasons.PersistRejectReasons.push(jsonReturnObj);
                var jsonReturnString = JSON.stringify(rejectReasons);
                console.log("Rejecting Store Number is "+$("#storeNum").val())

                var request2 = $.ajax({
                    headers: {
                        'RejectingStoreNum': $("#storeNum").val(), //thisRow.find('.dt_storeNo').text(), //thisRow.find("td").eq(16).text(),
                        'Content-Type': 'application/json'
                    },
                    type: "POST",

                    url: "persistStoreResearchOutcome", // REST endpoint. replace url1 with REST Endpoint
                    data : jsonReturnString,//json data. use data and contentType attributes for POST requests.
                    contentType: "application/json",
                    dataType: "text",
                    /*xhrFields: {
                        'withCredentials': true //Tell browser to provide credentials
                    },*/
                    crossDomain: true,// this is for cross domain requests*/
                    success: function (data, status, jqXHR){
                        console.log("status:"+status);
                       // $('#orderReportTable').append("call successful");
                        if(jsonReturnObj.Processed){
                            this.find('.dt_itemStatus').text("Researched");
                            table.draw();
                        }else{
                            this.find('.dt_itemStatus').text("Active");
                            table.draw();
                        }
                    },
                    error: function (jqXHR, status, errorThrown){
                        //console.log("failed: status:"+status);
                        $('#orderReportTable').append("call failed, error"+errorThrown);
                        alert("There was an error while trying to save this item");
                    }
                });
            });

        }); 

我该如何解决这个问题? 错误消息是

http://localhost:8080/persistStoreResearchOutcome 500 ()
send @ jquery-3.2.1.min.js:4
ajax @ jquery-3.2.1.min.js:4
(anonymous) @ (index):544
each @ jquery-3.2.1.min.js:2
each @ jquery-3.2.1.min.js:2
(anonymous) @ (index):509
dispatch @ jquery-3.2.1.min.js:3
q.handle @ jquery-3.2.1.min.js:3

我尝试了不同的方法来循环遍历表,包括来自datatables的every()函数,但是不断得到相同的错误

0 个答案:

没有答案