javascript - 如何从 json 填充 footable

标签 javascript html json footable

我有像这样定义footable的html代码

              <table class="table toggle-circle" id="exampleRowToggler">
                <thead>
                  <tr>
                    <th data-field="marca">Marca</th>
                        <th data-field="modelo">Modelo</th>
                        <th data-field="placa">Placa</th>
                        <th data-field="chasis">Chasis</th>
                        <th data-field="vigencia_desde">Vigencia Desde</th>
                        <th data-field="vigencia_hasta">Vigendia Hasta</th>
                    <th data-hide="all">Clausulas</th>
                    <th data-hide="all">Exclusiones</th>
                    <th data-hide="all">Beneficios</th>
                    <th data-hide="all">Deducibles</th>
                    <th data-hide="all">Coberturas</th>
                  </tr>
                </thead>
                <tbody>
                </tbody>
              </table>

我想从一个 .js 文件填充它,该文件包含一个带有 json 数据的函数,如下所示。我尝试了一些选项,但仍然无法得到它,我最后尝试的就是这个。

loadTable: function () {

            var bt_data = [{
                    "marca": "HYUNDAI",
                    "modelo": "IONIQ",
                    "placa": "T02096577",
                    "chasis":"KMHC851CGHU029520",
                    "vigencia_desde":"22/05/2017",
                    "vigencia_hasta":"22/05/2018",
                    "clausulas": "clauuuuuusulaaas",
                    "exclusiones": "eeexclusioooonesss",
                    "beneficios": "beeeeneeefiiiciiiooosss",
                    "deducibles":"deeeeduuuuciiiibleeeessss",
                    "coberturas":"cooooobeeeertuuuuraaaasssss"
                }];

                    $('#exampleRowToggler').footable({
                        "useParentWidth": true,
                        "columns": $.get('columns.bt_data'),
                        "rows": $.get('rows.bt_data')
                    });
        }

请帮帮我。

最佳答案

除非您有一个服务器端页面监听“columns.bt_data”和“rows.bt_data”URL 并返回相关的 JSON/数组响应,否则您传递给 footable 的列和行值将不会产生任何结果.

这就是我认为您想做的事情:

loadTable: function () {
    var bt_data = [{
        "marca": "HYUNDAI",
        "modelo": "IONIQ",
        "placa": "T02096577",
        "chasis":"KMHC851CGHU029520",
        "vigencia_desde":"22/05/2017",
        "vigencia_hasta":"22/05/2018",
        "clausulas": "clauuuuuusulaaas",
        "exclusiones": "eeexclusioooonesss",
        "beneficios": "beeeeneeefiiiciiiooosss",
        "deducibles":"deeeeduuuuciiiibleeeessss",
        "coberturas":"cooooobeeeertuuuuraaaasssss"
    }];

    //get Column JSON from first item in Rows array
    var columnJSON = $.map(Object.getOwnPropertyNames(bt_data[0]), function (column) {
        return {"name": column,"title": column}
    });

    $('#exampleRowToggler').footable({
        "useParentWidth": true,
        "columns": columnJSON, //Pass columns object through to footable
        "rows": bt_data //Pass your existing rows array through to footable
    });
}

关于javascript - 如何从 json 填充 footable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44759457/

相关文章:

javascript - 使用 JavaScript 和 HTML 提交包含所选音乐会座位的表格

javascript - 如何在加载时选择 Select2 下拉列表中的多个选项?

html - 无法证明导航中的内容位于 Bootstrap 4 中的品牌名称旁边

java - 有没有一种方法可以很好地使用 gson 来获取包含 java 中具有 4 个变量的数组的列表

javascript - JSON.parse 将空数组附加到字符串参数的末尾

javascript - Math.round() 控制小数位不起作用

javascript - 如何从某个属性最小的数组中选择一个对象?

html - 可滚动的表体

javascript - 如何让我的模态工作?

c# - 使用JSON.net解析json字符串