简体   繁体   English

如何使用 javascript 创建具有动态 json 数据的 el-table-tree 结构?

[英]How to create a el-table-tree structure with dynamic json data using javascript?

I have a task in which I need to convert an Array of Objects data to specified data tree structure for to display it in frontend tree-table .我有一个任务,我需要将一个对象数组数据转换为指定的数据树结构,以便在前端tree-table中显示它。 This below is my Array of Objects data.下面是我的对象数组数据。

<!-- This is my JSON Data -->
dictionary: {
  models: [{
      model: 'form',
      attributes: [{
          id: 1,
          name: 'field_01',
          displayName: 'Field 01',
          type: 'string'
        },
        {
          id: 2,
          name: 'field_02',
          displayName: 'Field 02',
          type: 'object',
          source: 'subForm'
        }]
    },
    {
      model: 'subForm',
      attributes: [{
          id: 11,
          name: 'subForm_01',
          displayName: 'Field 01',
          type: 'string'
        },
        {
          id: 12,
          name: 'subForm_02',
          displayName: 'Field 02',
          type: 'object',
          source: 'dialogForm'
        }]
    },
    {
      model: 'dialogForm',
      attributes: [{
          id: 21,
          name: 'dialogForm_01',
          displayName: 'Field 01',
          type: 'string'}]
    }]
}

I have to change this data into this below following data structure.我必须将此数据更改为以下数据结构。

<!-- Desired Output -->
tableData: [{
    id: 1,
    name: 'field_01',
    displayName: 'Field 01',
    type: 'string'
},
  {
    id: 2,
    name: 'field_02',
    displayName: 'Field 02',
    type: 'object',
    source: 'subForm'
    children: [{
      id: 11,
      name: 'subForm_01',
      displayName: 'Field 01',
      type: 'string'
    },
    {
      id: 12,
      name: 'subForm_02',
      displayName: 'Field 02',
      type: 'object',
      source: 'dialogForm',
      children: [{
        id: 21,
        name: 'dialogForm_01',
        displayName: 'Field 01',
        type: 'string'}]
        }]
      }]

I have tried to do this using Recursion in methods but, I can't do that in a way that I want.我曾尝试在方法中使用递归来做到这一点,但是我不能以我想要的方式做到这一点。 My task is I have to add an children whenever the type: 'object' is object with another referenced object inside that array of objects.我的任务是,只要type: 'object'是 object 并且在该对象数组中引用另一个 object,我必须添加一个孩子。 And by the way the data is dyanmic which is from mongodb database.顺便说一下,数据是动态的,来自 mongodb 数据库。 That's just a simplified version of the code.这只是代码的简化版本。 If my information is not enough or seemed unreasonable sorry for that, I am fresher so, I just asked what i want.如果我的信息不足或对此似乎不合理感到抱歉,我更新鲜,我只是问我想要什么。 Thank you in advance for the help.预先感谢您的帮助。

First第一的

First, two notes:首先,两个注意事项:

  • On StackOverflow, we expect you to do your own work, and to present your best attempt, explaining where it went wrong.在 StackOverflow 上,我们希望您做自己的工作,并展示您的最佳尝试,解释哪里出了问题。 This is a question-and-answer site, not a code-it-for-you one.这是一个问答网站,而不是为您编写代码的网站。 I'm going to try to answer you this time, but for the future, please read up on asking good questions .这次我会尝试回答你,但对于未来,请阅读提出好的问题

  • Your comment "This is my JSON Data" is not accurate.您的评论“这是我的 JSON 数据”不准确。 JSON is a string format, used to transfer data between different systems or store it. JSON 是一种字符串格式,用于不同系统之间的数据传输或存储。 What you have here is plain JavaScript data structures.您在这里拥有的是普通的 JavaScript 数据结构。

Answer回答

I think this is a fairly simple solution.我认为这是一个相当简单的解决方案。 But it makes one assumption that may not be justified.但它提出了一个可能不合理的假设。 I didn't see any way to distinguish your root objects in your input structure, except that they were first.我没有看到任何方法来区分输入结构中的根对象,除了它们是第一个。 I chose to use the first model as the source of your root objects.我选择使用第一个model作为根对象的来源。 If you need to choose instead the one that has the model name of "form" , we could change that easily enough.如果您需要选择 model 名称为"form"的那个,我们可以很容易地更改它。

The code looks like this:代码如下所示:

 const convertModels = (models, key) => models.filter (({model}) => model == key).flatMap ( ({attributes}) => attributes.map (att => ({... att, ... (att.type == 'object'? {children: convertModels (models, att.source)}: {}) })) ) const convert = (dictionary) => ({ tableData: convertModels (dictionary.models, dictionary.models [0].model) }) const dictionary = {models: [{model: "form", attributes: [{id: 1, name: "field_01", displayName: "Field 01", type: "string"}, {id: 2, name: "field_02", displayName: "Field 02", type: "object", source: "subForm"}]}, {model: "subForm", attributes: [{id: 11, name: "subForm_01", displayName: "Field 01", type: "string"}, {id: 12, name: "subForm_02", displayName: "Field 02", type: "object", source: "dialogForm"}]}, {model: "dialogForm", attributes: [{id: 21, name: "dialogForm_01", displayName: "Field 01", type: "string"}]}]} console.log (convert (dictionary))
 .as-console-wrapper {max-height: 100%;important: top: 0}

Our main function is convertModels , which accepts a list of models and the key we're going to search for.我们的主要 function 是convertModels ,它接受模型列表和我们要搜索的键。 It searches the models for those one(s) with that key, and then flat-maps their attributes by copying all properties and, if the type is "object" , adding a children node by recurring on the models and the source field.它使用该键在模型中搜索那些模型,然后通过复制所有属性来平面映射它们的attributes ,如果类型是"object" ,则通过在模型和source字段上重复添加children节点。

We wrap this in convert which handles your outer object, its models property convert along with the first model's model property, storing the result in a tableData property of a new object.我们将其包装在处理外部 object 的convert中,它的models属性与第一个模型的model属性一起convert ,将结果存储在新 object 的tableData属性中。


If you did want to use the "form" identifier instead of using the first record, the code is slightly simpler:如果您确实想使用"form"标识符而不是使用第一条记录,那么代码会稍微简单一些:

const convertModels = (models, key = 'form') =>  // simpler
  models .filter (({model}) => model == key) .flatMap (
    ({attributes}) => attributes .map (att => ({
      ... att,
      ... (att.type == 'object' ? {children: convertModels (models, att.source)} : {})
    }))
  )

const convert = (dictionary) => ({
  tableData: convertModels (dictionary .models)  // simpler
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM