【问题标题】:Angular dynamic template rendering like ui grid cell template (template declared in colDefs of parent)Angular 动态模板渲染,如 ui 网格单元模板(在父项的 colDefs 中声明的模板)
【发布时间】:2019-07-08 04:54:42
【问题描述】:

Angular(7)动态模板(ng-template)渲染类似ui网格单元模板(模板在父级colDefs中声明); 30多天以来,我尝试了很多方法,看了ng-template渲染视频,文章,但没有找到任何解决方案,请在这里帮助我。

提前致谢:)

app.component.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;
  });

 }

}

data-table.component.ts

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

  ngOnInit() {}
}

data-table.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.component.html

<app-data-table [gridOptions]="gridOptions"></app-data-table>

StackBlitz DEMO

【问题讨论】:

  • 为什么不将cellTemplate添加到模板中并添加条件,而不是在ts文件中定义呢?
  • 它应该适用于任何时间的 cellTemplate,适用于任何类型的 json,如 ui 网格,我必须不断为 diff json 服务添加条件,这里的 data-table 组件应该普通

标签: angular typescript ui-grid ng-template dynamicgridview


【解决方案1】:

据我所知,DataTables 不支持任何 Angular 魔法。 我用 DataTables 为您提供的渲染函数处理了类似的问题:https://datatables.net/reference/option/columns.render#function

例如,您需要更改行:

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

到:

{name:'email', displayName:'Email', render:(data, type, row, meta) => 'contact ' + row.name + ' via E-Mail'},

我希望这会有所帮助。

【讨论】:

    【解决方案2】:

    查看我为您创建的 StackBlitz 演示。它利用了渲染功能cellFn,您可以在其中随意自定义模板。还要注意safeHtml管道在模板内渲染html的用法。

    编辑:正如cmets中的OP所说,他还想在模板中使用Angular指令等。所以这里是用于此目的的StackBlitz 演示。

    【讨论】:

    • wooww... 非常感谢@**Bardr**,很好的回答,您为我从角度项目中的这个大问题节省了时间。仅仅感谢你是不够的..上帝保佑你..
    • @RajGopal 没问题。我很高兴能帮上忙。
    • 嗨@Bardr,我需要你的更多帮助,ngClass 不工作,你能检查一下---------{ name: 'name', displayName: 'Name', cellFn: rowData =&gt; '&lt;span [ngClass]="{'red':${rowData.name} == 'Ervin Howell'}"&gt;${rowData.name}&lt;span&gt;' }
    • 在这种情况下我无法使用任何角度属性
    • @RajGopal 是的,这很明显。它们将被呈现为纯 HTML。这里没有魔法。您只需要[ngClass] 或更多雄辩的指令吗? [ngClass] 等价物可以简单地在 cellFn 函数中进行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 2020-05-12
    • 2017-11-06
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多