NgFor带有可变键?

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

NgFor with variable keys?

问题

'Headers'(headers: any;)是从'data.items[0]'中使用'Object.keys'提取的键。

我应该如何循环遍历'data.items'对象。如果我尝试使用*ngfor进行循环遍历,它不起作用。

这是我想要的结果。
NgFor带有可变键?

但是当我使用'item.header'时出现错误,

> 属性'header'在类型'{ name: string; alias:
> string[]; qty: number; price: number; total: number; }上不存在

如果我尝试'item[header]',

> 因为类型为'any'的表达式不能用于索引类型'{ name: string; alias: string[]; qty: number; price: number; total: number; },所以元素隐式具有'any'类型

代码

对象

  private itemData: sale = {
    type: '',
    data: { date: ' ', InvoiceNo: ' ', tax: ' ' },
    total: { semitotal: 0, grandtotal: 0 },
    items: [
      { name: 'item 1', alias: ['01a'], qty: 0, price: 0, total: 0 },
      { name: 'item 2', alias: ['02a'], qty: 5, price: 250, total: 0 },
      { name: 'item 3', alias: ['03a'], qty: 6, price: 350, total: 0 },
      { name: 'item 4', alias: ['04a'], qty: 1, price: 150, total: 0 },
    ],
    params: {},
  };

Angular HTML 代码

 <tbody>
        <tr *ngFor="let item of itemData.items" class="table-data" >
            <td *ngFor="let header of headers"> 
                <input  type="text" [value]="item.header">  <---------- 问题 
            </td>
        </tr>
 </tbody>
英文:

'Headers' (headers: any;) are keys extracted from'data.items[0]' using 'object.keys'.

How should i loop through 'data.items' objects. It doesn't work if i try to loop though with *ngfor

this is the result i want.
NgFor带有可变键?

But i am getting error when i use 'item.header',

> Property 'header' does not exist on type '{ name: string; alias:
> string[]; qty: number; price: number; total: number; }

if i try 'item[header]'

> Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{ name: string; alias: string[]; qty: number; price: number; total: number; }

CODE

Object

  private itemData: sale = {
    type: '',
    data: { date: ' ', InvoiceNo: ' ', tax: ' ' },
    total: { semitotal: 0, grandtotal: 0 },
    items: [
      { name: 'item 1', alias: ['01a'], qty: 0, price: 0, total: 0 },
      { name: 'item 2', alias: ['02a'], qty: 5, price: 250, total: 0 },
      { name: 'item 3', alias: ['03a'], qty: 6, price: 350, total: 0 },
      { name: 'item 4', alias: ['04a'], qty: 1, price: 150, total: 0 },
    ],
    params: {},
  };

Angular HTML Code

 <tbody>
        <tr *ngFor="let item of itemData.items" class="table-data" >
            <td *ngFor="let header of headers"> 
                <input  type="text" [value]="item.header">  <---------- Problem 
            </td>
        </tr>
 </tbody>

答案1

得分: 2

You can create a type for the keys of an individual item and use that to index on an individual item:

type SaleTableHeader = "name" | "qty" | "alias" | "price" | "total";

// Component class
headers: SaleTableHeader[] = Object.keys(this.itemData.items[0]) as SaleTableHeader[];

// Component Template
<tbody>
  <tr *ngFor="let item of itemData.items" class="table-data">
    <td *ngFor="let header of headers">
      <input type="text" [value]="item[header]" />
    </td>
  </tr>
</tbody>
英文:

You can create a type for the keys of an individual item and use that to index on an individual item:

type SaleTableHeader = &quot;name&quot; | &quot;qty&quot; | &quot;alias&quot; | &quot;price&quot; | &quot;total&quot;;

// Component class
headers: SaleTableHeader[] = Object.keys(this.itemData.items[0]) as SaleTableHeader[];

// Component Template
&lt;tbody&gt;
  &lt;tr *ngFor=&quot;let item of itemData.items&quot; class=&quot;table-data&quot;&gt;
    &lt;td *ngFor=&quot;let header of headers&quot;&gt;
      &lt;input type=&quot;text&quot; [value]=&quot;item[header]&quot; /&gt;
    &lt;/td&gt;
  &lt;/tr&gt;
&lt;/tbody&gt;

答案2

得分: 1

&lt;tbody&gt;
    &lt;table class=&quot;table&quot;&gt;
    &lt;th *ngFor=&quot;let header of itemData.items[0] | keyvalue&quot; class=&quot;table-data&quot;&gt; 
        {{header.key}}
    &lt;/th&gt;
    &lt;tr *ngFor=&quot;let item of itemData.items&quot; class=&quot;table-data&quot; &gt;
       &lt;td *ngFor=&quot;let r of item | keyvalue&quot;&gt;
        &lt;input type=&quot;text&quot;[value]=&quot;r.value&quot;&gt;
       &lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;
&lt;/tbody&gt;
英文:

you could do this:

html

&lt;tbody&gt;
    &lt;table class=&quot;table&quot;&gt;
    &lt;th *ngFor=&quot;let header of itemData.items[0] | keyvalue&quot; class=&quot;table-data&quot;&gt; 
        {{header.key}}
    &lt;/th&gt;
    &lt;tr *ngFor=&quot;let item of itemData.items&quot; class=&quot;table-data&quot; &gt;
       &lt;td *ngFor=&quot;let r of item | keyvalue&quot;&gt;
        &lt;input type=&quot;text&quot;[value]=&quot;r.value&quot;&gt;
       &lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;
&lt;/tbody&gt;

Output
NgFor带有可变键?

huangapple
  • 本文由 发表于 2023年5月11日 01:23:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221107.html
匿名

发表评论

匿名网友

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

确定