包含数据的 Init 表仅适用于断点

Init table with data works only with breakpoint

本文关键字:适用于 断点 Init 数据 包含      更新时间:2023-09-26

我用我使用 ajax 获得的数据初始化表(使用这个 lib 引导表)。

var arr = [];
        var getRows = function () {
            $.ajax({
                type: "GET",
                url: hostUrl,
                contentType: "application/json;",
                dataType: "json",
                success: function (result) {
                    arr = result.d;
                }
            });
            return arr; // breakpoint here
        };
$('#bootstrap-table').bootstrapTable({
            data: getRows()
});

仅当我在 getRows 函数中设置返回断点时,此代码才有效。尝试在返回之前添加超时 - 但这没有帮助。

如果我不添加断点,我什么也得不到。

ajax 调用是异步的。

在呼叫返回之前返回 arr。

你最好选择这样的东西:

        $.ajax({
            type: "GET",
            url: hostUrl,
            contentType: "application/json;",
            dataType: "json",
            success: function (result) {
                $('#bootstrap-table').bootstrapTable({
                 data: result.d
                });
            }
        });
查询

中缺少异步参数

$.ajax({
            type: "GET",
            url: hostUrl,
            async: false, //This is the guy you want !!!!!!!!!!!!!
            contentType: "application/json;",
            dataType: "json",
            success: function (result) {
                arr = result.d;
            }
        });
实际上,

你最好使用承诺。只需返回 $.ajax promise 并处理该函数外部返回的数据:

// GetRows promises to return rows of data...
var getRows = function () {
    // simply return the ajax promise
    return $.ajax({
        type: "GET",
        url: hostUrl,
        contentType: "application/json;",
        dataType: "json",
    });
};

并像这样消费:

// Use the returned promise to process the data
getRows().done(function(rows){
    $('#bootstrap-table').bootstrapTable({
        data: rows
    });
});

回调或success:处理程序的问题在于,您将 GUI 依赖项构建到 GetRows 方法中。