【问题标题】:Processing nested arrays in Bootstrap Table处理 Bootstrap Table 中的嵌套数组
【发布时间】:2017-01-17 11:44:42
【问题描述】:

我正在开发一个包含 2000 多个条目的 Zotero 数据库,并希望使用 Bootstrap Table 公开展示它。

但是,由于 Zotero 字段的工作方式,即作者字段,JSON 文件存在问题。示例:

"author": [{
    "family": "Obama",
    "given": "Barack"
        }
    ,
        {
    "family": "Obama",
    "given": "Michele"
        }]

如果我使用“作者”字段,这将转换为 [Object Object],或者我可以使用 FlatJson 扩展并仅使用嵌套值(通过“author.0.family”) ,这会中断搜索并且不会返回所有作者。

更新:见jsfiddle

【问题讨论】:

    标签: json bootstrap-table


    【解决方案1】:

    您应该能够使用行格式化程序来处理它。

    表格中的行标题应如下所示:

    <th data-field="author" data-formatter="authorFormatter">
    

    在实例化引导表的 javascript 下方,您可以添加格式化程序代码。像这样的东西应该用“Barack Obama”组成一个字符串,尽管你可以随意格式化它。

    <script>
    function authorFormatter(value, row) {
        if ((value) && (value[0].given)) {
            return value[0].given + ' ' + value[0].family;
        }
    }
    </script>
    

    引导表中的格式化程序功能可让您轻松保持 JSON API 干净,同时根据需要在表中显示数据。

    更新

    假设您的 JSON 看起来像这样(基于您的示例):

    { 
    "type": "chapter", 
    "title": "Long title", 
    "container-title": "Other title", 
    "publisher": "Publisher", 
    "publisher-place": "City", 
    "page": "XX-XX", 
    "source": "Library", 
    "ISBN": "XXXXXXXXXXX", 
    "container-author": 
        [ 
            { 
                "family": "XXX", 
                "given": "XXX" 
            } 
        ], 
        "author": 
        [
            { 
                "family": "Obama", 
                "given": "Barack" 
            }, 
            { 
                "family": "Obama", 
                "given": "Michelle" 
            } 
        ],
        "issued": 
        { 
            "date-parts": 
                [ 
                    [ "2012" ] 
                ]
        } 
    
    }
    

    您的 HTML 表格看起来像这样:

    <table 
    id="table"
    class="table table-striped"
    data-toggle="table"
    data-url="https://url.to.your.json"
    data-side-pagination="server">
        <thead>
            <tr>
                <th data-field="author" data-formatter="authorsFormatter">Authors</th>
            </tr>
        </thead>
    </table>
    

    你的 javascript 看起来像这样:

    <script>
    // Initialize the bootstrap-table javascript
    var $table = $('#table');
    $(function () {
    });
    
    // Handle your authors formatter, looping through the 
    // authors list
    function authorsFormatter(value) {
        var authors = '';
    
        // Loop through the authors object
        for (var index = 0; index < value.length; index++) {
            authors += value[index].given + ' ' + value[index].family;
    
            // Only show a comma if it's not the last one in the loop
            if (index < (value.length - 1)) {
                authors += ', ';
            }
        }
        return authors;
    }
    </script>
    

    【讨论】:

    • 这绝对是我想要的,非常感谢。但是,我确实有一个问题:这将返回“巴拉克奥巴马”,而不是“巴拉克奥巴马,米歇尔奥巴马”。你知道如何添加到函数中,以便返回“作者”下的所有条目吗?
    • 很难说,没有看到 JSON 的整个格式,但我认为它看起来像这样: - book 1 - author 1 - author 2 - book 2 - author 1 - author 3 等?如果是这样,您只需使用 $.each() 在格式化程序中通过一个循环运行它,遍历每个索引。 **编辑:**啊……格式被搞砸了。让我想出一个 jsfiddle 什么的。
    • 字段示例:{ "type": "chapter", "title": "Long title", "container-title": "Other title", "publisher": "Publisher", "publisher-place": "City", "page": "XX-XX", "source": "Library", "ISBN": "XXXXXXXXXXX", "container-author": [ { "family": "XXX", "given": "XXX" } ], "author": [ { "family": "XXX", "given": "XXX" }, { "family": "XXX", "given": "XXX" } ], "issued": { "date-parts": [ [ "2012" ] ] } }
    • 这行得通!搜索时出现“未定义”错误,但这可能是因为某些条目没有作者,而是编辑。一个附加的“如果”处理了它。非常感谢您的参与!!我已经开始使用 Datatables 并最终在那里开发了很多项目。我要么回到这里,要么尝试在那里应用这个解决方案(那些该死的数组总是同样的问题)。
    • 我的问题陈述有点不同,因为它需要从嵌套的 json 数组中填充列,因此使用您的解决方案作为指导,我使它如 jsfiddle 代码所示工作:@987654327 @(通过 http 而不是 https 加载)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-04
    • 2013-10-11
    相关资源
    最近更新 更多