【问题标题】:How can I show this list structure? Angular如何显示此列表结构?角
【发布时间】:2019-11-29 09:15:39
【问题描述】:

'Picture a' 是我从 AJAX 调用中获得的数据。当一个任务的“dependencyTaskId”等于另一个任务的 ID 时,它成为该任务的子任务。如果“dependencyTaskId”为空,则该任务将处于顶层。最终结果将显示所有任务的树结构(“图片 b”)。

我正在使用 Angular 6。我怎样才能做到这一点?请帮我。非常感谢。

  "orderId": 879530,
  "status": "Processing",
  "dateCreated": "2019-11-30T20:47:23.953",
  "dateCommitted": "2019-11-30T20:47:25.627",
  "dateModified": "2019-11-30T20:47:25.377",
  "coreProducts": [
    {
      "coreProductId": 1597739,
      "serviceAddress": {
        "addressId": 7506180,
        "locationId": "3472036",
        "floor": null,
        "buildingName": null,
        "flatNumber": null,
        "streetNumber": "30",
        "streetNumberSubDescription": null,
        "streetName": "LOGIE",
        "streetDirection": null,
        "suburb": "STOKES VALLEY",
        "city": "LOWER HUTT",
        "postCode": "5019",
        "freeTextAddressOverride": null,
        "attentionTo": null,
        "careOf": null,
        "addressType": "Service",
        "addressAccessInformation": null
      },
      "coreProductKey": "NGA1000/500",
      "contractLength": 12,
      "requestedDateOfProvisioning": "2019-12-07T00:00:00",
      "installationInstructions": null,
      "coreProductComponents": [
        {
          "coreProductComponentId": 2245296,
          "serviceIdentifier": "KoR157510004402",
          "service": "Internet",
          "ratePlan": "OR_access_NGA1000/500_U_Sep2019",
          "overrideCharge": 0,
          "electricityPriceOverrides": null
        }
      ],
      "coreProductCharges": null
    }
  ],
  "additionalProducts": [],
  "hardwareOrders": [
    {
      "hardwareOrderId": 1829471,
      "deliveryAddress": {
        "addressId": 7506179,
        "locationId": "3472036",
        "floor": null,
        "buildingName": null,
        "flatNumber": null,
        "streetNumber": "30",
        "streetNumberSubDescription": null,
        "streetName": "LOGIE",
        "streetDirection": null,
        "suburb": "STOKES VALLEY",
        "city": "LOWER HUTT",
        "postCode": "5019",
        "freeTextAddressOverride": null,
        "attentionTo": null,
        "careOf": null,
        "addressType": "Courier",
        "addressAccessInformation": null
      },
      "linkedServiceIdentifier": "KoR157510004402",
      "contract": null,
      "hardwareOrderItems": [
        {
          "hardwareOrderItemId": 1828173,
          "key": "NetCommNF18ACV_FIBRE_OR",
          "type": null
        }
      ]
    }
  ],
  "account": {
    "accountId": 1939814,
    "customers": [
      {
        "customerId": 1935666,
        "firstName": "CC1FE844",
        "lastName": "D007C",
        "middleName": "",
        "email": "76FC65F@gmail.com",
        "homePhoneNumber": null,
        "mobilePhoneNumber": "0212727822",
        "workPhoneNumber": null,
        "faxNumber": null,
        "dateOfBirth": "1956-03-05T00:00:00",
        "address": {
          "addressId": 7506178,
          "locationId": "3472036",
          "floor": null,
          "buildingName": null,
          "flatNumber": null,
          "streetNumber": "30",
          "streetNumberSubDescription": null,
          "streetName": "LOGIE",
          "streetDirection": null,
          "suburb": "STOKES VALLEY",
          "city": "LOWER HUTT",
          "postCode": "5019",
          "freeTextAddressOverride": null,
          "attentionTo": null,
          "careOf": null,
          "addressType": "PostalPhysical",
          "addressAccessInformation": null
        }
      }
    ],
    "organisationType": null,
    "organisationReference": null,
    "address": {
      "addressId": 7506177,
      "locationId": "3472036",
      "floor": null,
      "buildingName": null,
      "flatNumber": null,
      "streetNumber": "30",
      "streetNumberSubDescription": null,
      "streetName": "LOGIE",
      "streetDirection": null,
      "suburb": "STOKES VALLEY",
      "city": "LOWER HUTT",
      "postCode": "5019",
      "freeTextAddressOverride": null,
      "attentionTo": null,
      "careOf": null,
      "addressType": "PostalPhysical",
      "addressAccessInformation": null
    }
  },
  "losingServiceProviders": [
    {
      "losingServiceProviderId": 677663,
      "serviceIdentifier": "KoR157510004402",
      "serviceProvider": "ihug",
      "accountName": "CEDC5BFE-53EE-4319",
      "accountNumber": "20387451"
    }
  ],
  "questions": [
    {
      "questionAnswerId": 6329580,
      "question": "LiveThereOrMovingIn",
      "answer": "LiveThere",
      "serviceIdentifier": "KoR157510004402"
    },
    {
      "questionAnswerId": 6329581,
      "question": "WhatServicesCurrentlyHave",
      "answer": "BroadbandOnly",
      "serviceIdentifier": "KoR157510004402"
    }
  ],
  "promoCodes": [
    {
      "promoCodeId": 2168141,
      "promoCode": "GIGANTIC_PROMO",
      "serviceIdentifier": "KoR157510004402"
    }
  ],
  "tasks": [
    {
      "taskId": 7670266,
      "name": "ChorusGatewayNGAConnectPrimary",
      "description": "Creates Chorus Gateway NGA NewConnection ticket",
      "displayName": "Chorus Gateway NGA Connect Primary",
      "dateStarted": "2019-11-30T20:47:34.617",
      "dateCompleted": null,
      "dateScheduled": null,
      "taskSource": "CoreProducts",
      "taskSourceId": "1597739",
      "dependencyTaskId": null,
      "ticketId": 36503526,
      "ticketStatus": "With Provider",
      "ticketPercentageComplete": 40,
      "externalReferenceId": null,
      "hasFailure": false,
      "failureReason": "Complete"
    },
    {
      "taskId": 7670268,
      "name": "InternetActivation",
      "description": "*InternetActivation",
      "displayName": "Internet Activation",
      "dateStarted": null,
      "dateCompleted": null,
      "dateScheduled": null,
      "taskSource": "CoreProducts",
      "taskSourceId": "1597739",
      "dependencyTaskId": 7670266,
      "ticketId": null,
      "ticketStatus": null,
      "ticketPercentageComplete": null,
      "externalReferenceId": null,
      "hasFailure": null,
      "failureReason": null
    },
    {
      "taskId": 7670271,
      "name": "InternetUsageEvaluation",
      "description": "Evaluates if the customer has internet usage",
      "displayName": "Internet Usage Evaluation",
      "dateStarted": null,
      "dateCompleted": null,
      "dateScheduled": null,
      "taskSource": "CoreProducts",
      "taskSourceId": "1597739",
      "dependencyTaskId": 7670268,
      "ticketId": null,
      "ticketStatus": null,
      "ticketPercentageComplete": null,
      "externalReferenceId": null,
      "hasFailure": null,
      "failureReason": null
    },
    {
      "taskId": 7670272,
      "name": "CustomerCoreProductInsert",
      "description": "Store LineNumber/Service data of a CoreProduct to customer tables.",
      "displayName": "Customer Core Product Insert",
      "dateStarted": null,
      "dateCompleted": null,
      "dateScheduled": null,
      "taskSource": "CoreProducts",
      "taskSourceId": "1597739",
      "dependencyTaskId": 7670268,
      "ticketId": null,
      "ticketStatus": null,
      "ticketPercentageComplete": null,
      "externalReferenceId": null,
      "hasFailure": null,
      "failureReason": null
    },
    {
      "taskId": 7670273,
      "name": "NoUsageComms",
      "description": "*NoUsageComms",
      "displayName": "No Usage Comms",
      "dateStarted": null,
      "dateCompleted": null,
      "dateScheduled": null,
      "taskSource": "CoreProducts",
      "taskSourceId": "1597739",
      "dependencyTaskId": 7670266,
      "ticketId": null,
      "ticketStatus": null,
      "ticketPercentageComplete": null,
      "externalReferenceId": null,
      "hasFailure": null,
      "failureReason": null
    },
    {
      "taskId": 7670269,
      "name": "Hardware",
      "description": "*Hardware",
      "displayName": "Order Hardware",
      "dateStarted": "2019-11-30T20:51:41.22",
      "dateCompleted": null,
      "dateScheduled": null,
      "taskSource": "HardwareOrders",
      "taskSourceId": "1828173",
      "dependencyTaskId": 7670266,
      "ticketId": 36503527,
      "ticketStatus": "Scheduled",
      "ticketPercentageComplete": 10,
      "externalReferenceId": null,
      "hasFailure": false,
      "failureReason": "Complete"
    },
    {
      "taskId": 7670270,
      "name": "PromoCode",
      "description": "Promo code task",
      "displayName": "Promo Code Task",
      "dateStarted": null,
      "dateCompleted": null,
      "dateScheduled": null,
      "taskSource": "PromoCodes",
      "taskSourceId": "2168141",
      "dependencyTaskId": 7670268,
      "ticketId": null,
      "ticketStatus": null,
      "ticketPercentageComplete": null,
      "externalReferenceId": null,
      "hasFailure": null,
      "failureReason": null
    },
    {
      "taskId": 7670267,
      "name": "SaveQuestionsAndAnswers",
      "description": "Save Questions And Answers",
      "displayName": "Save Questions And Answers",
      "dateStarted": "2019-11-30T20:47:33.307",
      "dateCompleted": "2019-11-30T20:47:33.307",
      "dateScheduled": null,
      "taskSource": "SaveQuestionsAndAnswers",
      "taskSourceId": "6329580",
      "dependencyTaskId": null,
      "ticketId": null,
      "ticketStatus": null,
      "ticketPercentageComplete": null,
      "externalReferenceId": null,
      "hasFailure": false,
      "failureReason": "Complete"
    }
  ],
  "fulfilmentItems": []
}

