【问题标题】:Bootstrap - My table can't read a local JSON fileBootstrap - 我的表无法读取本地 JSON 文件
【发布时间】:2019-11-09 17:00:22
【问题描述】:

我不知道,我无法读取一些 JSON 文件,所以我放置了一个读取 JSON 数据(内部或外部源)的表

有人有想法吗?

这是我使用的链接和脚本

  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.10.1/bootstrap-table.min.css">
  <script src="https://unpkg.com/bootstrap-table@1.15.5/dist/bootstrap-table.min.js"></script>

这是我创建表的脚本,我使用 data-URL 从本地 JSON 文件加载数据

  <table id="table" data-toggle="table" data-height="460" data-search="true" data-url="data.json">
    <thead>
      <tr>
        <th data-field="id">#</th>
        <th data-field="oeuvre" data-search-formatter="false" data-formatter="nameFormatter">Oeuvres</th>
        <th data-field="type" data-formatter="nameFormatter">Type</th>
        <th data-field="artist" data-formatter="nameFormatter">Artiste</th>
        <th data-field="sheet" data-formatter="nameFormatter">Fiche</th>
      </tr>
    </thead>
  </table>
  <script>
    $table.bootstrapTable('refresh',{data: data})
  })

    function nameFormatter(value) {
      return 'Formatted ' + value
    }
  var $table = $('#table')

  $(function() {
    var data = [
      {"id":1,"oeuvre":"choppe","type":"Ambre","artist":"Etienne","sheet":"<a href=\"description.html\">"}
    ]
    $table.bootstrapTable({data: data})
  })
</script>

我真的不知道为什么它不起作用......

提前致谢

【问题讨论】:

  • “本地”是指在运行浏览器的计算机上?这是不可能的

标签: javascript html bootstrap-4


【解决方案1】:

如果要加载本地 json 文件,请尝试如下。

function nameFormatter(value) {
          return 'Formatted ' + value
        }

        var data = [
          {"id":1,"oeuvre":"choppe","type":"Ambre","artist":"Etienne","sheet":"<a href=\"description.html\">"}
        ]
      $("#table").bootstrapTable({data: data})

并从表中删除数据属性data-url="data.json"

你可以运行下面的sn-p来查看结果。

function nameFormatter(value) {
      return 'Formatted ' + value
    }

    var data = [
      {"id":1,"oeuvre":"choppe","type":"Ambre","artist":"Etienne","sheet":"<a href=\"description.html\">"}
    ]
  $("#table").bootstrapTable({data: data})
<script
  src="https://code.jquery.com/jquery-3.4.1.min.js"
  integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
  crossorigin="anonymous"></script>
    <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.10.1/bootstrap-table.min.css">
  <script src="https://unpkg.com/bootstrap-table@1.15.5/dist/bootstrap-table.min.js"></script>
  
  
  <table id="table" data-toggle="table" data-height="460" data-search="true">
    <thead>
      <tr>
        <th data-field="id">#</th>
        <th data-field="oeuvre" data-search-formatter="false" data-formatter="nameFormatter">Oeuvres</th>
        <th data-field="type" data-formatter="nameFormatter">Type</th>
        <th data-field="artist" data-formatter="nameFormatter">Artiste</th>
        <th data-field="sheet" data-formatter="nameFormatter">Fiche</th>
      </tr>
    </thead>
  </table>

如果你想从外部源加载,你的函数应该只包含格式化函数。

试试下面的 sn-p 看看结果。

function nameFormatter(value) {
      return 'Formatted ' + value
}
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.10.1/bootstrap-table.min.css">
<script src="https://unpkg.com/bootstrap-table@1.15.5/dist/bootstrap-table.min.js"></script>

<table id="table" data-toggle="table" data-height="460" data-search="true" data-url="https://api.myjson.com/bins/q9n5g">
  <thead>
    <tr>
      <th data-field="id">#</th>
      <th data-field="oeuvre" data-search-formatter="false" data-formatter="nameFormatter">Oeuvres</th>
      <th data-field="type" data-formatter="nameFormatter">Type</th>
      <th data-field="artist" data-formatter="nameFormatter">Artiste</th>
      <th data-field="sheet" data-formatter="nameFormatter">Fiche</th>
    </tr>
  </thead>
</table>

【讨论】:

    【解决方案2】:

    如果您不从外部 json 文件加载数据,请删除 data-toggle 属性。

    当启用数据切换且未呈现数据时,似乎对 $('#table').bootstrapTable() 的任何调用都会被完全忽略。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-28
      相关资源
      最近更新 更多