【问题标题】:Spring MVC + jQuery Datatables + JSON ResponseSpring MVC + jQuery 数据表 + JSON 响应
【发布时间】:2016-02-09 03:23:06
【问题描述】:

我有一个使用 jQuery Datatables 呈现数据的 Spring MVC 应用程序。 这是我的课程和 javascript 代码

Employee.java

public class Employee {
    private int empId ;
    private String firstName;
    private String lastName ;

    // getters and setters
}

EmployeeResponse.java

public class EmployeeResponse {

    private List<Employee> empList ; 

    // getters and setters

}

EmployeeController.java

@Controller
@RequestMapping("/admin")
public class EmployeeController {

    @RequestMapping(value = "/get-all-employee", method = RequestMethod.POST, consumes = { MediaType.APPLICATION_JSON_VALUE })
    @ResponseBody
    public EmployeeResponse getAllEmployee(HttpServletRequest request) {
        EmployeeResponse response = new EmployeeResponse();
        try {

            response = getAllEmployeeList(); // returns response object

        } catch (Exception e) {
            logger.error("DCConsoleController::createNewStream exception: " + com.priceline.utils.StringUtils.getStackTrace(e));
            return null;
        }
        return response ; 
    }
}

emp.jsp

<div class="row">
    <table id="ldapStreamTable" class="table">
        <thead>
            <tr>
                <th>Emp Id</th>
                <th>First Name</th>
                <th>Last Name</th>
            </tr>
        </thead>
    </table>
</div>

emp.js

$(document).ready(function() {
    var table = $('#appTable').DataTable( {
        "sDom" : domSetting,
        "oLanguage" : {
            "sLengthMenu" : "_MENU_ records per page",
            "sSearch" : "<span class='add-on'><i class='icon-search'></i></span>Search Application:",
            "sZeroRecords" : "No matching records found",
        },
        "iDisplayLength" : 10,
        "aLengthMenu" : [
                [ 5, 10, 20, 25, 50, -1 ],
                [ 5, 10, 20, 25, 50, "All" ] ],
        "aaSorting" : [ [ 0, 'asc' ] ],
        'sAjaxSource': '{context}/admin/get-all-employee',
        [Pending here]
    });
});

我应该怎么做才能以 json 格式获取响应并为每一列应用/使用渲染方法以将数据渲染到表中?

[注意:看起来我不能对每列使用带有 mData 的 aoColumns',因为我的 json 响应是员工对象列表]

更新 - 1:下面的示例 json

{"empList":[{"empId":3,"firstName":"Kiran","lastName":"Kumar"},{"empId":1,"firstName":"John","lastName":"Smith"},{"empId":0,"firstName":"Sachin","lastName":"Kumar"}]}

【问题讨论】:

  • 您是否考虑过使用 Gson 或类似的库?
  • 我来自控制器的响应已经是使用 Jackson API 的 json 格式。我无法弄清楚的是如何将作为 List 对象的 json 响应绑定到 UI 上的列中
  • 你能发布你的json回复吗?
  • @GuruprasadRao - 使用示例 json 响应更新了问题。

标签: java jquery spring-mvc datatables


【解决方案1】:

我已将您的样本 ajax 数据保存在 github 中,并将其作为 ajax 来源提供。您将那部分保留在您的method 中,它返回json 响应,但不是empList 作为json 响应中的基本根,而是将其替换为data,正如我在上面所做的githubjson文件。现在除了ajaxSource,您还需要将columns 映射到您的dataTables,如下所示:

"columns": [
         { "data": "empId"},//should match column value in json file
         { "data": "firstName"},//should match column value in json file
         { "data": "lastName"}//should match column value in json file
]

A Demo here

【讨论】:

  • 我只是想出了另一种方法,通过设置 "sAjaxDataProp" : 'streamList' 和 "aoColumns" : [ {mDataProp: "empId"}, {mDataProp: "lastName"},{mDataProp : “名”} ]。但是,我使用 mRender 选项生成一个按钮,在呈现表格后如何将事件绑定到它。 fnRowCallback ?
  • 给你的按钮一个class 并添加事件到class
  • 这正是我所做的,但不知何故点击事件没有被注册。
  • 我认为你在这里没有做event delegation ..由于你将稍后部分的按钮控件渲染到DOM,你必须做event delegation,比如附加控制一些element,其中包含button 元素并已在 DOM 加载.. 例如:document,它是根。所以你做$(document).on('click','.yourButtonClass',function(){//click event methods});
  • 当然,这可能也可以。但我最终使用 fnDrawCallback 并将点击事件绑定到您前面提到的类。使用 fnDrawCallback 选项似乎是一种干净的方法。感谢您的所有帮助。
猜你喜欢
  • 2017-10-15
  • 1970-01-01
  • 2011-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-09
  • 1970-01-01
相关资源
最近更新 更多