javascript - 如何将以下 JSON 数据插入到表中?

标签 javascript jquery html json twitter-bootstrap

尝试修复以下代码时我感到非常沮丧。我是 Javascript 新手,我需要一点帮助:

我有以下代码,我尝试将所有这些插入到 Bootstrap-Table 中,但没有成功。我总是收到下一个错误:

No matching records found.

<script>
    function showResult(sectoresURL) {

      $('#info').empty(); // First, remove all child in stack.

      $.getJSON(sectoresURL, function (json) {
        if (sectores.getVisible() && typeof(json.features[0])  != 'undefined') {
          $('#info').append('<table id=table-javascript></table>');
          buildSectorTable(sectoresURL);
        }
      });
    }

    function buildSectorTable(sectoresURL) {

      $('#table-javascript').bootstrapTable({
              method: 'get',
              url: sectoresURL,
              cache: true,
              height: 400,
              striped: true,
              pagination: true,
              pageSize: 50,
              pageList: [10, 25, 50, 100, 200],
              search: true,
              showRefresh: true,
              minimumCountColumns: 2,
              clickToSelect: false,
              showToggle: true,
              columns: [{
                  field: 'type',
                  title: 'Numero',
                  align: 'center',
                  valign: 'bottom',
                  sortable: true
              }]
      });
    }

    </script>

在这里,我只是尝试插入“Numero”。

顺便说一下,JSON 数据是:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "id": "SectoresComunaCurico.34",
      "geometry_name": "FiguraGeometrica",
      "properties": {
        "Numero": 19,
        "Sector": "Cordillera"
      }
    }
  ],
  "crs": {
    "type": "name",
    "properties": {
      "name": "urn:ogc:def:crs:EPSG::32319"
    }
  }
}

最佳答案

首先,使用 ID 在 HTML 中定义表,以便可以访问它。如果您使用 Bootstrap,请为其提供必要的类。 (class="table")

<table id="table-id">
    <thead>
        <tr>
            <th>Some Header</th>
            <th>Another Header</th>
        </tr>
    </thead>
    <tbody>

    </tbody>
</table>

现在,对于 Javascript,您需要添加到表中。

$('#table-id > tbody').append('<tr><td>Some Content</td><td>Some Other Content</td></tr>');

如果要添加多行,请循环遍历列表并每次为列表中的该项目运行代码。

如果需要清空表格(新数据等),可以使用

$('#table-id > tbody').children().remove();

如果您需要,请要求澄清。

关于javascript - 如何将以下 JSON 数据插入到表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28777486/

相关文章:

javascript - Rails 获取请求无法使用 Angular 正常工作

javascript - Cufon textShadow + hover问题

html - 无法设置背景颜色

Javascript 从字符串中提取正则表达式

javascript - 单击清除文本框

javascript - 如何回滚无法在jstree中移动的节点

javascript - 如何使可折叠的侧边栏变粘?

html - div 不会垂直对齐

javascript - 基于jQuery中的其他数组动态过滤数组

javascript - 创建一组对象