【问题标题】:*ngFor create new row each "n" cells*ngFor 每“n”个单元格创建新行
【发布时间】:2017-05-17 10:57:04
【问题描述】:

我正在尝试动态创建表格的元素,但我有疑问。

如何创建新的<tr>,例如每个 4 个<td>,然后继续创建新的<td>

 <table>
    <tr>
    <td class="category-card" *ngFor= "let category of categories">

             <img class="product-card-image" src="{{category.icon}}">
             <span class="barImage">{{category.title}}</span>
    </td>
    </table>

编辑:

<table>     
<ng-template *ngFor="let category of categories; let i = index">
            <tr *ngIf="(i % 4) == 0">
                <td class="product-card">
                    <img class="product-card-image" src="/app/assets/img/{{category.icon}}.png">
                    <span class="barImage">{{category.titulo}}</span>
                </td>
            </tr>
        </ng-template>
    </table>

EDIT2: 我添加了

<table [outerHTML]="categorias | dynamicTablePipe"></table> on my html

这是一个外部类(我在 NGModule 上声明过)

@Pipe({
    name: 'dynamicTablePipe'
})
export class DynamicTablePipe implements PipeTransform{
    transform(contents) {
        let template = ``;
        let index = 0;
        for (let content of contents) {
            let currentTemplate = ``;
            if (index === 0) {
                // check if first record
                currentTemplate = `
                    <tr>
                        <td class="product-card">
                            <img class="product-card-image" src="/app/assets/img/${content.icono}.png">
                            <span class="barImage">${content.titulo}</span>
                    </td>`;
            } else if ((index % 4) === 0) {
                // check if multiple of 4
                currentTemplate = `
                    </tr>
                    <tr>
                   <td class="product-card">
                            <img class="product-card-image" src="/app/assets/img/${content.icono}.png">
                            <span class="barImage">${content.titulo}</span>
                    </td>`;
            } else {
                // normal row
                currentTemplate = `
                    <td class="product-card">
                            <img class="product-card-image" src="/app/assets/img/${content.icono}.png">
                            <span class="barImage">${content.titulo}</span>
                    </td>`;
            }
            index++;
            template += currentTemplate;
        }
        return template;
    }
}

管道内的代码工作正常,这是最后形成的模板字符串:

 <tr>
                            <td class="product-card">
                                <img class="product-card-image" src="/app/assets/img/smartphone.png">
                                <span class="barImage">Telefonia</span>
                        </td>
                        <td class="product-card">
                                <img class="product-card-image" src="/app/assets/img/gamepad.png">
                                <span class="barImage">Videojuegos</span>
                        </td>
                        <td class="product-card">
                                <img class="product-card-image" src="/app/assets/img/laptop.png">
                                <span class="barImage">Portatiles</span>
                        </td>
                        <td class="product-card">
                                <img class="product-card-image" src="/app/assets/img/speaker.png">
                                <span class="barImage">Reproductores</span>
                        </td>
                        </tr>
                        <tr>
                       <td class="product-card">
                                <img class="product-card-image" src="/app/assets/img/link.png">
                                <span class="barImage">Redes</span>
                        </td></tr>

问题出在回归的那一刻

谢谢

【问题讨论】:

  • "跳转到一个新文件" ---> whaaaat ?
  • 大声笑,对不起。我的意思是,我怎样才能每 4 个单元格创建一个新的表格行,然后继续将单元格添加到这个新行中
  • 老实说,“每行有 4 个元素”听起来应该在 CSS 中执行,而不是在标记中执行
  • @PeterT 如何通过 CSS 限制表格的 os 列/单元格的数量?
  • @Hanzo 我建议您不要使用&lt;table&gt;,而只是使用一串元素,然后使用 css 进行布局。是这样的:Fiddle link(应该说我是个css菜鸟,所以还有很大的提升空间)

标签: angular ngfor


【解决方案1】:

