I need to to auto calculate an input field on keyup function on other 2 input field which are created using Accordian

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

I need to to auto calculate an input field on keyup function on other 2 input field which are created using Accordian

问题

Example: 在长度和宽度的键盘弹起事件上计算面积。这三个都在折叠循环内定义。
我可以创建n个<div>来获取多边形的详细信息(我们使用折叠面板来实现,因为我们需要可折叠的<div>)。我们需要在输入长度和宽度时自动计算面积,并在每个多边形的面积输入框中显示。

我尝试使用keyup函数并在每个索引上获取值。对于普通的<div>,它可以工作,但当<div>在折叠面板类中时或者在循环中时,它不起作用。

<div class="accordion">
  <div class="accordion-content">
    <div>
      长度
      <input [(ngModel)]="item.length" (keyup)="functiontocalculate($event)" />
    </div>
    <div>
      宽度
      <input [(ngModel)]="item.breadth" (keyup)="functiontocalculate($event)" />
    </div>
    <div>面积 <input [(ngModel)]="item.area" /></div>
  </div>
</div>
英文:

Example: calculate area on keyup of length and breadth. All 3 are defined inside accordion loop.
I can create n no. <div> to fetch polygon details (which we are doing using accordion because we need collapsible div).We need to auto-calculate the area and show in the input box of area for each polygon at the time of entering l and b.

i tried using keyup function and get the value on each index. it works for normal div but doesnt work when the div is inside accordion class or(in loop)

&lt;div class=&quot;accordion&quot;&gt;
  &lt;div class=&quot;accordion-content&quot;&gt;
    &lt;div&gt;
      length
      &lt;input [(ngModel)]=&quot;item.length&quot; (keyup)=&quot;functiontocalculate($event)&quot; /&gt;
    &lt;/div&gt;
    &lt;div&gt;
      breadth
      &lt;input [(ngModel)]=&quot;item.breadth&quot; (keyup)=&quot;functiontocalculate($event)&quot; /&gt;
    &lt;/div&gt;
    &lt;div&gt;area &lt;input [(ngModel)]=&quot;item.area&quot; /&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

答案1

得分: 0

To make a calculate value (if it's a simple calculus) you can use:

&lt;input readOnly [value]=&quot;(+item.length)*(+item.breadth) || &#39;&#39;&quot; /&gt;

NOTE: The "+" before is to convert to a number the value. If the result is NaN, show ''.

In this way, you needn't use anything more.

Update Well, with this way we don't store the area in "item".

Generally, in a submit function, we can do:

this.items.forEach(item=&gt;{
   item.area=(+item.length)*(+item.breadth)
})

If we don't want to do this, really we can use the function "keyup", but I prefer using ngModelChange in this way. See that you pass to the function the "item":

&lt;input [ngModel]=&quot;item.length&quot; 
       (ngModelChange)=&quot;item.length=$event;calculateArea(item)&quot;/&gt;
&lt;input [ngModel]=&quot;item.breadth&quot; 
       (ngModelChange)=&quot;item.breadth=$event;calculateArea(item)&quot;/&gt;
&lt;input [value]=&quot;item.area&quot; /&gt;&lt;/div&gt;

//and in .ts

calculateArea(item:any)
{
   item.area=(+item.length)*(+item.breadth) || &#39;&#39;
}

See that the $event in ngModelChange is the value of the input.

英文:

To make a calculate value (if it's a simple calculus) you can use

&lt;input readOnly [value]=&quot;(+item.length)*(+item.breadth) || &#39;&#39;&quot; /&gt;

NOTE: the "+" before is to convert to number the value. If the result isNaN, show ''.

In this way you neend't use nothing more.

Update Well, with this way we don't store the area in "item".

Generally in a submit function we can do

this.items.forEach(item=&gt;{
   item.area=(+item.length)*(+item.breadth)
})

If we don't want to do this, really we can use the function "keyup", but I prefer use ngModelChange in the way. See that you pass to the function the "item"

  &lt;input [ngModel]=&quot;item.length&quot; 
         (ngModelChange)=&quot;item.length=$event;calculateArea(item)&quot;/&gt;
  &lt;input [ngModel]=&quot;item.breadth&quot; 
         (ngModelChange)=&quot;item.breadth=$event;calculateArea(item)&quot;/&gt;
&lt;input [value]=&quot;item.area&quot; /&gt;&lt;/div&gt;

//and in .ts

calculateArea(item:any)
{
   item.area=(+item.length)*(+item.breadth) || &#39;&#39;
}

See that the $event in ngModelChange is the value of the input

huangapple
  • 本文由 发表于 2023年6月8日 17:10:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430292.html
匿名

发表评论

匿名网友

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

确定