【问题标题】:How to display 4 values in every row in html using ngfor and ngif?如何使用 ngfor 和 ngif 在 html 的每一行中显示 4 个值?
【发布时间】:2021-11-14 04:35:56
【问题描述】:

我想通过表中的for循环显示键值对,tr标签。我想在一行中显示 3-4 对。

在此处添加我的代码-https://stackblitz.com/edit/angular-ivy-hrqira?file=src/app/app.component.ts

我想在一行中分别显示 4 个值。用这个链接更新了我的问题。

<table>
<div *ngFor="let table of uiFields | keyvalue">
<tr><td><label>{{table.key}}</label><label>{{table.value}}</table></td>
</tr>
</div>
</table>

我得到一个低于另一个的值,我想在一行中显示 3-4 对。 uiFields 是我的地图,包含不同的值。

【问题讨论】:

    标签: html css html-table ngfor angular-ng-if


    【解决方案1】:
        <table>
        <tr><td *ngFor="let table of uiFields | keyvalue"><label>{{table.key}}</label><label>{{table.value}}</table></td>
        </tr>
        </table>
    

    【讨论】:

    • 如果我只想要每行 4-5 个项目?
    • | slice:0:4 ,在 keyvalue 之后使用这个管道
    • 我尝试使用 它显示地图中的随机值
    【解决方案2】:

    我尝试了另一种方法来检查这是否适合您

    codeSolutionhttps://stackblitz.com/edit/angular-ivy-12fmx9?file=src/app/app.component.ts

    HTML

    <table>
      <div>
        <tr *ngFor="let items of uiFields">
          <td *ngFor="let table of items">
            <label>{{ table.key }}</label>
            {{ table.value }}
          </td>
        </tr>
      </div>
    </table>
    

    ts

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css'],
    })
    export class AppComponent {
      uiFields = [
        [
          { key: 1, value: 'Rishab' },
          { key: 2, value: 'Ram' },
          { key: 3, value: 'Ramu' },
        ],
        [
          { key: 1, value: 'Rishab' },
          { key: 2, value: 'Ram' },
          { key: 3, value: 'Ramu' },
        ],
        [
          { key: 1, value: 'Rishab' },
          { key: 2, value: 'Ram' },
          { key: 3, value: 'Ramu' },
        ],
      ];
    }
    

    【讨论】:

    • 发送您的 uiFields 列表
    • 在此处更新了我的问题和值 - stackblitz.com/edit/angular-ivy-hrqira?file=src/app/… 我想要每个连续 3-4 个值。
    • 你必须使用 MAP 吗?
    • 上面我写的方法
    • 我在循环中添加值,每次插入时都不能在第 3 或第 4 处中断。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签