【问题标题】:render columns value in nested json objects in JQuery Datatables在 JQuery 数据表中的嵌套 json 对象中呈现列值
【发布时间】:2018-05-21 06:09:15
【问题描述】:

我想将嵌套 json 对象的列数据渲染到 JQuery Datatable Cell。我为此尝试了几个小时,但未能实现。以下是我的 Json 结果。

JSON

    [
    {
        "auditSectionId": 2,
        "associateAuditId": 2,
        "associateAudit": {
            "id": 2,
            "auditTitle": "Duis id justo quis nibh facilisis rutrum.",
            "year": 2011,
            "term": 1,
            "createdUser": null,
            "createdUserUserId": 2,
            "createdDate": "2018-05-19T11:16:13.553",
            "lastUpdatedUser": null,
            "lastUpdatedUserUserId": null,
            "lastUpdatedDate": null,
            "updatedCount": 0,
            "isDeleted": false,
            "auditOfAuditSection": []
        },
        "auditAreaDepartmentId": 1,
        "auditArea": {
            "departmentId": 1,
            "departmentName": "MIS",
            "location": "D Block Ground Flow",
            "description": "All are qualified",
            "personInIncharge": null,
            "personInInchargeEmployeeId": 23,
            "responsibleDepartmentForCalibrationLog": null,
            "responsibleDepartmentForServiceLog": null,
            "locationOfEquipment": null,
            "departmentOfAuditSection": []
        },
        "plannedDateTime": "2018-05-01T13:00:00",
        "conductedDateTime": "2018-05-09T11:30:00",
        "auditor": [
            {
                "teamMember": {
                    "schedules": [],
                    "employeeId": 2,
                    "employeeNo": "00005339",
                    "employeeStatus": true,
                    "epfNo": "00000325",
                    "nameWithInitial": "R E PRASANDINI",
                    "callingName": "Erandika",
                    "department": {
                        "departmentId": 1,
                        "departmentName": "MIS",
                        "location": "D Block Ground Flow",
                        "description": "All are qualified",
                        "personInIncharge": null,
                        "personInInchargeEmployeeId": 23,
                        "responsibleDepartmentForCalibrationLog": null,
                        "responsibleDepartmentForServiceLog": null,
                        "locationOfEquipment": null,
                        "departmentOfAuditSection": []
                    },
                    "departmentDepartmentId": 1,
                    "appoinmentDate": "2011-11-08T00:00:00",
                    "designation": "Production Executive",
                    "isResigned": false,
                    "resignedDate": "2017-02-07T00:00:00",
                    "isTerminated": false,
                    "isRetired": false,
                    "isTrainee": false,
                    "isSkilled": false,
                    "isUnSkilled": false,
                    "isSemiSkilled": false,
                    "isManagerial": false,
                    "isContract": false,
                    "contractEndDate": "2016-07-15T00:00:00",
                    "isSupervisor": false,
                    "isSecurity": false,
                    "isOfficeStaff": true,
                    "isTechnical": false,
                    "qualifications": "",
                    "gender": "Female",
                    "profile": null,
                    "profileId": 1,
                    "personInIncharge": null,
                    "recieverCommunication": null,
                    "senderCommunication": null,
                    "responsiblePersonForCalibrationLog": null,
                    "responsiblePersonForServiceLog": null,
                    "employeeCommunicationAchievementResponsibility": null
                },
                "teamMemberEmployeeId": 2,
                "allowableScope": "Duis id justo quis nibh facilisis rutrum.",
                "qualification": "Duis id justo quis nibh facilisis rutrum.",
                "experience": "Duis id justo quis nibh facilisis rutrum.",
                "isActive": true,
                "section": []
            }
        ],
        "createdUser": null,
        "createdUserUserId": 2,
        "createdDate": "2018-05-19T11:24:26.907",
        "lastUpdatedUser": null,
        "lastUpdatedUserUserId": null,
        "lastUpdatedDate": null,
        "updatedCount": 0,
        "isDeleted": false
    }
]

