我是Jquery DataTables的noob。关于如何使用排序插件,我有一个非常基本的问题:在Google上搜索了很多内容,但是我没有得到正确答案,关于如何将它们合并到我的代码中
<html>
<head>
<link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css">
</head>
<body>
<table id="kiran">
<thead>
<tr>
<th class="hidden-480">Name</th>
<th class="hidden-480">Price</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js"></script>
<script>
$(function(){
//$("#kiran").dataTable();
$('#kiran').dataTable({
"order": [[ 1, "desc" ]],
"aaSorting": [[1, 'desc']],
});
displayData();
})
function displayData()
{
var json = [
{
"Name": "ONE",
"Price": "12"
},
{
"Name": "TWO",
"Price": "100"
},
{
"Name": "THREE",
"Price": "42"
}
]
for(var i=0;i<json.length;i++)
{
var name = json[i].Name;
var price = json[i].Price;
$('<tr>').append(
$("<td width='20%''>").text(name),
$("<td width='25%'>").text(price)
).appendTo('#kiran');
}
}
</script>
</body>
</html>
这是我的jsfiddle
有人可以帮忙解决这个问题吗?
答案 0 :(得分:1)
我将数据移动到var
var data = [
{
"Name": "ONE",
"Price": "12"
},
{
"Name": "TWO",
"Price": "100"
},
{
"Name": "THREE",
"Price": "42"
}
];
然后我将数据库更改为此配置
"aaData": data, //set the datatables data to the json var previously created
//mapped each column to the respective data it must present
"aoColumns": [
{ "mData": "Name" },
{ "mData": "Price" },
],