1

我正在开发一个打印页面,我们必须在其中显示所有报告图表。

每个客户的图表数量都不同。因此,我正在为每个可用图形编写 jquery 脚本,如下所示:

buildJQs: function () {

        $(".emailgraphs").each(function () {
            YAHOO.Report.Print("Email", $(this).attr("responsefield"), $(this).attr("id"), $(this).attr("metricid"))
        });
    },

Print: function (name, graphid, divid, metricid) {

        try {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: m_oReport.ds,
                async: false,
                timeout: 300,
                data: JSON.stringify(m_oReport.printp(name, graphid, metricid)),
                beforeSend: function () {
                    //Displays loading image before request send, till we get response.
                    //$("#" + divId).addClass("loading");
                },
                cache: false,
                success: function (data) {
                    // if they define a success function (s), call it and return data to it.
                    if (typeof m_oReport.prints === "function") {
                        //$("#" + divId).removeClass("loading");

                        m_oReport.prints(data, divid, name, metricid)
                    }
                },
                error: function (err) {
                    $("#" + divid).html(err);
                }
            });
        }
        catch (err) { alert("catch"); }
    }

问题是,控制器返回的数据没有任何问题,但是当我将其分配给 jqplot 时,数据变为空。我认为这是由于异步 ajax 调用。我尝试使用 async: false 和 timeout 但仍然面临问题。

有没有办法处理这个??

提前致谢...

4

1 回答 1

1

用 jquery deferred 试试这个:

buildJQs: function () {

    var deferreds = [], deferred;
    $(".emailgraphs").each(function () {

        deferred = YAHOO.Report.Print("Email", $(this).attr("responsefield"), $(this).attr("id"), $(this).attr("metricid")); 
        deferreds.push(deferred);
     });

     $.when.apply(null, deferreds).done(function() {
         alert('all done');
     });
},

Print: function (name, graphid, divid, metricid) {

        try {
           return $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: m_oReport.ds,
                //async: false, <- remove, this is a problem
                timeout: 300,
                data: JSON.stringify(m_oReport.printp(name, graphid, metricid)),
                beforeSend: function () {
                    //Displays loading image before request send, till we get response.
                    //$("#" + divId).addClass("loading");
                },
                cache: false,
                success: function (data) {
                    // if they define a success function (s), call it and return data to it.
                    if (typeof m_oReport.prints === "function") {
                        //$("#" + divId).removeClass("loading");

                        m_oReport.prints(data, divid, name, metricid)
                    }
                },
                error: function (err) {
                    $("#" + divid).html(err);
                }
            });

        }
        catch (err) { alert("catch"); }
        return null; 
    }
于 2013-09-05T13:58:08.260 回答