【问题标题】:load data in bootstrap-table from json从 json 加载引导表中的数据
【发布时间】:2020-10-31 00:05:34
【问题描述】:

最近我熟悉了 bootstrap-table,我正在尝试从 JSON 文件加载数据。根据文档,我尝试使用:

data-url="json/data.json"

还有:

id="person"

对于加载数据,我使用以下代码:

<th data-field="FName" data-sortable="true">First Name</th>

但它没有显示任何内容。请给我一些关于这个问题的提示吗?

【问题讨论】:

    标签: json bootstrap-4 bootstrap-table


    【解决方案1】:

    我设法使用您在jsonbin.io 上托管的 json 代码进行了演示

    这是你的 json...https://api.jsonbin.io/b/5f9dfe423269193b17bffab2

    在你发给我的 json 中,只有 1 行。所以我添加了另外 2 个额外的示例行来显示表中的数据加载。

    你错过的东西......

    首先是的,您缺少 [ ] 包装行数据。在对表 json 进行编码时,请考虑像这样的 json 布局表......

    [
       // this is your table body
       {
          // this a table row
          // set tr key
          "Nayment" : {
             
             // then define each td with a sub key / value
             "Id": 24,
             "Name": "Jack Allen",
             "TotalCost": 1000,
    
             // etc...
          }
       },
       {
          // next table row
          // tr key
          "Nayment" : {
    
              // etc...
          }
       },
    
       // etc...
    ]
    

    然后将此数据分配给相关的表列,在表头单元格中,使用带有json行键和子键Nayment.Namedata-field属性。

    如果你想要名字和姓氏,我想你必须在你的 json 中分离这些数据。

    <th data-field="Nayment.Name" data-sortable="true">Name</th>
    

    您也可以通过扩展子键来使用更深层次的数据,例如您的余额详细信息Nayment.BalanceDetails.PowerCash

    <th data-field="Nayment.BalanceDetails.PowerCash" data-sortable="true">Balance: Power Cash</th>
    

    使用您的 json 查看下面的现场演示...

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://unpkg.com/bootstrap-table@1.18.0/dist/bootstrap-table.min.css">
    
    <div class="container">
      <table id="table" data-toggle="table" data-flat="true" data-search="true" data-url="https://api.jsonbin.io/b/5f9dfe423269193b17bffab2">
        <thead>
          <tr>
            <th data-field="Nayment.Id" data-sortable="true">ID</th>
            <th data-field="Nayment.Name" data-sortable="true">Name</th>
            <th data-field="Nayment.RemainCash" data-sortable="true">Remaining Cash</th>
            <th data-field="Nayment.TotalAsset" data-sortable="true">Total Assets</th>
            <th data-field="Nayment.BalanceDetails.PowerCash" data-sortable="true"><small>Balance Details</small><br/>Power Cash</th>
            <th data-field="Nayment.BalanceDetails.BlockedCash" data-sortable="true"><small>Balance Details</small><br/>Blocked Cash</th>
            <th data-field="Nayment.BalanceDetails.creditCash" data-sortable="true"><small>Balance Details</small><br/>Credit Cash</th>
            <th data-field="Nayment.BalanceDetails.AccountCash" data-sortable="true"><small>Balance Details</small><br/>Account Cash</th>
          </tr>
        </thead>
      </table>
    </div>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
    <script src="https://unpkg.com/bootstrap-table@1.18.0/dist/bootstrap-table.min.js"></script>

    【讨论】:

    • 谢谢你,我试试你提到的代码,它有效。您知道我的 JSON 不以 [ 开头并以 ] 结尾,并且它不显示任何文件。
    • 你能分享你的json吗?
    • { "Nayment": { "Id": "Nayment", "Name": "Jack Allen", "TotalCost": 1000, "RemainCash": 100, "TotalAsset": 350, "Branch": ["Store One", "Store Two"], "BalanceDetails": { "PowerCash": 12345, "BlockedCash": 12335, "creditCash": 1235, "AccountCash": 1235 }, "LastOrder": { "O1FKHZ0001": { "OrderId": "O1FKHZ0001", "TotalCost": 19330500, "TotalValue": 29330500, "ProftiLoss": 123423, "Status": "Open", "FirstBuyPrice": 2340 } } } }
    • 我已经使用您提供的 json 更新了问题并进行了更改。希望这会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-19
    • 1970-01-01
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    相关资源
    最近更新 更多