Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

angular - 如何将列表分成几列?(How to split list into columns?)

I have list of objects like:

(我有类似的对象列表:)

 cases: Case[] = [
    { id: 1, name: 'A' },
    { id: 1, name: 'B' },
    { id: 1, name: 'C' },
    { id: 1, name: 'D' },
    { id: 1, name: 'E' },
    { id: 1, name: 'F' },
  ];

and I want to display it as 4 columns in ngFor directive

(我想在ngFor指令中将其显示为4列)

  <li *ngFor="let row of casesRows; index as i;">
    <div class="card" style="width: 18rem;">
      <div class="card-body">
        <h5 class="card-title">{{i}}</h5>

      </div>
    </div>
  </li>

I'm stuck with Typescript converting simple list into 4 column list.

(我被Typescript转换为4列列表所困扰。)

My first idea was:

(我的第一个想法是:)

ngOnInit() {
    this.getCasesRows();
  }

  getCasesRows() {
    this.i = 0;
    this.cases.forEach(element => {
      console.log(element)
      if (this.i = 0 ) {
        this.cases4.col1 = element;
      } else if (this.i = 1) {
        this.cases4.col2 = element;
      } else if (this.i = 2) {
        this.cases4.col3 = element;
      } else if (this.i = 3) {
        this.cases4.col4 = element;
        this.i = -1;
      }
      this.i = this.i + 1;
    });
  }

But I end up with error like: ERROR TypeError: "_this.cases4 is undefined"

(但是我最终遇到如下错误错误TypeError:“ _ this.cases4未定义”)

Expected Output: 4 columns displayed like this:

(预期输出:显示四列,如下所示:)

ABCD

(A B C D)

EF

(英孚)

as cards

(作为卡)

  ask by Tommy Lee Jones w Sciganym translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need something like array_chunk like in example:

(您需要像array_chunk这样的示例:)

 var cases = [ { id: 1, name: 'A' }, { id: 1, name: 'B' }, { id: 1, name: 'C' }, { id: 1, name: 'D' }, { id: 1, name: 'E' }, { id: 1, name: 'F' }, ]; function array_chunk(arr, len) { var chunks = [], i = 0, n = arr.length; while (i < n) { chunks.push(arr.slice(i, i += len)); } return chunks; } console.log(array_chunk(cases, 4)); 

Here is an example in angular: https://stackblitz.com/edit/angular-wplxwm

(这是角度的示例: https : //stackblitz.com/edit/angular-wplxwm)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...