材料-ui中的多级嵌套列表

时间:2018-02-04 12:04:13

标签: reactjs user-interface menu material-ui nested-lists

我想创建多级嵌套列表以显示左侧的菜单 - 与非常类似于官方网站:https://material-ui-next.com/。 数据源是JSON,其中每个项目还有关于父项和其他一些数据的信息 - 这就是示例:

{
    "task_number": 1201092,
    "task": "Monthly Cash Flow from Deliveries",
    "task_parent_number": 1201090,
    "task_parent": "Wholesale Cash Flow"
},
{
    "task_number": 1201095,
    "task": "Monthly Cash Flow from Fix Amounts",
    "task_parent_number": 1201090,
    "task_parent": "Wholesale Cash Flow"
},
{
    "task_number": 1201100,
    "task": "Wholesale Positions",
    "task_parent_number": 1200007,
    "task_parent": "Wholesale Contract Portfolio"
},
{
    "task_number": 1201200,
    "task": "All Wholesale Positions",
    "task_parent_number": 1201100,
    "task_parent": "Wholesale Positions"
}

我能够使用各种嵌套元素创建一个对象 - children - 如果它们存在,则具有followin功能:

function getNestedChildren(arr, parent) {
  var out = [];
  for (var i in arr) {
    if (arr[i].task_parent_number == parent) {
      //console.log(i, arr[i].task_parent_number, arr[i].task_number);
      var children = getNestedChildren(arr, arr[i].task_number);
      if (children.length) {
        arr[i].children = children;
      }
      out.push(arr[i]);
    }
  }

  return out;
}

我一直在遵循创建嵌套列表并在此处导入的说明:https://material-ui-next.com/demos/lists/#nested-list

..但我无法根据需要创建具有嵌套元素的菜单。 。如果有人能指出我正确的方向会很棒..

1 个答案:

答案 0 :(得分:3)

好的,我让这个结合这两部分:

  1. React示例递归渲染:https://gist.github.com/nkvenom/bf7b1adfe982cb47dee3
  2. 此处的材料清单指南 - https://medium.com/@ali.atwa/getting-started-with-material-ui-for-react-59c82d9ffd93