以下json数据段出现问题

"auditor": [
        {
            "teamMember": {
                "schedules": [],
                "employeeId": 2,
                "employeeNo": "00005339",
                "employeeStatus": true,
                "epfNo": "00000325",
                "nameWithInitial": "R E PRASANDINI",
                "callingName": "Erandika",
                "department": {
                    "departmentId": 1,
                    "departmentName": "MIS",
                    "location": "D Block Ground Flow",
                    "description": "All are qualified",
                    "personInIncharge": null,
                    "personInInchargeEmployeeId": 23,
                    "responsibleDepartmentForCalibrationLog": null,
                    "responsibleDepartmentForServiceLog": null,
                    "locationOfEquipment": null,
                    "departmentOfAuditSection": []
                },
                "departmentDepartmentId": 1,
                "appoinmentDate": "2011-11-08T00:00:00",
                "designation": "Production Executive",
                "isResigned": false,
                "resignedDate": "2017-02-07T00:00:00",
                "isTerminated": false,
                "isRetired": false,
                "isTrainee": false,
                "isSkilled": false,
                "isUnSkilled": false,
                "isSemiSkilled": false,
                "isManagerial": false,
                "isContract": false,
                "contractEndDate": "2016-07-15T00:00:00",
                "isSupervisor": false,
                "isSecurity": false,
                "isOfficeStaff": true,
                "isTechnical": false,
                "qualifications": "",
                "gender": "Female",
                "profile": null,
                "profileId": 1,
                "personInIncharge": null,
                "recieverCommunication": null,
                "senderCommunication": null,
                "responsiblePersonForCalibrationLog": null,
                "responsiblePersonForServiceLog": null,
                "employeeCommunicationAchievementResponsibility": null
            },

我想将嵌套在auditor.teamMember 中的auditor namewithinitials 列呈现到JQuery Datatables 单元格中。 AuditorSection 和 Employee 表具有多对多关系。 到目前为止,我编写了如下的 JQuery Datatable 渲染方法

{
  data: "auditor.[,].teamMember",
  render: function (data, type, row) {                                
         alert(data.nameWithInitial);
         return data;
    }
 }

但这行不通。我是数据表的新手。所以请给我任何帮助和建议来解决这个问题。谢谢

【问题讨论】:

    标签: jquery json datatables nested


    【解决方案1】:

    谢谢大家 我找到了解决方案。我只需要从审计对象数组中获取 teamMember.callingName 对象。

    {
        //8
        data: "auditor.[,].teamMember.callingName",
        render: function (data, type, row) {
           return data;
        }
    }
    

    【讨论】:

      【解决方案2】:

      在我正在使用的这个数据 JSON 模式中:

      {
        id: 3271,
        date: "2019-12-18",
        description: "Lots of text",
        agreements: [
          {
            agreement: {
              description: "Ok. "
            }
          }
        ]
      }
      

      我在访问上述 JSON 数据时遇到问题,我可以解决它

      
      $('.table-data-correspondencia').DataTable({
              "ajax": {
                  "processing": true,
                  "url": "agreements_data.json",
                  "dataSrc": ""
              },
              "columns": [
                  { "data": "id" },
                  { "data": "date" },
                  { "data": "description" },
                  { "data": "agreements.[,].agreement.description" },
              ],
                  "lengthChange": false,
                  "bSort": false,
              responsive: true,
                  language: {
                      search: "_INPUT_",
                      searchPlaceholder: "Buscar",
                      info: "Mostrando _PAGE_ de _PAGES_",
                      "paginate": {
                          "next": "Siguiente página",
                          "previous": "Página Anterior"
                      }
                  }
              });
      
      

      我希望这可以帮助别人!

      【讨论】:

        猜你喜欢
        • 2020-12-28
        • 1970-01-01
        • 2016-09-16
        • 2018-05-27
        • 2021-06-24
        • 1970-01-01
        • 2018-03-16
        • 1970-01-01
        • 2021-11-12
        相关资源
        最近更新 更多