【问题标题】:Populate Bootstrap Table with data from json source使用来自 json 源的数据填充引导表
【发布时间】:2021-02-18 10:18:11
【问题描述】:

我是使用 bootstrap 和 json 文件的新手,但遇到了以下问题:

我有以下代码:

<div class="container">

  <h1 class="text text-success text-center ">Kontoauszug</h1>

  <table id="table" data-toggle="table" data-url="/json/data.json">
    <thead>
    <tr>
      <th data-field="auszug.betrag">ID</th>
      <th data-field="auszug.unix">Item Name</th>
      <th data-field="auszug.transaktionsart">Item Price</th>
    </tr>
    </thead>
  </table>

</div>

<script src="https://code.jquery.com/jquery-3.3.1.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.16.0/dist/bootstrap-table.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>

<link rel="stylesheet" href = "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href= "https://unpkg.com/bootstrap-table@1.16.0/dist/bootstrap-table.min.css">
<link rel="stylesheet" href="https://unpkg.com/bootstrap-table@1.18.0/dist/bootstrap-table.min.css">

我正在使用的 json 具有以下结构:

{
  "auszug": {
    "1604400036082-3450": {
      "betrag": -367.5,
      "von/an_uuid": "Test1",
      "von/an": "Test1",
      "autorisiert-durch": "SYSTEM",
      "unix": 1604400036,
      "transaktionsart": "Lohnzahlung"
    },
    "1604406781759-8437": {
      "betrag": 85.17,
      "von/an": "Test2",
      "autorisiert-durch": "SYSTEM",
      "unix": 1604406782,
      "transaktionsart": "Überweisung"
    },
    "1604395596115-5983": {
      "betrag": 1226.48,
      "von/an": "Test3",
      "autorisiert-durch": "SYSTEM",
      "unix": 1604395596,
      "transaktionsart": "Überweisung"
    }
  },
  "kontonummer": "DEF05487151498683",
  "kontostand": 44641548.66,
  "success": true
}

但是当我尝试运行代码时,我得到“没有找到匹配的记录”。 如何从这样的 json 中获取数据到表中?

提前非常感谢!

编辑: 我不知道如何在此处准确获取 responseText,但这是 console.log 的屏幕截图:

【问题讨论】:

  • 你能展示你的javascript,你是如何加载json文件的吗?
  • 我想用 bootstrap.table 我不需要编写额外的 javascript?如果我错了,请纠正我:) 我将脚本 srcs 添加到了 sn-p。
  • 不,你是对的,但你确定网址没问题吗?文件被触动了吗?
  • 我认为你的 json 文件结构不好..我没有看到 []
  • 我觉得网址没问题。我可以在这个设置中检查它吗?我的意思是通常我会 consolo.log()... 另一件事是我不知道数据字段是否正确。 (我想我需要更深一层,不是吗?

标签: javascript jquery json bootstrap-4 bootstrap-table


【解决方案1】:

可以观察到的是,您不知道密钥,因为它是动态的。您可以做的是,进行 ajax 调用并获取变量中的数据。现在您必须对响应进行扁平化,以便将扁平数组传递给 Bootstrap 表。而不是使用 data-url 属性,您遵循小提琴中给出的过程

我添加了一个小提琴,您可以将其用作示例。我还添加了适当的评论。

HTML

<link href="https://unpkg.com/bootstrap-table@1.18.0/dist/bootstrap-table.min.css" rel="stylesheet">

<script src="https://unpkg.com/bootstrap-table@1.18.0/dist/bootstrap-table.min.js"></script>

<table id="table">
  <thead>
    <tr>
      <th data-field="betrag">betrag</th>
      <th data-field="autorisiert-durch">autorisiert-durch</th>
      <th data-field="unix">unix</th>
    </tr>
  </thead>
</table>

你的脚本应该是

<script>
var $table = $('#table')

  $(function() {
  
  // do an ajax call here to get the response. your response should be like responseData
  
  var responseData = {
    "1604400036082-3450": {
        "betrag": -367.5,
        "von/an_uuid": "asdqwe2413",
        "von/an": "Test1",
        "autorisiert-durch": "SYSTEM",
        "unix": 1604400036,
        "transaktionsart": "Überweisung"
        },
    "1604406781759-8437": {
        "betrag": 85.17,
        "von/an": "Test2",
        "autorisiert-durch": "SYSTEM",
        "unix": 1604406782,
        "transaktionsart": "Überweisung"
        },
    };
  
  var data = [];
  
  // Here you have to flat the array
  Object.keys(responseData).forEach(function(key){ 
  
  var value = responseData[key]; 
  data.push(value);
  })
    $table.bootstrapTable({data: data})
  })
  
  </script>

如果您需要此代码的 ajax 版本,请告诉我。

小提琴http://jsfiddle.net/8ngoh4y1/

【讨论】:

  • 如果我能得到一个 Ajax 版本会很棒:)
  • 您好,非常感谢!我试图进行 Ajax 调用,将“auszug”从 json 响应中删除,但我没有得到它。你对此有什么建议吗?谢谢!
  • 你能发布你在ajax调用后得到的响应吗?当我使用 ajax Json 调用发布答案时。
  • 我在我的问题中将 json 编辑为正确格式,我还附上了 console.log 的屏幕截图。已经非常感谢+1
  • jsfiddle.net/hwnajc5f 检查这个小提琴,这有 json 调用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-03
  • 2018-06-15
  • 1970-01-01
  • 1970-01-01
  • 2019-10-15
  • 2018-10-13
  • 1970-01-01
相关资源
最近更新 更多