Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用角网格创建多级网格_Javascript_Html_Angular - Fatal编程技术网

Javascript 使用角网格创建多级网格

Javascript 使用角网格创建多级网格,javascript,html,angular,Javascript,Html,Angular,我有一个需要创建多级网格的需求。其中,当用户单击复选框时,将打开子网格。请查看下面显示的HTML: 检查 后果 测验 描述 测试 描述 问题是:你有什么?你想要什么? ... 在向数据中添加项目时,一个想法是 一开始你有 this.data=[ {id:1, desc:'test', level: 0}, {id:2, desc:'testing',level: 0} ] 如果单击您收到的“1” [ {id:5, desc: 'test test', level: 1} ] 您可以在

我有一个需要创建多级网格的需求。其中,当用户单击复选框时,将打开子网格。请查看下面显示的HTML:


检查
后果
测验
描述
测试
描述

问题是:你有什么?你想要什么? ... 在向数据中添加项目时,一个想法是 一开始你有

this.data=[
 {id:1, desc:'test', level: 0},
 {id:2, desc:'testing',level: 0}
]
如果单击您收到的“1”

[ {id:5, desc: 'test test', level: 1} ]
您可以在添加“children”时更改数据。所以在getData中(data.id,i)

因此,您的数据成为

this.data=[
 {id:1, desc:'test', level: 0,children:
   [ {id:5, desc: 'test test', level: 1} ]
 },
 {id:2, desc:'testing',level: 0}
]
this.data=[
 {id:1, desc:'test', level: 0}
 {id:2, desc:'testing',level: 0}
 {id:5, desc: 'test test', level: 1,parent:1}
]
现在您只需要使用两个*ngFor

<div *ngFor="let level0 of data;let index=i">
    {{level0.id}}{{level0.desc}}{{level0.level}}
    <button (click)="getData(level0.id,index)">click</button>
    <div *ngFor="let level1 of level0.children">
       {{level1.id}}{{level1.desc}}{{level1.level}}
    </div>
</div>
因此,您的数据成为

this.data=[
 {id:1, desc:'test', level: 0,children:
   [ {id:5, desc: 'test test', level: 1} ]
 },
 {id:2, desc:'testing',level: 0}
]
this.data=[
 {id:1, desc:'test', level: 0}
 {id:2, desc:'testing',level: 0}
 {id:5, desc: 'test test', level: 1,parent:1}
]
如果你有这样一个函数

listData(parent:number)
{
    return (parent)? this.data.filter(x=>x.parent==parent)
                   : this.data.filer(x=>!x.parent)
}
你的ngFor可以是

<div *ngFor="let level0 of data;let index=i">
    {{level0.id}}{{level0.desc}}{{level0.level}}
    <button (click)="getData(level0.id)">click</button>
    <div *ngFor="let level1 of listData(level0.id)">
       {{level1.id}}{{level1.desc}}{{level1.level}}
    </div>
</div>

