我有一个Java(Spring MVC的)后端返回的POJO作为JSON对象如下:
@RequestMapping(value = "/getWidgetsByType", method = RequestMethod.POST)
public @ResponseBody List<WidgetVO> getWidgetsByType(@RequestParam("type") String type)
{
return widgetDAO.getWidgetsByType(token);
}
public class WidgetVO {
private String type;
private String name;
private boolean isAwesome;
// Getters and setters, etc.
}
在前端,我试图调用/getWidgetsByType
从一个jQuery内$.getJSON
调用,然后使用JSON结果来从后面来填充数据表 。 具体而言,我想数据表出现一个内部<div>
标记,是目前在页面加载如下空:
<div id="#table-to-display"></div>
var t = getTypeFromDOM();
$.getJSON(
url: "/getWidgetsByType",
data: {
type: t
},
success: function() {
// How do I extract the JSON version of the List<WidgetVO>'s coming
// back from the server and use them to populate the table?
$("#table-to-display").datatable();
}
);
我想在datatable
包含了相同的列 领域 WidgetVO
(类型,名称,isAwesome),所有的String
值(不渲染器等)。
在此先感谢这里的任何帮助!
例如从数据表中的网站为您提供所有所需的详细信息。
例如JS代码
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "scripts/server_processing.php" // for you it will be - /getWidgetsByType
} );
} );
您的JSON应该是这样的
{
"sEcho": 1,
"iTotalRecords": "57",
"iTotalDisplayRecords": "57",
"aaData": [
[],[],[],...
]
}
这里的例子显示设置在飞行中的列名。
干杯!!
1.Controller
@RequestMapping(value = "/json", method = RequestMethod.GET)
public
@ResponseBody
String listUsersJson(ModelMap model) throws JSONException {
int no=1;
JSONArray userArray = new JSONArray();
for (TileType tT: tD.getAll()){
String n=Integer.toString(no);
String id=Integer.toString(tT.getTileTypeId());
String[] value =
{n,
tT.getTileTypeName(),
tT.getTileTypeDensity()
};
userArray.put(value);
no++;
}
String x=userArray.toString();
String replace1=x.replace("{", "[");
String replace2=replace1.replace("}","]");
String replace3=replace2.replace(":",",");
return ("{\"aaData\":"+replace3+"}");
}
2.Dao
@Override
public List<TileType> getAll() {
Session session=HibernateUtil.getSessionFactory().openSession();
List<TileType>list=new ArrayList<TileType>();
try{
Query query=session.createQuery("from TileType T order by T.tileTypeId DESC");
list=query.list();
}
catch(HibernateException he){}
return list;
}
3.Javascript
var table = $('#example').dataTable({
"sPaginationType": "full_numbers",
"sAjaxSource": "/data/tipeTile/json",
"sDom": "<'row'<'col-xs-6'l><'col-xs-6'f>r>t<'row'<'col-xs-6'i><'col-xs-6'p>>",
"sPaginationType": "bootstrap",
"oLanguage": {
"sLengthMenu": "_MENU_ records per page",
"sSearch": ""
}
});
4.Html
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
<thead>
<tr>
<th style="width: 50px">No</th>
<th>Name</th>
<th>Density</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
希望这有助于
从春天获得相应的JSON数据到数据表中最简单的方法是,而不是返回你的实体的列表,返回一个图是这样的:
@RequestMapping(value = "/getWidgetsByType", method = RequestMethod.POST)
public @ResponseBody Map<String, WidgetVO> getWidgetsByType(@RequestParam("type") String type) {
Map<String, WidgetVO> result = new HashMap<>();
result.put("WidgetVO", widgetDAO.getWidgetsByType(token));
return result;
}
就是这样,现在你可以访问你的对象:
$(document).ready(function() {
$('#example').dataTable( {
"ajax": "data/objects.txt",
"dataSrc": "WidgetVO",
"columns": [
{ "data": "type" },
{ "data": "name" },
{ "data": "isAwesome" }
]
});
});
文章来源: How to populate rows of jQuery datatable with JSON object sent back from Spring MVC?