如何使用从Spring MVC发送回的JSON对象填充jQuery数据表的行?


问题内容

我有一个 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.
}

在前端,我尝试/getWidgetsByTypejQuery $.getJSON调用中进行调用,然后使用从中返回的 JSON
结果填充datatable。具体来说,我希望数据表显示<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包含与( 字段 ,类型,名称,isAwesome)的 字段 相同的 ,全部作为值(无渲染器等)。
__WidgetVO``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": [
    [],[],[],...
  ]
}

这是显示即时设置列名称示例。

干杯!!