如何使用从Spring MVC发回的JSON对象来填充jQuery datatable行? [英] How to populate rows of jQuery datatable with JSON object sent back from Spring MVC?

查看:190
本文介绍了如何使用从Spring MVC发回的JSON对象来填充jQuery datatable行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java (Spring MVC)后端,将 POJO 返回为对象,如下所示:

I have a Java (Spring MVC) backend that returns POJOs as JSON objects as follows:

@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.
}

在前端我试图从一个 jQuery $。getJSON / getWidgetsByType >调用,然后使用从其返回的 JSON 结果来填充 datatable 。具体来说,我希望datatable显示在一个< div> 标签中,当前在页面加载时为空,如下所示:

In the frontend I'm trying to call /getWidgetsByType from inside a jQuery $.getJSON call, and then use the JSON results coming back from that to populate a datatable. Specifically, I want the datatable to appear inside a <div> tag that is currently empty at page load as follows:

<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 (type,name,isAwesome)的字段,全部为字符串值(没有渲染器等)。

I would like the datatable to contain the same columns as the fields of the WidgetVO (type, name, isAwesome), all as String values (no renderers, etc.).

提前感谢您的帮助!

推荐答案

示例可以提供所需的所有详细信息。

Example from the datatable site gives you all the details required.

示例JS代码

$(document).ready(function() {
  $('#example').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "scripts/server_processing.php" // for you it will be - /getWidgetsByType
  } );
} );

你的json应该是这样的

Your json should be something like this

{
  "sEcho": 1,
  "iTotalRecords": "57",
  "iTotalDisplayRecords": "57",
  "aaData": [
    [],[],[],...
  ]
}

以下是示例,用于设置列名称

Here's example showing to set the column names on the fly.

干杯!!

这篇关于如何使用从Spring MVC发回的JSON对象来填充jQuery datatable行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