函数结束后循环打印检索多个数据

问题描述 投票:0回答:1

我已经创建了一个Web应用程序来存储从客户请求的报价。

一个报价可以是多个,所以当我打印一个报价时,有可能我必须打印连接的报价。

为此,我试图在完成从json检索数据并构建页面的功能后进行打印。

可能有多个数据,所以当我单击打印按钮时,该功能必须读取json数据以获取所有已连接的引号,构建页面并进行打印,然后对任何引号进行此操作

此功能从服务器读取报价数据

                        function prev_edit(id){
                            return $.ajax({
                                type        : 'POST',
                                url         : 'json/get.php?t=2&p='+id,
                                dataType    : 'json',
                                encode      : true
                            })
                            .then(function(data) {
                                var prom;
                                if ((data)["success"]===false) {
                                    ...do something...
                                    prom=$("#edit").show("slow").promise();
                                }
                                return prom;
                            });
                        };

单击打印按钮后此功能有效

                        $('#prev_print').on("click",(function( event ) {
                            $.ajax({
                                type        : 'POST',
                                url         : 'json/get.php?multi='+$("#id").val(),
                                dataType    : 'json',
                                encode      : true
                            }).then(function(data) {
                                if ((data)["success"]===true) {
                                    $.each((data)["multi"], function( i, n ) {
                                        prev_edit(n).then(function(){
                                            window.print();
                                        });
                                    });
                                }
                            });
                        }));

我也尝试了延迟的对象,但似乎prev_edit(n).then不会在函数结束工作时等待。

jquery ajax promise jquery-deferred
1个回答
0
投票

您可以尝试:

  • 而不是使用简单的.each(),而是序列化data.multi的迭代。
  • 等待返回$("#edit").show('slow').promise()

您也有灵魂:

  • prev_edit()处理引起的捕获和吞咽错误
  • 使用端子.catch()捕获任何意外错误>>
  • 您应该以如下形式结束:

function prev_edit(id) {
    return $.ajax({
        'type': 'POST',
        'url': 'json/get.php?t=2&p=' + id,
        'dataType': 'json',
        'encode': true
    })
    .then(function(data) {
        if (!data.success) {
            ...do something...
            return $("#edit").show('slow').promise();
         // ^^^^^^
        }
        throw new Error('prev_edit failed');
     // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    });
};

$('#prev_print').on('click', function(event) {
    $.ajax({
        'type': 'POST',
        'url': 'json/get.php?multi=' + $('#id').val(),
        'dataType': 'json',
        'encode': true
    })
    .then(function(data) {
        if (data.success) {
            // serialize the preview/print process with data.multi.reduce(...)
            data.multi.reduce(function(promise, n) {
                return promise.then(function() {
                    return prev_edit(n)
                    .then(function() {
                        window.print();
                    })
                    // catch possible error arising from prev_edit().
                    .catch(function(error) {
                        console.log(error);
                        // swallow (don't rethrow) the error.
                    });
                });
            }, Promise.resolve());
        }
    })
    .catch(function(error) {
        console.log(error);
        // no need to rethrow the error as this is top-level code.
    });
});

您可能需要在序列化的预览/打印过程中添加一些延迟,以便留出时间查看预览。或者,您可以提供一个按钮,需要单击该按钮才能移至下一个预览/打印。两者都很简单。

© www.soinside.com 2019 - 2024. All rights reserved.