Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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/5/spring-mvc/2.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
Angular 角度动态模板呈现,如ui网格单元模板(在父级的colDefs中声明的模板)_Angular_Typescript_Ui Grid_Ng Template_Dynamicgridview - Fatal编程技术网

Angular 角度动态模板呈现,如ui网格单元模板(在父级的colDefs中声明的模板)

Angular 角度动态模板呈现,如ui网格单元模板(在父级的colDefs中声明的模板),angular,typescript,ui-grid,ng-template,dynamicgridview,Angular,Typescript,Ui Grid,Ng Template,Dynamicgridview,角度(7)动态模板(ng模板)呈现,如ui网格单元模板(在父级的colDefs中声明的模板);30多天以来,我尝试了很多方法,观看了ng模板渲染视频、文章,但找不到任何解决方案,请在这里帮助我 提前感谢:) 应用程序组件.ts export class AppComponent implements OnInit { name = 'Angular'; gridOptions:any; constructor(private http: HttpClient){} ngOnInit(){

角度(7)动态模板(ng模板)呈现,如ui网格单元模板(在父级的colDefs中声明的模板);30多天以来,我尝试了很多方法,观看了ng模板渲染视频、文章,但找不到任何解决方案,请在这里帮助我

提前感谢:)

应用程序组件.ts

export class AppComponent implements OnInit {
name = 'Angular';
gridOptions:any;

constructor(private http: HttpClient){}


ngOnInit(){

  this.gridOptions = {
    colDefs:[
     {name:'id', displayName:'ID',width:80},
     {name:'name', displayName:'Name'},
     {name:'username', displayName:'Username', cellTemplate:'Static text from template'},
     {name:'email', displayName:'Email', cellTemplate:'{{row[col.name]}}'},  // i tried like this
     {name:'phone', displayName:'phone', cellTemplate:"<strong style='color:blue'> + row[col.name] + </strong>"}, // I need to achive this kind of template like ui grid for angularjs
     {name:'website', displayName:'website', cellTemplate:'row[col.name]'}, // i tried like this also
    ]
   }

   this.http.get("https://jsonplaceholder.typicode.com/users").subscribe((res) =>{
    this.gridOptions.data = res;
  });

 }

}
export class DataTableComponent implements OnInit {
@Input() gridOptions: any = [];
  constructor() { } 

  ngOnInit() {}
}
数据表.component.html

<table>
    <tr>
        <th *ngFor="let col of gridOptions.colDefs">{{col.name}}</th>
    </tr>

    <tr *ngFor="let row of gridOptions.data ;let i = index">
        <td *ngFor="let col of gridOptions.colDefs">
            <ng-container *ngIf="!col.cellTemplate else myTemplate" [ngTemplateOutlet]="myTemplate">
                {{row[col.name]}}
            </ng-container>
            <ng-template #myTemplate row="row" col="col">
                {{col.cellTemplate}}
            </ng-template>

            <!-- <ng-template #myTemplate row="row" col="col" innerHTML="col.cellTemplate}"></ng-template> -->
        </td>
    </tr>
</table>
<app-data-table [gridOptions]="gridOptions"></app-data-table>

{{col.name}}
{{row[col.name]}
{{col.cellTemplate}}
app.component.html

<table>
    <tr>
        <th *ngFor="let col of gridOptions.colDefs">{{col.name}}</th>
    </tr>

    <tr *ngFor="let row of gridOptions.data ;let i = index">
        <td *ngFor="let col of gridOptions.colDefs">
            <ng-container *ngIf="!col.cellTemplate else myTemplate" [ngTemplateOutlet]="myTemplate">
                {{row[col.name]}}
            </ng-container>
            <ng-template #myTemplate row="row" col="col">
                {{col.cellTemplate}}
            </ng-template>

            <!-- <ng-template #myTemplate row="row" col="col" innerHTML="col.cellTemplate}"></ng-template> -->
        </td>
    </tr>
</table>
<app-data-table [gridOptions]="gridOptions"></app-data-table>

据我所知,DataTables不支持任何角度魔法。 我处理过DataTables提供的渲染函数的类似问题:

例如,您需要更改行:

{name:'email', displayName:'Email', cellTemplate:'{{row[col.name]}}'},
致:


我希望这能有所帮助。

据我所知,DataTables不支持任何角度魔法。 我处理过DataTables提供的渲染函数的类似问题:

例如,您需要更改行:

{name:'email', displayName:'Email', cellTemplate:'{{row[col.name]}}'},
致:

我希望这能有所帮助。

请查看我为您创建的演示。它利用了渲染函数
cellFn
,您可以在其中自定义模板。还要注意使用
safeHtml
pipe在模板内呈现html

编辑:正如OP在评论中所述,他还希望在模板中使用角度指令等。因此,这里是用于此目的的演示

查看我为您创建的演示。它利用了渲染函数
cellFn
,您可以在其中自定义模板。还要注意使用
safeHtml
pipe在模板内呈现html


编辑:正如OP在评论中所述,他还希望在模板中使用角度指令等。因此,这里是用于此目的的演示

为什么不将cellTemplate添加到模板和添加条件中,而不是在ts文件中定义它们?cellTemplate在任何时候都应该可以工作,对于任何类型的json,比如ui网格,我必须不断为diff json服务添加条件,这里的数据表组件应该是通用的。为什么不将cellTemplate添加到模板中并添加条件,而不是在ts文件中定义它们呢?它应该可以在cellTemplate的任何时候工作,对于任何类型的json,比如ui网格,我必须不断为diff json服务添加条件,这里的数据表组件应该是通用的。。。非常感谢你,回答得很好,你帮我省去了这个项目中的大问题。仅仅谢谢你是不够的。。上帝保佑你。@RajGopal没问题。我很高兴我帮了忙。嗨@Bardr,我还需要你的帮助,ngClass不工作,你能检查一下吗-------
{name:'name',displayName:'name',cellFn:rowData=>'${rowData.name}}
我不能在这里使用任何角度属性case@RajGopal是的,这是显而易见的。它们将仅呈现为普通HTML。这里没有魔法。您是否只需要
[ngClass]
或更具说服力的指令<代码>[ngClass]可以简单地在
cellFn
函数中制作等效代码。wooww。。。非常感谢你,回答得很好,你帮我省去了这个项目中的大问题。仅仅谢谢你是不够的。。上帝保佑你。@RajGopal没问题。我很高兴我帮了忙。嗨@Bardr,我还需要你的帮助,ngClass不工作,你能检查一下吗-------
{name:'name',displayName:'name',cellFn:rowData=>'${rowData.name}}
我不能在这里使用任何角度属性case@RajGopal是的,这是显而易见的。它们将仅呈现为普通HTML。这里没有魔法。您是否只需要
[ngClass]
或更具说服力的指令<代码>[ngClass]可以在
cellFn
函数中简单地制作等效代码。