【问题讨论】:

  • 您可以控制这个 API 吗?如果是,那么我建议你在服务器上准备 JSON 数据作为父子数据,所以在前端你不需要做所有的事情
  • 你需要一个递归函数
  • 我愿意。据我了解,这种操作最好发生在前端。如我错了请纠正我。干杯
  • 可以分享一下json数据吗?
  • @hbamithkumara。 Json 被添加到问题中。谢谢

标签: javascript angular iteration hierarchy


【解决方案1】:

你必须通过前端试试这个:

const apiResponse = {"orderId": 879530, "status": "Processing", "dateCreated": "2019-11-30T20:47:23.953", "dateCommitted": "2019-11-30T20:47:25.627", "dateModified": "2019-11-30T20:47:25.377", "coreProducts": [ { "coreProductId": 1597739, "serviceAddress": { "addressId": 7506180, "locationId": "3472036", "floor": null, "buildingName": null, "flatNumber": null, "streetNumber": "30", "streetNumberSubDescription": null, "streetName": "LOGIE", "streetDirection": null, "suburb": "STOKES VALLEY", "city": "LOWER HUTT", "postCode": "5019", "freeTextAddressOverride": null, "attentionTo": null, "careOf": null, "addressType": "Service", "addressAccessInformation": null }, "coreProductKey": "NGA1000/500", "contractLength": 12, "requestedDateOfProvisioning": "2019-12-07T00:00:00", "installationInstructions": null, "coreProductComponents": [ { "coreProductComponentId": 2245296, "serviceIdentifier": "KoR157510004402", "service": "Internet", "ratePlan": "OR_access_NGA1000/500_U_Sep2019", "overrideCharge": 0, "electricityPriceOverrides": null } ], "coreProductCharges": null } ], "additionalProducts": [], "hardwareOrders": [ { "hardwareOrderId": 1829471, "deliveryAddress": { "addressId": 7506179, "locationId": "3472036", "floor": null, "buildingName": null, "flatNumber": null, "streetNumber": "30", "streetNumberSubDescription": null, "streetName": "LOGIE", "streetDirection": null, "suburb": "STOKES VALLEY", "city": "LOWER HUTT", "postCode": "5019", "freeTextAddressOverride": null, "attentionTo": null, "careOf": null, "addressType": "Courier", "addressAccessInformation": null }, "linkedServiceIdentifier": "KoR157510004402", "contract": null, "hardwareOrderItems": [ { "hardwareOrderItemId": 1828173, "key": "NetCommNF18ACV_FIBRE_OR", "type": null } ] } ], "account": { "accountId": 1939814, "customers": [ { "customerId": 1935666, "firstName": "CC1FE844", "lastName": "D007C", "middleName": "", "email": "76FC65F@gmail.com", "homePhoneNumber": null, "mobilePhoneNumber": "0212727822", "workPhoneNumber": null, "faxNumber": null, "dateOfBirth": "1956-03-05T00:00:00", "address": { "addressId": 7506178, "locationId": "3472036", "floor": null, "buildingName": null, "flatNumber": null, "streetNumber": "30", "streetNumberSubDescription": null, "streetName": "LOGIE", "streetDirection": null, "suburb": "STOKES VALLEY", "city": "LOWER HUTT", "postCode": "5019", "freeTextAddressOverride": null, "attentionTo": null, "careOf": null, "addressType": "PostalPhysical", "addressAccessInformation": null } } ], "organisationType": null, "organisationReference": null, "address": { "addressId": 7506177, "locationId": "3472036", "floor": null, "buildingName": null, "flatNumber": null, "streetNumber": "30", "streetNumberSubDescription": null, "streetName": "LOGIE", "streetDirection": null, "suburb": "STOKES VALLEY", "city": "LOWER HUTT", "postCode": "5019", "freeTextAddressOverride": null, "attentionTo": null, "careOf": null, "addressType": "PostalPhysical", "addressAccessInformation": null } }, "losingServiceProviders": [ { "losingServiceProviderId": 677663, "serviceIdentifier": "KoR157510004402", "serviceProvider": "ihug", "accountName": "CEDC5BFE-53EE-4319", "accountNumber": "20387451" } ], "questions": [ { "questionAnswerId": 6329580, "question": "LiveThereOrMovingIn", "answer": "LiveThere", "serviceIdentifier": "KoR157510004402" }, { "questionAnswerId": 6329581, "question": "WhatServicesCurrentlyHave", "answer": "BroadbandOnly", "serviceIdentifier": "KoR157510004402" } ], "promoCodes": [ { "promoCodeId": 2168141, "promoCode": "GIGANTIC_PROMO", "serviceIdentifier": "KoR157510004402" } ], "tasks": [ { "taskId": 7670266, "name": "ChorusGatewayNGAConnectPrimary", "description": "Creates Chorus Gateway NGA NewConnection ticket", "displayName": "Chorus Gateway NGA Connect Primary", "dateStarted": "2019-11-30T20:47:34.617", "dateCompleted": null, "dateScheduled": null, "taskSource": "CoreProducts", "taskSourceId": "1597739", "dependencyTaskId": null, "ticketId": 36503526, "ticketStatus": "With Provider", "ticketPercentageComplete": 40, "externalReferenceId": null, "hasFailure": false, "failureReason": "Complete" }, { "taskId": 7670268, "name": "InternetActivation", "description": "*InternetActivation", "displayName": "Internet Activation", "dateStarted": null, "dateCompleted": null, "dateScheduled": null, "taskSource": "CoreProducts", "taskSourceId": "1597739", "dependencyTaskId": 7670266, "ticketId": null, "ticketStatus": null, "ticketPercentageComplete": null, "externalReferenceId": null, "hasFailure": null, "failureReason": null }, { "taskId": 7670271, "name": "InternetUsageEvaluation", "description": "Evaluates if the customer has internet usage", "displayName": "Internet Usage Evaluation", "dateStarted": null, "dateCompleted": null, "dateScheduled": null, "taskSource": "CoreProducts", "taskSourceId": "1597739", "dependencyTaskId": 7670268, "ticketId": null, "ticketStatus": null, "ticketPercentageComplete": null, "externalReferenceId": null, "hasFailure": null, "failureReason": null }, { "taskId": 7670272, "name": "CustomerCoreProductInsert", "description": "Store LineNumber/Service data of a CoreProduct to customer tables.", "displayName": "Customer Core Product Insert", "dateStarted": null, "dateCompleted": null, "dateScheduled": null, "taskSource": "CoreProducts", "taskSourceId": "1597739", "dependencyTaskId": 7670268, "ticketId": null, "ticketStatus": null, "ticketPercentageComplete": null, "externalReferenceId": null, "hasFailure": null, "failureReason": null }, { "taskId": 7670273, "name": "NoUsageComms", "description": "*NoUsageComms", "displayName": "No Usage Comms", "dateStarted": null, "dateCompleted": null, "dateScheduled": null, "taskSource": "CoreProducts", "taskSourceId": "1597739", "dependencyTaskId": 7670266, "ticketId": null, "ticketStatus": null, "ticketPercentageComplete": null, "externalReferenceId": null, "hasFailure": null, "failureReason": null }, { "taskId": 7670269, "name": "Hardware", "description": "*Hardware", "displayName": "Order Hardware", "dateStarted": "2019-11-30T20:51:41.22", "dateCompleted": null, "dateScheduled": null, "taskSource": "HardwareOrders", "taskSourceId": "1828173", "dependencyTaskId": 7670266, "ticketId": 36503527, "ticketStatus": "Scheduled", "ticketPercentageComplete": 10, "externalReferenceId": null, "hasFailure": false, "failureReason": "Complete" }, { "taskId": 7670270, "name": "PromoCode", "description": "Promo code task", "displayName": "Promo Code Task", "dateStarted": null, "dateCompleted": null, "dateScheduled": null, "taskSource": "PromoCodes", "taskSourceId": "2168141", "dependencyTaskId": 7670268, "ticketId": null, "ticketStatus": null, "ticketPercentageComplete": null, "externalReferenceId": null, "hasFailure": null, "failureReason": null }, { "taskId": 7670267, "name": "SaveQuestionsAndAnswers", "description": "Save Questions And Answers", "displayName": "Save Questions And Answers", "dateStarted": "2019-11-30T20:47:33.307", "dateCompleted": "2019-11-30T20:47:33.307", "dateScheduled": null, "taskSource": "SaveQuestionsAndAnswers", "taskSourceId": "6329580", "dependencyTaskId": null, "ticketId": null, "ticketStatus": null, "ticketPercentageComplete": null, "externalReferenceId": null, "hasFailure": false, "failureReason": "Complete" } ], "fulfilmentItems": [] }

