简体   繁体   English

如何使用 html / angular / javascript 扩展表

[英]How to expand a table using html / angular / javascript

I'm new in angular 2+.我是 angular 2+ 的新手。 I have created a table in angular web api.我在 angular web api 中创建了一个表。 I need to do it like this - when I press the button (icon) expand the table with the data from my web api.我需要这样做 - 当我按下按钮(图标)时,使用来自我的 web api 的数据展开表格。 I did it that way but it's not working.我是这样做的,但它不起作用。 Could someone help me with a solution?有人可以帮我解决吗?

app.component.html app.component.html

   <div class="card-body">
      <table datatable class="table table-striped table-bordered hover" [dtOptions]="dtOptions" style="width:100%">
         <thead>
            <tr>
               <th width="2%" class="text-center"></th>
               <th width="8%" class="text-center">Name</th>
               <th width="8%" class="text-center">Price</th>
            </tr>
         </thead>
         <tbody>
            <tr *ngIf="empty">
               <td colspan="8" class="text-center" translate>shared.table.not-found</td>
            </tr>
            <tr *ngFor="let product of products; index as i" (click)="onSelectLine(i)"
            [ngClass]="{'tr-selected': selectedIndex === i}">
            <td class="text-left">
               <a type="button" (click)="onProductComponent(product)">
               <i class="fa fa-chevron-right"></i>
               </a>
            </td>
            <td class="text-left">{{product.name}}</td>
            <td class="text-left">{{product.price}}</td>
            </tr>
         <thead>
            <tr>
               <td colspan="12" >
                  <div *ngIf="isShown" class="row container-fluid"  id="divshow" >
               <td class="text-left">{{product.name}}</td>
               <td class="text-left">{{product.price}}</td>
               </div>
               </td>
            </tr>
         </thead>
         </tbody>
      </table>
   </div>
</div>

app.component.ts app.component.ts

isShown = false;

onProductComponent(product: Product) {
        this.isShown = ! this.isShown;
        this._product = product;
        this.productComponentService.getProduct(this._product.id)
        .subscribe(element => {
        });
} 

It would be like this会是这样

在此处输入图像描述

You can add isShown property to every product.您可以为每个产品添加isShown属性。 And change only that value.并且只改变那个值。

If you want to have multiple rows opened than just remove the this.products.map() part from onProductComponent如果您想打开多行,而不仅仅是从onProductComponent中删除this.products.map()部分

 public products = [
    { name: "Product 1", price: 101, isShown: false },
    { name: "Product 2", price: 102, isShown: false },
    { name: "Product 2", price: 103, isShown: false },
    { name: "Product 3", price: 104, isShown: false },
    { name: "Product 5", price: 105, isShown: false }
  ];

  onProductComponent(product) {
    this.products.map(p => {
      if (product !== p) {
        p.isShown = false;
      }
    });
    product.isShown = !product.isShown;
  }

This will lead to change in HTML because you can move the hidden section inside of the loop.这将导致 HTML 发生变化,因为您可以将隐藏部分移动到循环内。 Because you don't need to save product .因为您不需要保存product

<div class="card-body">
  <table datatable class="table table-striped table-bordered hover" style="width:100%">
    <thead>
      <tr>
        <th width="2%" class="text-center"></th>
        <th width="8%" class="text-center">Name</th>
        <th width="8%" class="text-center">Price</th>
      </tr>
    </thead>
    <tbody>
      <ng-container *ngFor="let product of products; index as i">
        <tr>
          <td class="text-left">
            <a type="button" (click)="onProductComponent(product)">
              <i class="fa fa-chevron-right"></i>
              Open
            </a>
          </td>
          <td class="text-left">{{product.name}}</td>
          <td class="text-left">{{product.price}}</td>
        </tr>
        <thead>
          <tr>
            <td colspan="12">
              <div *ngIf="product.isShown" class="row container-fluid" id="divshow">
                <span class="text-left">{{product.name}}</span>
                <span class="text-left">{{product.price}}</span>
              </div>
            </td>
          </tr>
        </thead>
      </ng-container>
    </tbody>
  </table>
</div>

You can check a working example here您可以在此处查看一个工作示例

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

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