jQuery数据表错误地解析json

编程入门 行业动态 更新时间:2024-10-11 23:25:33
本文介绍了jQuery数据表错误地解析json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用具有以下json对象的查询的数据表的示例...

I'm just trying an example of datatables with query with the following json object...

[{"firstName":"pom", "lastName":"sdfpom", "email":null, "password":"cfe9a43acec0a35f903bc2d646ce8e58b5ef6b67", "username":"Dave", "access":null, "id":1}, {"firstName":"FirstName", "lastName":"LastName", "email":null, "password":"8d60258ef3ae1b8eae67e9298f15edf5c6e05dfe", "username":"Username", "access":null, "id":2}]

这是在下面的变量数据中返回的...

This is returned in the variable data below...

<script> $(document).ready(function() { $.getJSON('userManagement/getAllUsers', function(data) { $('#table').dataTable( { "sAjaxSource": data }); }); }); </script> <table id="table"> <thead> <tr> <th>firstName</th> <th>lastName</th> <th>email</th> <th>password</th> </tr> </thead> <tbody> <tr> <td>Row 1 Data 1</td> <td>Row 1 Data 2</td> <td>etc</td> </tr> <tr> <td>Row 2 Data 1</td> <td>Row 2 Data 2</td> <td>etc</td> </tr> </tbody> </table>

现在JSON似乎是有效的,当我做任何其他事情,例如使用它jquery它工作正常,但是数据表根本不能正确渲染。在我使用的JavaScript中有什么问题吗?

Now the JSON seems to be valid, and when I do anything else with it for instance use it within jquery it works fine, but datatables just doesn't render correctly at all. Is there something wrong with the javascript I'm using?

推荐答案

默认情况下,DataTables会处理数组的数组来源:当必须处理数组的对象(如你的情况)时,还有一个额外的步骤。这在插件文档中的此示例中有描述。基本上,你要做的是添加'列'属性(如array)的描述:

By default, DataTables will process an array of arrays for its data source: there's an additional step when it has to deal with array of objects (as in your case). It's described in this example in the plugin documentation. Basically, what you have to do is to add description of 'column' properties (as array):

$('#table').dataTable({ "aaData": data, "aoColumns": [ { "mData": "firstName" }, { "mData": "lastName" }, { "mData": "email" }, { "mData": "password" }, { "mData": "username" }, { "mData": "access" }, { "mData": "id" } ] });

这里是 小提琴 来玩。

Here's fiddle to play with.

更多推荐

jQuery数据表错误地解析json

本文发布于:2023-07-09 21:17:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1088239.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据表   错误   jQuery   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!