0

在我的项目中,我使用 Setinterval 来显示 excelsheet 到数据库的导入状态。即,如果 excelsheet 包含 100 条记录意味着我的状态消息是,第 1 行 0f 100 行已插入第 2 行,共 100 行已插入。

但是,如果该行包含任何空值意味着我需要获取错误详细信息并将其添加到状态消息中。所以它适用于一些问题。

我的javascript函数如下。

 function ImportFormSuccess(taskId) {
        endform();
    Tid = taskId;

            try {
                intervalId = setInterval(function () {
                    $.ajax(
                    {
                        type: "POST",
                        url: rootDir + "Import/Progress",
                        data: { id: Tid },
                        success: function (data) {

                            if (data.split(',').length > 1) {
                                ErrorMessage = ErrorMessage + data.split(',')[1] + "<br/>";
                                updateMonitor();
                            }
                            else {
                                Message = data;
                                updateStatus();
                            }
                        }
                    });
                }, 100);

            }
            catch (err) {
                txt = "Error Description" + err.Message + "</br>";
                txt += "Click Ok to Continue. . .";
                alert(txt);
            }

        function updateMonitor() {
            $('#monitors').attr("class", "");
            $("#monitors").html(Message + "<br/>" + ErrorMessage);
        }

        function updateStatus() {
            $('#status').attr("class", "");
            $("#status").html(Message);

        }

查看代码是:

<div id="monitors" style="padding: 2px;width:500px;height:150px;overflow:auto;border:1px solid;></div>

如果我运行项目并启动导入装置,它会显示以下内容:

Row 5 Insert Failed. Row Contains Empty Value
Row 5 Insert Failed. Row Contains Empty Value
Row 5 Insert Failed. Row Contains Empty Value
Row 5 Insert Failed. Row Contains Empty Value
Row 7 Insert Failed. Row Contains Empty Value
Row 7 Insert Failed. Row Contains Empty Value
Row 7 Insert Failed. Row Contains Empty Value
Row 7 Insert Failed. Row Contains Empty Value
Row 10 Insert Failed. Row Contains Empty Value
Row 10 Insert Failed. Row Contains Empty Value
Row 10 Insert Failed. Row Contains Empty Value
Row 10 Insert Failed. Row Contains Empty Value
Row 15 Insert Failed. Row Contains Empty Value
Row 15 Insert Failed. Row Contains Empty Value
Row 15 Insert Failed. Row Contains Empty Value

但我只需要打印一次......即,

Row 5 Insert Failed. Row Contains Empty Value
Row 7 Insert Failed. Row Contains Empty Value
Row 10 Insert Failed. Row Contains Empty Value
Row 15 Insert Failed. Row Contains Empty Value

有什么帮助吗???

4

3 回答 3

1
globalVar = "";

Message = data;
                         var rowVal = data.match(/((-?\d+))/)[1];

                         if(globalVar == "")
                         {
                             globalVar = rowVal;
                             updateStatus();
                         }
                         else 
                         {
                             if(globalVar != rowVal)
                             {
                                 updateStatus();
                             }
                         }

像这样试试

于 2013-05-28T12:27:01.387 回答
0

Finally i fixed this issue.

I use match function to check whether thar errormessage is already exist or not?

 if (data.split(',').length > 1) {
     if (!ErrorMessage.match(data.split(',')[1])) {
           $("#monitors").show();
           ErrorMessage = ErrorMessage + data.split(',')[1] + "<br/>";
           updateMonitor();
           }
     }

Thanks for all who is helped me a lot for my question.. Special Thanks to @Anna.P, @TallMaris, @VitorCanova

于 2013-05-29T08:35:37.967 回答
0

根据评论,我会尝试这样的事情:

function ImportFormSuccess(taskId) {
    endform();
    Tid = taskId;

    try {
        callServer();
    }
    catch (err) {
        // ...snip...
    }
}

function callServer() {
    $.ajax({
        type: "POST",
        url: rootDir + "Import/Progress",
        data: { id: Tid },
        success: function (data) {
            // your code here
        },
        complete: function () {
            // if not 100% (or something similar)
            setTimeout(function () { callServer(); }, 100)
        }
    });
}
于 2013-05-28T12:48:28.753 回答