限制HTML表格中的行数。

huangapple go评论62阅读模式
英文:

How to limit the number of rows in a table html

问题

我想在表格中显示项目名称,但行数限制为三行。如果项目超过三个,第四个项目应该在第二列,如果项目超过六个,第七个项目应该在第三列。
我正在使用普通的表格。

<table>
    <tr *ngFor="let item of group; let i = index">
        <td *ngIf="i < 3">{{item}}</td>
        <td *ngIf="i >= 3 && i < 6" [ngClass]="{ 'second-column': i === 3 }">{{item}}</td>
        <td *ngIf="i >= 6" [ngClass]="{ 'third-column': i === 6 }">{{item}}</td>
    </tr>
</table>

请告诉我我需要设置什么条件,以限制行数并根据项目数量将它们放在不同的列中。

英文:

I want to display items names in a table but limit to number of rows is three. If there are more than 3 items then 4th item should be in 2nd column, if there are more than 6 items then 7th item should be in 3rd column.
I am using normal table.

<table>
    <tr *ngFor="let item of group">
        <td>{{item}}</td>
    </tr>
</table>

Please let me know what condition I have to give, to limit number of rows and get them in columns based on number of items.

答案1

得分: 2

你可以使用CSS Grid来使用grid-auto-flow来安排这种布局。以下是一些资源供您参考:

您可以在下面看到使用网格的布局。我已经注释了相关部分:

.table {
  display: grid;
  grid-auto-flow: column; /* 使项目按列而不是按行填充网格容器 */
  grid-template-rows: repeat(3, 1fr); /* 最多有3行,并使它们的高度相同 */
  gap: 0.125rem; /* 在每个元素之间留下小间隔 */
}

.table > div {
  background-color: teal;
  padding: 0.5rem 1rem;
}
<div class="table">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
  <div>6</div>
  <div>7</div>
</div>
英文:

You can use CSS Grid to arrange this sort of layout using grid-auto-flow. To get you started here's a few resources:

Your layout using grid can be seen below. I've annotated the pertinent bits:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.table {
  display: grid;
  grid-auto-flow: column; /* make the items fill up the grid container by column and not row */
  grid-template-rows: repeat(3, 1fr); /* have a max number of 3 rows and make them all the same height */
  gap: 0.125rem; /*put a small gap between each element */
}

.table &gt; div {
  background-color:teal;
  padding: 0.5rem 1rem;
}

<!-- language: lang-html -->

&lt;div class=&quot;table&quot;&gt;
  &lt;div&gt;1&lt;/div&gt;
  &lt;div&gt;2&lt;/div&gt;
  &lt;div&gt;3&lt;/div&gt;
  &lt;div&gt;4&lt;/div&gt;
  &lt;div&gt;5&lt;/div&gt;
  &lt;div&gt;6&lt;/div&gt;
  &lt;div&gt;7&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

@Adam已经使用CSS方法发布了正确答案。

如果你希望通过数据处理手动解决这个问题,那么这是一个适合你的解决方案。

export class UsersComponent {
  // Your data
  group: Array<string> = ['A', 'B', 'C', 'D'];
  // Fixed number of rows
  rows: number = 3;
  // Calculate number of columns based on data length and required number of rows
  columns: number = Math.ceil(this.group.length / this.rows);

  // We will push data here
  data: any = [];

  constructor() {
    // counter for data
    let elem = 0;
    // Outer loop
    for (let i = 0; i < this.columns; ++i) {
      // Create a row in data
      this.data.push([]);
      // Inner Loop
      for (let j = 0; j < this.rows; ++j) {
        // Do not push undefined elements (if any)
        if (this.group[elem]) {
          this.data[i][j] = this.group[elem];
        }
        // increment counter
        elem++;
      }
    }
    // We got the data, now lets take the transpose to invert it.
    this.data = this.transpose(this.data);
    console.log(this.data);
  }

  // Credits: https://stackoverflow.com/questions/17428587/transposing-a-2d-array-in-javascript
  transpose(matrix: any) {
    return matrix[0].map((col: any, i: any) =>
      matrix.map((row: any) => row[i])
    );
  }
}

现在我们已经有了以2D格式呈现的数据(具有正确的行数和列数),你可以在HTML中显示它:

<table class="table">
  <tr *ngFor="let row of data">
    <span *ngFor="let column of row">
      <td *ngIf="column">{{ column }}</td>
    </span>
  </tr>
</table>

你可以通过调整代码中的外部和内部循环来改变出现在一行中的元素的顺序(首先填充行或首先填充列)。此外,你可以更改行数和数据大小以测试各种情况。

在此处演示:https://stackblitz.com/edit/angular-mgte7k?file=src%2Fusers%2Fusers.component.ts

英文:

@Adam has posted the correct answer for this using CSS methods.

If you are looking to manually solve this through data manipulation, then this is a solution for you.

export class UsersComponent {
  // Your data
  group: Array&lt;string&gt; = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;];
  // Fixed number of rows
  rows: number = 3;
  // Calculate number of columns based on data length and required number of rows
  columns: number = Math.ceil(this.group.length / this.rows);

  // We will push data here
  data: any = [];

  constructor() {
    // counter for data
    let elem = 0;
    // Outer loop
    for (let i = 0; i &lt; this.columns; ++i) {
      // Create a row in data
      this.data.push([]);
      // Inner Loop
      for (let j = 0; j &lt; this.rows; ++j) {
        // Do not push undefined elements (if any)
        if (this.group[elem]) {
          this.data[i][j] = this.group[elem];
        }
        // increment counter
        elem++;
      }
    }
    // We got the data, now lets take the transpose to invert it.
    this.data = this.transpose(this.data);
    console.log(this.data);
  }

  // Credits: https://stackoverflow.com/questions/17428587/transposing-a-2d-array-in-javascript
  transpose(matrix: any) {
    return matrix[0].map((col: any, i: any) =&gt;
      matrix.map((row: any) =&gt; row[i])
    );
  }
}

Now that we have data in 2d format (with correct number of rows and columns), you can display it in your HTML like:

&lt;table class=&quot;table&quot;&gt;
  &lt;tr *ngFor=&quot;let row of data&quot;&gt;
    &lt;span *ngFor=&quot;let column of row&quot;&gt;
      &lt;td *ngIf=&quot;column&quot;&gt;{{ column }}&lt;/td&gt;
    &lt;/span&gt;
  &lt;/tr&gt;
&lt;/table&gt;

It is possible to change ordering of elements appearing in a row (populate row first or populate columns first) just by tweaking outer and inner loops in code. Also you can change number of rows and size of your data to test it for various cases.

Demo here https://stackblitz.com/edit/angular-mgte7k?file=src%2Fusers%2Fusers.component.ts

huangapple
  • 本文由 发表于 2023年5月29日 20:58:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定