jQuery DataTables-无法重新初始化DataTable [英] jQuery DataTables - Cannot reinitialise DataTable
问题描述
我正在使用jQuery DataTables呈现Spring 4.x应用程序中的语句页面.
I'm making a statements page in a spring 4.x application using jQuery DataTables to render the statements.
在通过Jackson消息转换器进行解析之后,服务器返回了以下对象.
I have the following object returned by the server after parsing thru the Jackson message converter.
{
"data":[
{
"desc":"SUBWAY 00501197 BRONX NYUS",
"amount":"-",
"date":"5.72"
},
{
"desc":"MIDTOWN COMICS NEW YORK NYUS",
"amount":"-",
"date":"73.32"
},
{
"desc":"GOOGLE *PlayDog Soft GOOGLE.COM/CHCAUS",
"amount":"-",
"date":"1.99"
},
{
"desc":"BIMG PRIMARY CARE NEW YORK NYUS",
"amount":"-",
"date":"25.00"
},
{
"desc":"GOOGLE *PlayDog Soft GOOGLE.COM/CHCAUS",
"amount":"-",
"date":"1.99"
},
{
"desc":"GOOGLE *PlayDog Soft GOOGLE.COM/CHCAUS",
"amount":"-",
"date":"1.99"
},
{
"desc":"Walgreens Speci 205 8THNEW YORK NYUS",
"amount":"-",
"date":"10.00"
},
{
"desc":"GOOGLE *SGN Games GOOGLE.COM/CHCAUS",
"amount":"-",
"date":"9.99"
},
{
"desc":"REDBOX *DVD RENTAL 866-733-2693 ILUS",
"amount":"-",
"date":"35.93"
},
{
"desc":"SHUN FA RELAXING CENTE NEW YORK NYUS",
"amount":"-",
"date":"20.00"
},
{
"desc":"CHIPOTLE 0590 NEW YORK NYUS",
"amount":"-",
"date":"9.00"
},
{
"desc":"CHIPOTLE 0590 NEW YORK NYUS",
"amount":"-",
"date":"9.00"
},
{
"desc":"ALEX CLEANERS BRONX NYUS",
"amount":"-",
"date":"58.95"
},
{
"desc":"SUBWAY 00501197 BRONX NYUS",
"amount":"-",
"date":"5.17"
},
{
"desc":"PAYPAL *LAROMANAAPA 4029357733 CAUS",
"amount":"-",
"date":"103.20"
},
{
"desc":"GOOGLE *PlayDog Soft GOOGLE.COM/CHCAUS",
"amount":"-",
"date":"1.99"
},
{
"desc":"GOOGLE *PlayDog Soft GOOGLE.COM/CHCAUS",
"amount":"-",
"date":"1.99"
},
{
"desc":"GOOGLE *SGN Games GOOGLE.COM/CHCAUS",
"amount":"-",
"date":"4.99"
},
{
"desc":"GOOGLE *SGN Games GOOGLE.COM/CHCAUS",
"amount":"-",
"date":"4.99"
},
{
"desc":"LA ISLA RESTAURANT #2 NEW YORK NYUS",
"amount":"-",
"date":"12.75"
},
{
"desc":"GOOGLE *PlayDog Soft GOOGLE.COM/CHCAUS",
"amount":"-",
"date":"1.99"
},
{
"desc":"GOOGLE *PlayDog Soft GOOGLE.COM/CHCAUS",
"amount":"-",
"date":"1.99"
},
{
"desc":"REDBOX *DVD RENTAL 866-733-2693 ILUS",
"amount":"-",
"date":"7.62"
},
{
"desc":"GOOGLE *PlayDog Soft GOOGLE.COM/CHCAUS",
"amount":"-",
"date":"1.99"
},
{
"desc":"GOOGLE *PlayDog Soft GOOGLE.COM/CHCAUS",
"amount":"-",
"date":"1.99"
},
{
"desc":"GOOGLE *PlayDog Soft GOOGLE.COM/CHCAUS",
"amount":"-",
"date":"1.99"
},
{
"desc":"GOOGLE *PlayDog Soft GOOGLE.COM/CHCAUS",
"amount":"-",
"date":"1.99"
},
{
"desc":"GOOGLE *PlayDog Soft GOOGLE.COM/CHCAUS",
"amount":"-",
"date":"3.99"
},
{
"desc":"GOOGLE *PlayDog Soft GOOGLE.COM/CHCAUS",
"amount":"-",
"date":"1.99"
},
{
"desc":"GOOGLE *PlayDog Soft GOOGLE.COM/CHCAUS",
"amount":"-",
"date":"1.99"
}
]
}
现在,我正尝试将其传递给数据表,如下所示.但是我得到一个错误.
Now I'm trying to pass this to data table as follows. But I get an error.
$('#example').dataTable(data)
但是它给了我一个错误,如下所示:
But it gives me an error shown below:
DataTables警告:表id = example-无法重新初始化DataTable.有关此错误的更多信息,请参见 https://datatables.net/manual/tech-notes/3 .
呈现的表还具有代替列名称的数据,并且看起来是错误的.
Also the table rendered has data in place of column names and looks just wrong.
下面是我的控制器
@RequestMapping(value = "/getStmt", method = RequestMethod.GET)
public @ResponseBody HashMap<String,ArrayList<UserStatement>> getStatement(@RequestParam(required = true) String name, @RequestParam(required = true) String month, HttpServletRequest request, Model model){
System.out.println(name+", "+month+"");
User user = userDao.findUserByName(name);
ArrayList<UserStatement> s=new ArrayList<UserStatement>();
if (user != null && user.getUser_token()!=null) {
s = (ArrayList<UserStatement>) userDao.getUserSelectedStatement(user.getUser_token(), 07, 2015);
}else{
UserStatement us =new UserStatement();
us.setAmount("A user token could not be found for the user ");
s.add(us);
}
HashMap<String, ArrayList<UserStatement>> h=new HashMap<String, ArrayList<UserStatement>>();
h.put("data", s);
return h;
}
下面是我的Ajax函数
Below is my Ajax function
$(document).ready(function(){
var today = new Date();
var month=today.getMonth();
var name="test";
$.ajax({
type: "GET",
url: "getStmt.ajax",
data: "name="+name+"&month="+month,
success: function(msg){
$('#stdata').html( '<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"></table>' );
var data1 = JSON.stringify(msg);
console.log(data1);
console.log(data1.data);
console.log( "Data Saved: " + msg );
$('#example').dataTable( {
"aaData": data,
"aoColumns": [
{"sTitle": "Description"},
{"sTitle": "Amount" },
{"sTitle": "Date" }
]
} );
},
error: function(msg){
var data = msg;
console.log( "There was an error: " + msg );
$('#stdata').dataTable( {
"ajax": data,
"columns": [
{ "title": "Description" },
{ "title": "Amount" },
{ "title": "Date" },
{ "title": "Balance" }
]
} );
}
});
});
和html:
<div class="panel-body">
<div id="stdata">
</div>
</div>
表格应位于stdata
div之内.
the table should sit inside stdata
div.
我该如何使用它?我还希望这样一种方式:如果用户更改了下拉列表中选择的日期,则应该通过Ajax重新填充该日期的表.
How can I get this to work? I also want it in such a way that if a user changes the date selected in a drop-down list, the table should be repopulated thru Ajax for that date.
推荐答案
解决方案1
使用 ajax.data
选项添加将发送到服务器的其他参数.
SOLUTION 1
Use ajax.data
option to add additional parameters that would be sent to the server.
那样,您无需重新初始化表,这使得代码更加简单.
That way you don't need to re-initialize the table which makes the code much simple.
例如:
$('#example').DataTable({
ajax: {
url: "/script.php",
data: function(d){
d.select1 = $('#example-select1').val();
d.select2 = $('#example-select2').val();
}
}
});
$('#example-select1, #example-select2').on('change', function(){
$('#example').DataTable().ajax.reload();
});
其中example-select1
,example-select2
是下拉菜单的ID.
where example-select1
, example-select2
are IDs of your drop-downs.
有关代码和演示,请参见此jsFiddle .
See this jsFiddle for code and demonstration.
与clear()
/datatables.net/reference/api/rows.add%28%29"rel =" noreferrer> rows.add()
清除表并为DataTables 1.10+添加新数据.对于DataTables 1.9,请使用 fnClearTable
和 要将数据添加到现有表(对于DataTables 1.10 +):
$('#example').DataTable().clear();
$('#example').DataTable().rows.add(msg.data);
其中,msg
是服务器保留的变量.
where msg
is a variable holding response from the server.
使用 bDestroy
(对于DataTables 1.9)或
Use bDestroy
(for DataTables 1.9) or destroy
(for DataTables 1.10+) options to destroy any existing table matching the selector and replace with the new options.
要重新初始化表(对于DataTables 1.10 +):
$('#example').DataTable( {
"destroy": true,
"data": msg.data,
"columns": [
{"title": "Description"},
{"title": "Amount" },
{"title": "Date" }
]
});
其中,msg
是服务器保留的变量.
where msg
is a variable holding response from the server.
这篇关于jQuery DataTables-无法重新初始化DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!