【问题标题】:Parsing LocalDate and list of nested objects in ajax response在ajax响应中解析LocalDate和嵌套对象列表
【发布时间】:2016-11-21 05:52:36
【问题描述】:

谁能帮帮我。 我正在尝试解决两个问题。首先,如何在 ajax 响应中显示 LocalDate。第二,遍历在 ajax 响应中收到的自定义对象列表。

我在 ajax 响应中传递了自定义对象列表和 localDate,但不确定如何在 UI 中显示它。提交的日期显示 [object object]。下面是我的ajax调用代码。

var table = $("#example").DataTable( {
        "bProcessing": true,
        "bServerSide": true,
        'sDom' : 'T<"clear">lrtip',
        "sort": "position",
        "sAjaxSource": "springPaginationDataTables.web",
        "aoColumns": [
            { "mData": "name" },
            { "mData": "position" },
            { "mData": "office" },
            { "mData": "phone" },
            { "mData": "start_date" },
            { "mData": "salary" },
            { "mData": "dob" },//LocalDate
            { "mData": "addresses" },//list of addresses

        ],
        columnDefs: [
                     {
                         targets: [ 6 ],
                         render: function ( data, type, row ) {
                             console.log(type);
                             return data;//process LocalDate here
                         }
                     },
                     {targets: [ 7 ],
                         render: function ( data, type, row ) {
                             console.log(type);
                             return data;//process addresses list here
                         }

                     }
                    ]
    } );

从 MVC 控制器我将人员列表传递给 JSON 对象,如下所示

PersonJsonObject personJsonObject = new PersonJsonObject();
        //Set Total display record
        personJsonObject.setiTotalDisplayRecords(500);
        //Set Total record
        personJsonObject.setiTotalRecords(500);
        personJsonObject.setAaData(personsList);

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String json2 = gson.toJson(personJsonObject);

        return json2;

PersonJsonObject 是 Jquery Datatable 类型的对象,它保存要在 UI 上显示的记录跟踪和数据列表。 Person 对象具有地址对象列表。获取列表后如何迭代列表说 data.getaddresses 并将其存储在变量中。我知道我们如何使用 jsp 标签遍历列表,但不知道如何在 Jquery 中做同样的事情

【问题讨论】:

    标签: javascript jquery json ajax list


    【解决方案1】:

    解决了。对于 LocalDate 问题,需要注册自定义模块。检查此链接以获取详细信息。 Ajax response does not parse LocalDate

    要遍历列表,请使用简单的 jquery foreach 循环

    $j.each(data, function (index, value) {
            var temp = value.field1+" "+value.field1+" ";//field1 and field2 are fields of custom object
            console.log(temp);
       return temp;
      });
    

    【讨论】:

      猜你喜欢
      • 2016-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-05
      • 2020-02-04
      • 1970-01-01
      • 2020-07-07
      • 2013-06-16
      相关资源
      最近更新 更多