【问题标题】:Datasource from Nested Object Data - Not loading来自嵌套对象数据的数据源 - 未加载
【发布时间】:2016-10-10 09:07:03
【问题描述】:

我遇到了 jQuery DataTables 不显示任何结果的问题。我有有效的 JSON,我正在使用带有 Yajra DataTables 包的 Laravel 5.2。

我不断收到这条消息:

'DataTables 警告:table id=users - 请求的未知参数 第 0 行第 0 列的“名字”。有关此的更多信息 错误,请看http://datatables.net/tn/4'

我的代码是:JS

$('#users').DataTable( {
    "processing": true,
    "ajax": "clients/json",
    "columns": [
        { "data": 'firstname' }
    ]
});

HTML

<table id="users" class="table table-hover table-condensed">
 <thead>
 <tr>
 <th>Firstname</th>
 </tr>
 </thead>
 </table>

JSON数据如下:

{"draw":0,"recordsTotal":7,"recordsFiltered":7,"data":[{"\"uniqueid\"":"57cea728a724c","\"firstname\"":"ken","\"lastname\"":"ertert                        ","\"telephone\"":"","\"mobile\"":"","\"postcode\"":"","\"email\"":"ken@koodoocreative.co.uk","\"tems\"":1,"\"children\"":{"\"3\"":{"\"first_name\"":"tertyreter","\"last_name\"":"etywert                       "}}},{"\"uniqueid\"":"57c69f469b1fb","\"firstname\"":"bryan","\"lastname\"":"johnson                       ","\"telephone\"":"03454345324","\"mobile\"":"03453523452","\"postcode\"":"sr690k","\"email\"":"bryan@koodoocreative.co.uk","\"tems\"":1,"\"children\"":{"\"4\"":{"\"first_name\"":"zak","\"last_name\"":"johnson                       "},"\"5\"":{"\"first_name\"":"sue","\"last_name\"":"johnson                       "}}},{"\"uniqueid\"":"57cd426ed8414","\"firstname\"":"not","\"lastname\"":"paying                        ","\"telephone\"":"","\"mobile\"":"","\"postcode\"":"r55rwefe","\"email\"":"not@koodoocreative.co.uk","\"tems\"":1,"\"children\"":{"\"2\"":{"\"first_name\"":"cant","\"last_name\"":"affordit                      "}}},{"\"uniqueid\"":"57ce97a188f6f","\"firstname\"":"barry","\"lastname\"":"sdf                           ","\"telephone\"":"","\"mobile\"":"","\"postcode\"":"","\"email\"":"barry@koodoocreative.com","\"tems\"":null,"\"children\"":{"\"1\"":{"\"first_name\"":"wherareU","\"last_name\"":"                              "}}},{"\"uniqueid\"":"57f3a5f56539d","\"firstname\"":"bob","\"lastname\"":"smith                         ","\"telephone\"":"","\"mobile\"":"","\"postcode\"":"","\"email\"":"bob@koodoocreative.co.uk","\"tems\"":null,"\"children\"":{"\"8\"":{"\"first_name\"":"claire","\"last_name\"":"smith"}}},{"\"uniqueid\"":"57cd410b0a601","\"firstname\"":"keey","\"lastname\"":"smith                         ","\"telephone\"":"01928272623","\"mobile\"":"07836736534","\"postcode\"":"ts34 y78","\"email\"":"michael@koodoocreative.co.uk","\"tems\"":1,"\"children\"":{"\"7\"":{"\"first_name\"":"blake","\"last_name\"":"smith                         "}}},{"\"uniqueid\"":"57d29c2f72883","\"firstname\"":"test","\"lastname\"":"waiti                         ","\"telephone\"":"","\"mobile\"":"","\"postcode\"":"","\"email\"":"test@koodoocreative.co.uk","\"tems\"":null,"\"children\"":{"\"6\"":{"\"first_name\"":"dunni","\"last_name\"":"sdf                           "}}}],"input":{"_":"1476088895989"}}

【问题讨论】:

    标签: jquery json datatables


    【解决方案1】:

    由于某种原因,您的 JSON 字段名称包含双引号。此外,您正在使用 Yajra DataTables 的服务器端处理模式,但在客户端缺少 serverSide: true 选项。

    与您的数据匹配的代码应该是:

    $("#users").DataTable( {
       "processing": true,
       "serverSide": true,
       "ajax": "clients/json",
       "columns": [
          { "data": '"firstname"' }
       ]
    });
    

    但是,您在 PHP 代码中的某处使用额外的双引号设置列名,如果您可以找到并删除额外的双引号,则在客户端应该只是 { "data": "firstname" }

    【讨论】:

      猜你喜欢
      • 2018-01-12
      • 1970-01-01
      • 2020-03-09
      • 2018-11-14
      • 2012-03-01
      • 1970-01-01
      • 2021-02-01
      • 2021-01-31
      • 1970-01-01
      相关资源
      最近更新 更多