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

时间:2020-04-09 17:15:16

标签: jquery ajax promise jquery-deferred

我已经创建了一个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)。然后没有等到函数结束工作。

1 个答案:

答案 0 :(得分:1)

您可以尝试:

  • 而不是使用简单的 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="10dp"> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" android:layout_marginBottom="8dp"/> <ScrollView android:id="@+id/nested_scrollview_in_recycleview" android:layout_width="match_parent" android:visibility="gone" android:background="@color/colorAccent" android:layout_height="200dp"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycle_view_nested" android:layout_width="match_parent" android:nestedScrollingEnabled="false" android:layout_height="wrap_content"/> </ScrollView> </LinearLayout> ,而是序列化.each()的迭代。
  • 等待data.multi返回。

您也感到:

  • 捕获和吞并$("#edit").show('slow').promise()过程中产生的错误
  • 使用终端prev_edit()捕获任何意外错误

您应该最终得到这样的东西:

.catch()

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