{level0.id}{{level0.desc}{{level0.level}}
点击
{{level1.id}{{level1.desc}{{level1.level}}

问题是:你有什么?你想要什么? ... 在向数据中添加项目时,一个想法是 一开始你有

this.data=[
 {id:1, desc:'test', level: 0},
 {id:2, desc:'testing',level: 0}
]
如果单击您收到的“1”

[ {id:5, desc: 'test test', level: 1} ]
您可以在添加“children”时更改数据。所以在getData中(data.id,i)

因此,您的数据成为

this.data=[
 {id:1, desc:'test', level: 0,children:
   [ {id:5, desc: 'test test', level: 1} ]
 },
 {id:2, desc:'testing',level: 0}
]
this.data=[
 {id:1, desc:'test', level: 0}
 {id:2, desc:'testing',level: 0}
 {id:5, desc: 'test test', level: 1,parent:1}
]
现在您只需要使用两个*ngFor

<div *ngFor="let level0 of data;let index=i">
    {{level0.id}}{{level0.desc}}{{level0.level}}
    <button (click)="getData(level0.id,index)">click</button>
    <div *ngFor="let level1 of level0.children">
       {{level1.id}}{{level1.desc}}{{level1.level}}
    </div>
</div>
因此,您的数据成为

this.data=[
 {id:1, desc:'test', level: 0,children:
   [ {id:5, desc: 'test test', level: 1} ]
 },
 {id:2, desc:'testing',level: 0}
]
this.data=[
 {id:1, desc:'test', level: 0}
 {id:2, desc:'testing',level: 0}
 {id:5, desc: 'test test', level: 1,parent:1}
]
如果你有这样一个函数

listData(parent:number)
{
    return (parent)? this.data.filter(x=>x.parent==parent)
                   : this.data.filer(x=>!x.parent)
}
你的ngFor可以是

<div *ngFor="let level0 of data;let index=i">
    {{level0.id}}{{level0.desc}}{{level0.level}}
    <button (click)="getData(level0.id)">click</button>
    <div *ngFor="let level1 of listData(level0.id)">
       {{level1.id}}{{level1.desc}}{{level1.level}}
    </div>
</div>

{level0.id}{{level0.desc}{{level0.level}}
点击
{{level1.id}{{level1.desc}{{level1.level}}
显示“迭代”组件的示例

//app.component
@Component({
  selector: 'app-root',
  template:`
  <app-app-children [children]="data"></app-app-children>
  `
})
export class AppComponent {
  title = 'app';
  isdisabled:boolean=false;
  data=[{id:1},
        {id:2,
            children:[{id:4},{id:5},{id:6,
                      children:[{id:9},{id:10}]}]},
        {id:3,
             children:[{id:7},{id:8}]}
        ];

  constructor(){}
}


//app-children
@Component({
  selector: 'app-app-children',
  template:`
  <p>
  app-children works!
</p>
<div *ngFor="let item of children">{{item.id}}
  <div *ngIf="item.children">
      <app-app-children [children]="item.children"></app-app-children>
  </div>
</div>`
})
export class AppChildrenComponent {

  constructor() { }
  @Input() children

}

//If whe can have a table, We can change the template like:
@Component({
    selector: 'app-app-children',
    template: `
    <table>
      <thead>
        <th>#</th>
      </thead>
      <tbody>
      <tr *ngFor="let item of children">
        <td>
           <div>
            {{item.id}}
           </div>
           <div *ngIf="item.children">
            <app-app-children [children]="item.children"></app-app-children>
           </div>
        </td>
    </table>
    `,
    styles:[`
      table, th, td {
         border: 1px solid black;
         margin-left:2rem;
      }
      `
     ]
  })
//app.component
@组成部分({
选择器:'应用程序根',
模板:`
`
})
导出类AppComponent{
标题=‘应用程序’;
isdisabled:boolean=false;
数据=[{id:1},
{id:2,
子项:[{id:4},{id:5},{id:6,
子项:[{id:9},{id:10}]},
{id:3,
子项:[{id:7},{id:8}]}
];
构造函数(){}
}
//应用程序儿童
@组成部分({
选择器:“应用程序子项”,
模板:`

应用程序儿童工程!

{{item.id} ` }) 导出类AppChildrenComponent{ 构造函数(){} @Input()子项 } //如果我们有一个表格,我们可以更改模板,如下所示: @组成部分({ 选择器:“应用程序子项”, 模板:` # {{item.id} `, 风格:[` 表,th,td{ 边框:1px纯黑; 左边距:2em; } ` ] })
显示“迭代”组件的示例

//app.component
@Component({
  selector: 'app-root',
  template:`
  <app-app-children [children]="data"></app-app-children>
  `
})
export class AppComponent {
  title = 'app';
  isdisabled:boolean=false;
  data=[{id:1},
        {id:2,
            children:[{id:4},{id:5},{id:6,
                      children:[{id:9},{id:10}]}]},
        {id:3,
             children:[{id:7},{id:8}]}
        ];

  constructor(){}
}


//app-children
@Component({
  selector: 'app-app-children',
  template:`
  <p>
  app-children works!
</p>
<div *ngFor="let item of children">{{item.id}}
  <div *ngIf="item.children">
      <app-app-children [children]="item.children"></app-app-children>
  </div>
</div>`
})
export class AppChildrenComponent {

  constructor() { }
  @Input() children

}

//If whe can have a table, We can change the template like:
@Component({
    selector: 'app-app-children',
    template: `
    <table>
      <thead>
        <th>#</th>
      </thead>
      <tbody>
      <tr *ngFor="let item of children">
        <td>
           <div>
            {{item.id}}
           </div>
           <div *ngIf="item.children">
            <app-app-children [children]="item.children"></app-app-children>
           </div>
        </td>
    </table>
    `,
    styles:[`
      table, th, td {
         border: 1px solid black;
         margin-left:2rem;
      }
      `
     ]
  })
//app.component
@组成部分({
选择器:'应用程序根',
模板:`
`
})
导出类AppComponent{
标题=‘应用程序’;
isdisabled:boolean=false;
数据=[{id:1},
{id:2,
子项:[{id:4},{id:5},{id:6,
子项:[{id:9},{id:10}]},
{id:3,
子项:[{id:7},{id:8}]}
];
构造函数(){}
}
//应用程序儿童
@组成部分({
选择器:“应用程序子项”,
模板:`

应用程序儿童工程!

{{item.id} ` }) 导出类AppChildrenComponent{ 构造函数(){} @Input()子项 } //如果我们有一个表格,我们可以更改模板,如下所示: @组成部分({ 选择器:“应用程序子项”, 模板:` # {{item.id} `, 风格:[` 表,th,td{ 边框:1px纯黑; 左边距:2em; } ` ] })

我也更新了我的答案。你认为你可以有多少级别?我无法得到,因为数据是动态的。我们可以有尽可能多的级别。然后选择最后一个选项并创建dinamicaly组件。e、 g.检查此链接,它可能会帮助您
https://stackoverflow.com/questions/38888008/how-can-i-use-create-dynamic-template-to-compile-dynamic-component-with-angular
谢谢你的推荐。我也更新了我的答案。你认为你可以有多少级别?我无法得到,因为数据是动态的。我们可以有尽可能多的级别。然后选择最后一个选项并创建dinamicaly组件。e、 g.检查此链接,它可能会帮助您
https://stackoverflow.com/questions/38888008/how-can-i-use-create-dynamic-template-to-compile-dynamic-component-with-angular
谢谢您的参考。谢谢!但我没有儿童在页面加载时的数据。当我点击id时,我会得到它的数据。@TavishAggarwal,我把它作为你可以实现的工具,当你点击时,在你的数据中添加“子项”:(是的。对不起。我刚才看到了,正在考虑应该使用哪种方法。我的代码是
。问题是使用这种方式,所有列只排在第1位。有什么解决方案吗?使用上面显示的代码,可以创建多个表标题。如果可能的话,可以共享plunk链接吗?谢谢!谢谢!但我没有da在页面加载时,儿童的ta。当我单击id时,我将获取id的数据。@TavishAggarwal,我将在您执行“单击”时向您的数据添加“儿童”:(是的。对不起。我刚才看到了,正在考虑应该使用哪种方法。我的代码是
。问题是使用这种方式,所有列只排在第1位。有什么解决方案吗?使用上面显示的代码,可以创建多个表标题。如果可能,请共享plunk链接。谢谢!