简体   繁体   English

如何将 JSON 到表的数据限制为 Angular 中的某些行?

[英]How to restrict the data from JSON to table to certain rows in Angular?

I'm extracting data from the server in the form of JSON and adding it to the HTML table.我正在以 JSON 的形式从服务器提取数据并将其添加到 HTML 表中。 I want to fix the table rows to 5 and only the top 5 value from JSON should go into table using Angular.我想将表格行固定为 5,并且只有来自 JSON 的前 5 个值应该使用 Angular 进入表格。 If there are less than 5 data available, fill the table columns with '-'.如果可用数据少于 5 个,请用“-”填充表格列。

component.ts组件.ts

import { HttpClient } from '@angular/common/http';
import { HttpErrorResponse } from '@angular/common/http';

@Component({
  selector: 'app-orderbook',
  templateUrl: './orderbook.component.html',
  styleUrls: ['./orderbook.component.css']
})
export class OrderbookComponent implements OnInit {
  title = 'JSON to Table Example';
  constructor (private httpService: HttpClient) { }
  arrBirds: string [];
  

  ngOnInit() {
    this.httpService.get('http://localhost:7000/orders/list').subscribe(
      data => {
        this.arrBirds = data as string [];   // FILL THE ARRAY WITH DATA.
         //console.log(this.arrBirds[1]);
      },
      (err: HttpErrorResponse) => {
        //console.log (err.message);
      }
    );
  }
  }

component.html组件.html

      <div style="text-align:left;width:500px;">
        <h1>
            {{ title }}!
        </h1>
        
        <table *ngIf="arrBirds">
            
              <tr>
                <th>Order ID</th>
                <th>Type</th>
                <th>Category</th>
                <th>Quantity</th>
                <th>Price</th>
              </tr>
          
          <tr *ngFor="let bird of arrBirds">
            <td>{{bird.orderId}}</td>
            <td>{{bird.type}}</td>
            <td>{{bird.category}}</td>
            <td>{{bird.quantity}}</td>
            <td>{{bird.price}}</td>
          </tr>
      </table>
    </div>

component.css组件.css

table {
    border-collapse: collapse;
    width: 95%;
    margin:0 0 0 20px;
  }
  
  table, th, td {
    border: 1px solid black;
  }

One simple way of achieving this would be to initialize your Array with a given length:实现此目的的一种简单方法是使用给定长度初始化数组:

arrBirds = Array.from({length: 5});

Then you can assign your data to this array然后你可以将你的数据分配给这个数组

if (data) {
    data.forEach((bird, index) => {
        this.arrBirds[index] = bird;
    });
}

Slightly adapt your display to handle null values:稍微调整您的显示以处理空值:

          <tr *ngFor="let bird of arrBirds">
            <td>{{ bird?.orderId || '-' }}</td>
            <td>{{ bird?.type || '-' }}</td>
            <td>{{ bird?.category || '-' }}</td>
            <td>{{ bird?.quantity || '-' }}</td>
            <td>{{ bird?.price || '-' }}</td>
          </tr>

And you should be good to go !你应该很高兴去!

This sort of data manipulation should be done in the component (or preferably a separate service).这种数据操作应该在组件(或者最好是单独的服务)中完成。

You can write a function in your OrderbokComponent to get the top 5 birds (based on your custom criteria, price / quantity, etc), store that in a variable and use that to generate the list.您可以在 OrderbokComponent 中编写一个函数来获取前 5 只鸟(基于您的自定义标准、价格/数量等),将其存储在一个变量中并使用它来生成列表。

You can even sort the arrBirds array and do something like this in ngFor :您甚至可以对 arrBirds 数组进行排序并在 ngFor 中执行以下操作:

<tr *ngFor="let bird of arrBirds | slice:0:4">
            <td>{{ bird?.orderId || '-' }}</td>
            <td>{{ bird?.type || '-' }}</td>
            <td>{{ bird?.category || '-' }}</td>
            <td>{{ bird?.quantity || '-' }}</td>
            <td>{{ bird?.price || '-' }}</td>
          </tr>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM