我需要使用递归在 angular 6 中制作行和列,其中行可以列和列有任意数量的行
I need to make rows and columns in angular 6 using recursion in which row can columns and columns have any numbers of row
我尝试使用动态 HTML 制作它,但我无法在动态 HTML 中调用点击事件。
这是自己试过的
my .ts file
htmlgrid:any;
jsonData:any;
ngOnInit(){
this.htmlgrid= this.parse(this.jsonData)
}
createRow (r) {
return '<div style="background-color : ' + r.color + '" class="row">' +
(r.text ? r.text : '') + this.parse(r) + '</div>';
}
createColumn (c) {
return '<div style="background - color: red;" class="col-md-' + 6 + ' test">' +
(c.text ? c.text : '') + this.parse(c) + '<img click="hell();" src = "../../../../assets/img/collection.jpg" style = "height: 100px; width:auto;" />' + '</div>';
}
parse (s) {
let S = '';
if (s.rows) {
for (let i in s.rows) {
console.log(s.rows[ i ], 'i of data');
S += this.createRow(s.rows[ i ]);
}
}
if (s.columns) {
for (let i in s.columns) {
S += this.createColumn(s.columns[ i ]);
}
}
console.log(S, 'value of s');
return S;
}
My.html 文件
<div class="one" [innerHtml]="htmlToAdd"></div>
这种JSON是用来做行和列的,我们的JSON也有标识符和行列校验。请帮助我被困在这里很糟糕,
我需要根据以下 json
制作行列网格
this.jsonData={
"rows":[
{
"columns":[
{
"identifier":"c1",
"hasRows":false,
"cashBack":{
"text":""
},
"title":{
"text":""
},
"images":{
"leafBanner":{
"url":"",
"bannerName":"",
"bannerType":"",
"bannerTarget":""
},
"listPageBanner":{
"image":"",
"2X":{
"height":"200px",
"width":"400px"
},
"3X":{
"height":"300px",
"width":"600px"
}
}
},
"height":"50",
"width":"50"
},
{
"identifier":"c2",
"hasRows":false,
"cashBack":{
"text":""
},
"title":{
"text":""
},
"images":{
"leafBanner":{
"url":"",
"bannerName":"",
"bannerType":"",
"bannerTarget":""
},
"listPageBanner":{
"image":"",
"2X":{
"height":"200px",
"width":"400px"
},
"3X":{
"height":"300px",
"width":"600px"
}
}
},
"height":"50",
"width":"50"
}
]
},
{
"columns":[
{
"identifier":"c3",
"hasRows":false,
"cashBack":{
"text":""
},
"title":{
"text":""
},
"images":{
"leafBanner":{
"url":"",
"bannerName":"",
"bannerType":"",
"bannerTarget":""
},
"listPageBanner":{
"image":"",
"2X":{
"height":"200px",
"width":"400px"
},
"3X":{
"height":"300px",
"width":"600px"
}
}
},
"height":"33",
"width":"33"
},
{
"identifier":"c4",
"hasRows":false,
"cashBack":{
"text":""
},
"title":{
"text":""
},
"images":{
"leafBanner":{
"url":"",
"bannerName":"",
"bannerType":"",
"bannerTarget":""
},
"listPageBanner":{
"image":"",
"2X":{
"height":"200px",
"width":"400px"
},
"3X":{
"height":"300px",
"width":"600px"
}
}
},
"height":"33",
"width":"33"
},
{
"identifier":"c5",
"hasRows":false,
"cashBack":{
"text":""
},
"title":{
"text":""
},
"images":{
"leafBanner":{
"url":"",
"bannerName":"",
"bannerType":"",
"bannerTarget":""
},
"listPageBanner":{
"image":"",
"2X":{
"height":"200px",
"width":"400px"
},
"3X":{
"height":"300px",
"width":"600px"
}
}
},
"height":"33",
"width":"33"
}
]
}
]
}
您可以定义一个组件来生成您的 html 并且您可以递归调用它
@Component({
selector: "grid",
template: `
<ng-container [ngSwitch]="(data | keyvalue)[0].key">
<ng-container *ngSwitchCase="'rows'">
<div class="row" *ngFor="let row of data.rows">
<grid [data]="row"></grid>
</div>
</ng-container>
<ng-container *ngSwitchCase="'columns'">
<div class="col" *ngFor="let col of data.columns">
<grid [data]="col"></grid>
</div>
</ng-container>
<ng-container *ngSwitchDefault>
<grid [data]="data.rows" *ngIf="data.hasRows; else cell"></grid>
<ng-template #cell>
<div class="cell">{{ data | json }}</div>
</ng-template>
</ng-container>
</ng-container>
`,
styles: [
".row{background-color:red;padding: 5px;}",
".col{background-color:green; padding:5px;}",
".cell{background-color:cyan;padding:5px;}"
]
})
export class GridComponent {
@Input()
data: any;
}
您可以像这样从 app/display 组件中调用此网格组件
<grid [data]="jsonData"></grid>
这是一个良好的开端,您可以修改默认的 switch case 以满足您的需求我刚刚在您的 json 中找到了一个属性 hasRows
如果是这样,它将递归调用网格组件又回来了。
希望对您有所帮助,Stackblitz 供您参考:https://stackblitz.com/edit/angular-6cqbsg
我尝试使用动态 HTML 制作它,但我无法在动态 HTML 中调用点击事件。
这是自己试过的
my .ts file
htmlgrid:any;
jsonData:any;
ngOnInit(){
this.htmlgrid= this.parse(this.jsonData)
}
createRow (r) {
return '<div style="background-color : ' + r.color + '" class="row">' +
(r.text ? r.text : '') + this.parse(r) + '</div>';
}
createColumn (c) {
return '<div style="background - color: red;" class="col-md-' + 6 + ' test">' +
(c.text ? c.text : '') + this.parse(c) + '<img click="hell();" src = "../../../../assets/img/collection.jpg" style = "height: 100px; width:auto;" />' + '</div>';
}
parse (s) {
let S = '';
if (s.rows) {
for (let i in s.rows) {
console.log(s.rows[ i ], 'i of data');
S += this.createRow(s.rows[ i ]);
}
}
if (s.columns) {
for (let i in s.columns) {
S += this.createColumn(s.columns[ i ]);
}
}
console.log(S, 'value of s');
return S;
}
My.html 文件
<div class="one" [innerHtml]="htmlToAdd"></div>
这种JSON是用来做行和列的,我们的JSON也有标识符和行列校验。请帮助我被困在这里很糟糕, 我需要根据以下 json
制作行列网格this.jsonData={
"rows":[
{
"columns":[
{
"identifier":"c1",
"hasRows":false,
"cashBack":{
"text":""
},
"title":{
"text":""
},
"images":{
"leafBanner":{
"url":"",
"bannerName":"",
"bannerType":"",
"bannerTarget":""
},
"listPageBanner":{
"image":"",
"2X":{
"height":"200px",
"width":"400px"
},
"3X":{
"height":"300px",
"width":"600px"
}
}
},
"height":"50",
"width":"50"
},
{
"identifier":"c2",
"hasRows":false,
"cashBack":{
"text":""
},
"title":{
"text":""
},
"images":{
"leafBanner":{
"url":"",
"bannerName":"",
"bannerType":"",
"bannerTarget":""
},
"listPageBanner":{
"image":"",
"2X":{
"height":"200px",
"width":"400px"
},
"3X":{
"height":"300px",
"width":"600px"
}
}
},
"height":"50",
"width":"50"
}
]
},
{
"columns":[
{
"identifier":"c3",
"hasRows":false,
"cashBack":{
"text":""
},
"title":{
"text":""
},
"images":{
"leafBanner":{
"url":"",
"bannerName":"",
"bannerType":"",
"bannerTarget":""
},
"listPageBanner":{
"image":"",
"2X":{
"height":"200px",
"width":"400px"
},
"3X":{
"height":"300px",
"width":"600px"
}
}
},
"height":"33",
"width":"33"
},
{
"identifier":"c4",
"hasRows":false,
"cashBack":{
"text":""
},
"title":{
"text":""
},
"images":{
"leafBanner":{
"url":"",
"bannerName":"",
"bannerType":"",
"bannerTarget":""
},
"listPageBanner":{
"image":"",
"2X":{
"height":"200px",
"width":"400px"
},
"3X":{
"height":"300px",
"width":"600px"
}
}
},
"height":"33",
"width":"33"
},
{
"identifier":"c5",
"hasRows":false,
"cashBack":{
"text":""
},
"title":{
"text":""
},
"images":{
"leafBanner":{
"url":"",
"bannerName":"",
"bannerType":"",
"bannerTarget":""
},
"listPageBanner":{
"image":"",
"2X":{
"height":"200px",
"width":"400px"
},
"3X":{
"height":"300px",
"width":"600px"
}
}
},
"height":"33",
"width":"33"
}
]
}
]
}
您可以定义一个组件来生成您的 html 并且您可以递归调用它
@Component({
selector: "grid",
template: `
<ng-container [ngSwitch]="(data | keyvalue)[0].key">
<ng-container *ngSwitchCase="'rows'">
<div class="row" *ngFor="let row of data.rows">
<grid [data]="row"></grid>
</div>
</ng-container>
<ng-container *ngSwitchCase="'columns'">
<div class="col" *ngFor="let col of data.columns">
<grid [data]="col"></grid>
</div>
</ng-container>
<ng-container *ngSwitchDefault>
<grid [data]="data.rows" *ngIf="data.hasRows; else cell"></grid>
<ng-template #cell>
<div class="cell">{{ data | json }}</div>
</ng-template>
</ng-container>
</ng-container>
`,
styles: [
".row{background-color:red;padding: 5px;}",
".col{background-color:green; padding:5px;}",
".cell{background-color:cyan;padding:5px;}"
]
})
export class GridComponent {
@Input()
data: any;
}
您可以像这样从 app/display 组件中调用此网格组件
<grid [data]="jsonData"></grid>
这是一个良好的开端,您可以修改默认的 switch case 以满足您的需求我刚刚在您的 json 中找到了一个属性 hasRows
如果是这样,它将递归调用网格组件又回来了。
希望对您有所帮助,Stackblitz 供您参考:https://stackblitz.com/edit/angular-6cqbsg