const nestedChild = (inputArray, parentKeyName, childKeyName, relationKeyValue = null) => {
    const tree = []
    for(let i in inputArray) {
        if(inputArray[i][childKeyName] == relationKeyValue) {
            let children = nestedChild(inputArray, parentKeyName, childKeyName, inputArray[i][parentKeyName])

            if(children.length) {
                inputArray[i].children = children
            }
            tree.push(inputArray[i])
        }
    }
    return tree
}



apiResponse.tasks = nestedChild(
  apiResponse.tasks, 
  'taskId',
  'dependencyTaskId',
  null
);

console.log(apiResponse);

输出:

现在可以遍历并打印成无序列表,也可以通过css格式化。

希望这对你有很大帮助。

【讨论】:

  • 感谢您的回答。我想知道这个解决方案是否适用于多级嵌套?再次感谢。
  • 是的,它也适用于多级嵌套项。
  • 我今天试了一下,但似乎没有用。它总是返回空数组。
  • 嗨@Mark,我已经更新了代码,我已经将es5转换为es6并且犯了一个错误,我已经纠正并更新了它。如果仍然不起作用,请分享示例 JSON 数据。
  • @Aman,感谢您的更新,但仍然无法正常工作。 Json 已添加到问题中。
【解决方案2】:

您可以使用angular-tree-component 来显示任务。

为了使用此组件,您必须将平面任务数组转换为节点树,其中每个节点都包含对其子节点的引用。这可以使用一个简单的递归函数来完成。

工作示例:https://stackblitz.com/edit/angular-tree-component-u3gmlp-r9hqpg

(您可能希望将其配置为更像您提供的屏幕截图)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 2021-11-26
    • 2022-01-23
    • 2017-01-19
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多