不要在&lt;tr&gt;&lt;td&gt; 中使用*ngFor,而是在&lt;template&gt; 中使用它,并根据index 或找到的记录数量选择呈现&lt;td&gt;&lt;tr&gt; .

按照本教程/指南来解释这个确切的事情:https://toddmotto.com/angular-ngfor-template-element

请记住,对于 Angular v4 使用 &lt;ng-template&gt; 或对于 Angular v2 使用 &lt;template&gt;

最后,你可以有类似的东西:

<ng-template ngFor let-i="index" let-c="count" let-contact [ngForOf]="contacts | async">
    <tr *ngIf="if((i % 4) == 0)">
        // Check if index is multiple of 4
    </tr>
</ng-template>

最后一个示例是我链接到您的教程/指南的最终代码。我使用它,所以当你到达这一点时,你有一个友好的代码。

选项 2:

使用&lt;template&gt;,您可以构建一个指令来呈现数据。

所以你会有类似的东西:

<template [myDirective]="myData"></template>

在您的 myDirective 逻辑中,您选择了如何使用数据循环中的索引来呈现数据。

选项 3 - @Pipe:

@Pipe 可能是正确的!你可以试试这样的:

@Pipe({
    name: 'renderTable'
})
class RenderTable {
    transform(content) {
        let template = ``;
        let index = 0;
        for (let row in content) {
            let currentTemplate = ``;
            if (index === 0) {
                // check if first record
                currentTemplate = `
                    <tr>
                        <td>${row}</td>`;
            } else if ((index % 4) === 0) {
                // check if multiple of 4
                currentTemplate = `
                    </tr>
                    <tr>
                    <td>${row}</td>`;
            } else {
                // normal row
                currentTemplate = `
                    <td>${row}</td>`;
            }
            index++;
            template += currentTemplate;
        }
        return template;
    }
}

在您的表格中,类似于:

<table [outerHTML]="categories | renderTable"></table>

更新:

试试innerHTML:

<table [innerHTML]="categories | renderTable"></table>

更新 2:

对于样式丢失问题,这里是解决方案!找到了!

In RC.1 some styles can't be added using binding syntax

【讨论】:

  • 我要试试。谢谢
  • 嗨@SrAxi,我已经在我的原始问题中添加了一个使用您的解决方案更新的代码。对我不起作用。它不渲染任何东西。另外,我正在使用 Angular 4,如果我使用
  • 我错过了打字。 Angular4 使用&lt;ng-template&gt;。对不起!尝试使用自定义指令。检查我在这里做了什么:stackoverflow.com/questions/43395808/… 使用我为菜单创建的自定义指令。我的&lt;li&gt; 元素中的内容是从被选为指令的组件中获取的。示例:selector: [myComponent].
  • 您好,首先感谢您的回复。我编辑了我的原始问题,添加了我的测试(EDIT2)。首先,如果我离开(让内容IN 内容)我无法访问内容的对象属性它说我需要一个字符串。但是,如果我使用我编辑的问题的代码,它会返回错误“DOMException:无法在“元素”上设置“outerHTML”属性:该元素没有父节点。在 EmulatedEncapsulationDomRenderer2.DefaultDomRenderer2.setProperty`` 会出现什么问题?
  • 谢谢。适用于样式标签,但不适用于类。
【解决方案2】:

这就是我解决的方法,它不是优化的解决方案,但我得到了解决。希望能帮到你。

<table class="table table-bordered">
  <ng-container *ngFor="let item of categories; let i = index">
    <tr *ngIf="((i % 4) == 0)">
      <ng-container *ngFor="let item of categories; let j = index">
        <td *ngIf="j >= i && j < i+4">
          <label>
            <input type="checkbox" [value]="item.id">{{item.name}}
          </label>
        </td>
      </ng-container>
    </tr>
  </ng-container>
</table>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 2018-03-12
    • 2016-07-22
    相关资源
    最近更新 更多