【问题标题】:Bootstrap-Table does not render the JSON data (but loads it successfully)Bootstrap-Table 不渲染 JSON 数据(但加载成功)
【发布时间】:2016-11-25 09:00:39
【问题描述】:

我正在尝试使用 bootstrap-table 插件在表格中呈现我的 json 数据。数据加载成功,但是插件没有将数据渲染到表格中,它只是说No matching records found

我一直在关注文档中的示例,尝试使用loadrefresh 之类的方法,尽管根据the example I almost copy-pasted,您不需要使用任何方法来加载和呈现数据,您只需在table 标记中指定data-url 属性,或在您的js 文件中的表对象上添加url 属性。我尝试了两种变体,但似乎都不起作用。

这是我定义表格的方式:

<h1>Table</h1>
<div id="toolbar">
    <button id="remove" class="btn btn-danger" disabled="">
        <i class="glyphicon glyphicon-remove"></i>
        Delete
    </button>
</div>

<table
    id="table"
    data-url="/books/all"
    data-toolbar="#toolbar"
    data-search="true"
    data-sortable="true"
    data-show-refresh="true"
    data-show-toggle="true"
    data-show-columns="true"
    data-show-export="true"
    data-detail-view="true"
    data-detail-formatter="detailFormatter"
    data-minimum-count-columns="2"
    data-show-pagination-switch="true"
    data-pagination="true"
    data-id-field="id"
    data-page-list="[10, 25, 50, 100, ALL]"
    data-show-footer="false"
    data-side-pagination="server"
    data-response-handler="responseHandler">
</table>

/books/all 像这样返回 json 数据:

[{"id":42
    "name":"whatever",
"description":"whatever"
"cover_img":"https://whatever.jpg"
"available_count":10,
"price":6.99,
"author_id":21,
"publisher_id":5,
"author_first_name":"Harper",
"author_last_name":"Lee",
"author_birthday":"1926-04-27T22:00:00.000Z",
"publisher_name":"Penguin Fiction"},...]

我在 js 中定义我的列:

let $table = $('#table'),
    $remove = $('#remove'),
    selections = [];

const initTable = () => {
    $table.bootstrapTable({
        url: '/books/all',
        height: getHeight(),
        columns: [
            [
                {
                    field: 'state',
                    checkbox: true,
                    rowspan: 2,
                    align: 'center',
                    valign: 'middle'
                }, {
                    title: 'Book ID',
                    field: 'id',
                    rowspan: 2,
                    align: 'center',
                    valign: 'middle',
                    sortable: true,
                    footerFormatter: totalTextFormatter
                }, {
                    title: 'Book Detail',
                    colspan: 3,
                    align: 'center'
                }
            ],
            [
                {
                    field: 'name',
                    title: 'Book Name',
                    sortable: true,
                    editable: true,
                    align: 'center',
                    footerFormatter: totalNameFormatter
                }, {
                    field: 'price',
                    title: 'Book Price',
                    sortable: true,
                    align: 'center',
                    editable: {
                        type: 'text',
                        title: 'Book Price',
                        validate(value) {
                            value = $.trim(value);

                            if (!value) {
                                return 'This field is required';
                            }

                            if (!/^\$/.test(value)) {
                                return 'This field needs to start with $';
                            }

                            const data = $table.bootstrapTable('getData'),
                                  index = $(this).parents('tr').data('index');
                            console.log(data[index]);
                            return '';
                        }
                    },
                    footerFormatter: totalPriceFormatter
                }, {
                    field: 'operate',
                    title: 'Book Operate',
                    align: 'center',
                    events: operateEvents,
                    formatter: operateFormatter
                }
            ]
        ]
    });

    setTimeout(() => {
        $table.bootstrapTable('resetView');
    }, 200);

    $table.on('check.bs.table uncheck.bs.table ' +
        'check-all.bs.table uncheck-all.bs.table',
        () => {
            $remove.prop('disabled', !$table.bootstrapTable('getSelections').length);

            selections = getIdSelections();
    });

    $table.on('expand-row.bs.table', (e, index, row, $detail) => {
        if (index % 2 == 1) {
            $detail.html('Loading from ajax request...');
            $.get('LICENSE', res => {
                $detail.html(res.replace(/\n/g, '<br>'));
            });
        }
    });

    $table.on('all.bs.table', (e, name, args) => {
        console.log(name, args);
    });

    $remove.click(() => {
        const ids = getIdSelections();
        $table.bootstrapTable('remove', {
            field: 'id',
            values: ids
        });
        $remove.prop('disabled', true);
    });

    $(window).resize(() => {
        $table.bootstrapTable('resetView', {
            height: getHeight()
        });
    });
};


function getIdSelections() {
    return $.map($table.bootstrapTable('getSelections'), row => row.id)
}

function responseHandler(res) {
    $.each(res.rows, (i, row) => {
        row.state = $.inArray(row.id, selections) !== -1;
    });
    return res;
};

每次我刷新页面或表格时,load-success.bs.table 事件都会接收数据。 responseHandle 函数也被触发并接收相同的有效数据。

JSON 数据格式是有效的,如果我只是从 /books/all 请求中复制响应并将其粘贴到 bootstrapTable 初始化对象中的 data 属性中(就在 columns 属性之后),数据将正常呈现.


您能否帮助我了解我做错了什么并解决该问题?

【问题讨论】:

    标签: jquery json ajax twitter-bootstrap bootstrap-table


    【解决方案1】:

    我认为您只是缺少 JSON 中指定行数的附加信息,并将实际数据包含在行对象中,例如:

    {
      "total": 2,
      "rows": [
      {
        "id":42
        "name":"whatever",
        "description":"whatever"
        "cover_img":"https://whatever.jpg"
        "available_count":10,
        "price":6.99,
        "author_id":21,
        "publisher_id":5,
        "author_first_name":"Harper",
        "author_last_name":"Lee",
        "author_birthday":"1926-04-27T22:00:00.000Z",
        "publisher_name":"Penguin Fiction"
        },
        ...]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      • 2011-08-15
      • 1970-01-01
      相关资源
      最近更新 更多