简体   繁体   English

带有DataTables的Javascript JSON数据

[英]Javascript JSON data with DataTables

I am using a JavaScript with jQuery DataTables but I have been having problem in loading the JSON data into the Bootstrap table. 我在jQuery DataTables中使用JavaScript,但是在将JSON数据加载到Bootstrap表中时遇到了问题。

This is a sample of the table 这是表的示例

[ { name: 'Bryant Stone',       hobbies: 'Football',       favorite_team: 'Sea hawks',       height: '5 Feet 10 in' },
  { name: 'Jesse Smith',        hobbies: 'boxing',          favorite_team: 'Spurs',           height: '6 feet 6 in' },
  { name: 'Emily Wyclef',       hobbies: 'cooking',         favorite_team: 'Venus Williams',  height: '5 feet 2 in' }
]

In JavaScript I get the data in this format and put it inside JavaScript like this 在JavaScript中,我以这种格式获取数据并将其放入JavaScript中,如下所示

$(document).ready(function(){
    $(\'#respondent\').dataTable({
        destroy: true,
        data: jsonStr1,
        columns:[   
                {title:"Name", data: "name"},
                {title:"Hobbies", data: "hobbies"},
                {title:"Favorite team" ,data: "favorite_team"},
                {title: "Height" ,data: "height"}
            ]
    });
})

When I load the page it shows my data in the console but a DataTables dialog box shows this message 当我加载页面时,它会在控制台中显示我的数据,但“数据表”对话框会显示此消息

DataTable warning table id=respondent-
Requested unknown parameter 'name' for
row0, column 0. For information more.....

I don't know what else I can do. 我不知道还能做什么。 It has taken the whole day from me. 我花了整整一天的时间。 Any help would be appreciated. 任何帮助,将不胜感激。

UPDATE Thanks to all who helped by providing some answers all of which i have done. 更新感谢所有帮助我提供一些我已经完成的答案的人。 This is my html 这是我的HTML

<table class="display" id="respondent">
    <thead>

        <tr>

            <th>Name</th>
            <th>Hobbies</th>
            <th>Favorite Team</th>
            <th>Height</th>
        </tr>

    </thead>
</table>

I have made needed typo correction to the code displayed earlier but I still keep on getting this error message 我已经对前面显示的代码进行了拼写错误校正,但是我仍然继续收到此错误消息

DataTables warning: table id=respondent- 
Requested unknown parameter 'name' for 
row 0, column 0, for more information about this 
error, please see http://datatables.net/tn/4

I went to the link but could not get anything helpful. 我去了链接,但没有任何帮助。 After the above message, the table get filled up with empty spaces and after going to some pages I see just one character in the first cell only. 出现上述消息后,表中充满了空白,进入某些页面后,我仅在第一个单元格中看到一个字符。 Those characters either letter or braces I do not know where they came from because I could not see such sequence in my json data even numbers shows up. 我不知道这些字符是字母还是括号,因为即使出现数字,我也无法在json数据中看到这样的序列。 It keeps on baffling me I do not know what else to do. 它一直困扰着我,我不知道该怎么办。 Any help would be appreciated. 任何帮助,将不胜感激。

UPDATE I discovered that the problem was that the data was in a string. 更新我发现问题在于数据在字符串中。 Does anyone know how to convert string in javascript to object without using eval(). 有谁知道如何在不使用eval()的情况下将javascript中的字符串转换为对象。 JSON.parse return string and not object which is what is being looked for. JSON.parse返回字符串,而不是要查找的对象。

Minor changes: 小改动:

  1. , missing after data option , data选项后缺失
  2. Column is passed as favourite_team (British english) but data has favorite_team (American English) 列以favourite_team (英式英语)形式传递,但数据具有favourite_team (美式英语)形式
  3. Column contains hobbies where as data contains hobbies in the first row while hobbie in the other rows. 列包含其中作为数据包含在第一行中的爱好 的爱好在其他行。 You gotta match them. 你要匹配他们。
  4. Use title option in the columns to show the header name 在列中使用title选项以显示标题名称

Info: That error/alert is usually due to missing columns in the data. 信息:该错误/警报通常是由于缺少数据列造成的。

Here's a code snippet fixing the above: 这是修复以上问题的代码段:

 $(document).ready(function(){ var jsonStr1 = [ { name: 'Bryant Stone', hobbies: 'Football', favorite_team: 'Sea hawks', height: '5 Feet 10 in' }, { name: 'Jesse Smith', hobbies: 'boxing', favorite_team: 'Spurs', height: '6 feet 6 in' }, { name: 'Emily Wyclef', hobbies: 'cooking', favorite_team: 'Venus Williams', height: '5 feet 2 in' } ]; $('#respondent').dataTable({ destroy: true, data: jsonStr1, columns:[ {title: "name", data: "name"}, {title: "hobbies", data: "hobbies"}, {title: "favorite_team", data: "favorite_team"}, {title: "height", data: "height"} ] }); }) 
 <script src="https://code.jquery.com/jquery-3.2.1.js"></script> <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.18/datatables.min.css"/> <script src="//cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script> <table id="respondent" class="display"> <thead> </thead> <tbody> </tbody> </table> 

Hope this helps. 希望这可以帮助。

Typo errors of column names - hobbies are mentioned as "hobbie" and favo u rite_team as favorite_team. 列名的错字错误-爱好是提到“霍”和FAVOürite_team为favorite_team。

Maintain same property names for all objects to avoid that error 为所有对象保持相同的属性名称,以避免该错误

Code sample for reference - https://codepen.io/nagasai/pen/vzNXPe 参考代码示例-https: //codepen.io/nagasai/pen/vzNXPe

HTML: HTML:

<table class="display" id="respondent">
    <thead>

        <tr>

            <th>Name</th>
            <th>Hobbies</th>
            <th>Favorite Team</th>
            <th>Height</th>
        </tr>

    </thead>
</table>

JS JS

var jsonStr1 = [ { name: 'Bryant Stone',  hobbies: 'Football', favorite_team: 'Sea hawks',       height: '5 Feet 10 in' },
  { name: 'Jesse Smith', hobbies: 'boxing', favorite_team: 'Spurs',           height: '6 feet 6 in' },
  { name: 'Emily Wyclef', hobbies: 'cooking',  favorite_team: 'Venus Williams',  height: '5 feet 2 in' }
]

$(document).ready(function() {
 $('#respondent').dataTable({
        destroy: true,
        data: jsonStr1,
        columns:[   
            {title:"Name", data: "name"},
            {title:"Hobbies", data: "hobbies"},
            {title:"Favorite team" ,data: "favorite_team"},
            {title: "Height" ,data: "height"}
        ]
    });
} );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM