如何动态编辑一个表格单元格以影响另一个表格单元格?

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

How to dynamically edit a table cell that affect another table cell?

问题

我有一个动态填充数据的表格,显然是通过循环来填充的。它的数据填充如下图所示。

正如您所看到的,表格包括可编辑字段。我需要的是,如果我编辑一个单元格,我需要在右侧的总单元格中显示更改。

以下是表格的代码:

<tr>
    <td colspan="4">
        <h4 class="ui header">
            <i class="settings icon"></i>
            <div class="content">
                Loans
            </div>
        </h4>
    </td>
</tr>
@for (int i = 0; i < Model.RecoveriesLoan.Count; i++)
{
    @Html.HiddenFor(modelItem => Model.RecoveriesLoan[i].TXR_ABATEMENT_ID)
    @Html.HiddenFor(modelItem => Model.RecoveriesLoan[i].TXR_TX_TYPE_ID)
    @Html.HiddenFor(modelItem => Model.RecoveriesLoan[i].TXR_MEMBER_ID)
    @Html.HiddenFor(modelItem => Model.RecoveriesLoan[i].TXR_SEQ_NO)
    @Html.HiddenFor(modelItem => Model.RecoveriesLoan[i].TXR_RECORD_STATUS)
    @Html.HiddenFor(modelItem => Model.RecoveriesLoan[i].TXR_RECEIVABLE_AMT)

    <tr class="@(Model.RecoveriesLoan[i].TXR_RECEIVED_AMT < Model.RecoveriesLoan[i].TXR_RECEIVABLE_AMT ? "negative" : "positive")">
        <td>
            @Html.DisplayFor(modelItem => Model.RecoveriesLoan[i].MTX_TX_DESC)
        </td>
        <td id="LoanRecv">
            @Html.DisplayFor(modelItem => Model.RecoveriesLoan[i].TXR_RECEIVABLE_AMT)
        </td>
        <td>
            <div class="ui fluid icon input no-margin-bottom">
                @Html.EditorFor(modelItem => Model.RecoveriesLoan[i].TXR_RECEIVED_AMT, new { @id = "LoanRec", @class = "Loan" })
            </div>
        </td>
        <td id="LoanUR">
            @{decimal ur = Model.RecoveriesLoan[i].TXR_RECEIVABLE_AMT - Model.RecoveriesLoan[i].TXR_RECEIVED_AMT;}
            @ur
        </td>
    </tr>
}

以下是我尝试使用 JavaScript 实现的方式:

<script>
    $('#RecoveriesCont_0__TXR_RECEIVED_AMT').change(function () {
        var conRecv = $('#RecoveriesCont_0__TXR_RECEIVABLE_AMT').val();
        var conRec = $('#RecoveriesCont_0__TXR_RECEIVED_AMT').val();
        var conUR = 0;

        console.log(conRecv);
        conUR = conRecv - conRec;

        console.log("conUR:", parseFloat(conUR).toFixed(2));

        $('#ContUR').val(parseFloat(conUR).toFixed(2));
    });

    $('#LoanRec').change(function () {
        var loanRecv = $('#LoanRecv').val();
        var loanRec = $('#LoanRec').val();

        var loanUR = 0;

        loanUR = loanRecv - loanRec;

        $('#LoanUR').val(loanUR.toFixed(2));
    });
</script>

如您所见,我只尝试实现了表格的第一行。但我需要相应地编辑所有行。上述代码完全不起作用。

如何使用 JavaScript 实现此目标?帮助将不胜感激。

英文:

I have a table that dynamically fills data obviously through a loop. It fills data as shown in the figure below.如何动态编辑一个表格单元格以影响另一个表格单元格?

As you can see above The table is filled including editable fields. What I need is, If I edit one cell I need to show the change in the right side cell which is the total.

Here's the code for the table.

&lt;tr&gt;
&lt;td colspan=&quot;4&quot;&gt;
&lt;h4 class=&quot;ui header&quot;&gt;
&lt;i class=&quot;settings icon&quot;&gt;&lt;/i&gt;
&lt;div class=&quot;content&quot;&gt;
Loans
&lt;/div&gt;
&lt;/h4&gt;
&lt;/td&gt;
&lt;/tr&gt;
@for (int i = 0; i &lt; Model.RecoveriesLoan.Count; i++)
{
@Html.HiddenFor(modelItem =&gt; Model.RecoveriesLoan[i].TXR_ABATEMENT_ID)
@Html.HiddenFor(modelItem =&gt; Model.RecoveriesLoan[i].TXR_TX_TYPE_ID)
@Html.HiddenFor(modelItem =&gt; Model.RecoveriesLoan[i].TXR_MEMBER_ID)
@Html.HiddenFor(modelItem =&gt; Model.RecoveriesLoan[i].TXR_SEQ_NO)
@Html.HiddenFor(modelItem =&gt; Model.RecoveriesLoan[i].TXR_RECORD_STATUS)
@Html.HiddenFor(modelItem =&gt; Model.RecoveriesLoan[i].TXR_RECEIVABLE_AMT)
&lt;tr class=&quot;@(Model.RecoveriesLoan[i].TXR_RECEIVED_AMT &lt; Model.RecoveriesLoan[i].TXR_RECEIVABLE_AMT ? &quot;negative&quot; : &quot;positive&quot;)&quot;&gt;
&lt;td&gt;
@Html.DisplayFor(modelItem =&gt; Model.RecoveriesLoan[i].MTX_TX_DESC)
&lt;/td&gt;
@*&lt;td&gt;
@Html.DisplayFor(modelItem =&gt; Model.RecoveriesLoan[i].TXR_SEQ_NO)
&lt;/td&gt;*@
&lt;td id=&quot;LoanRecv&quot;&gt;
@Html.DisplayFor(modelItem =&gt; Model.RecoveriesLoan[i].TXR_RECEIVABLE_AMT)
&lt;/td&gt;
&lt;td&gt;
&lt;div class=&quot;ui fluid icon input no-margine-bottom&quot;&gt;
@Html.EditorFor(modelItem =&gt; Model.RecoveriesLoan[i].TXR_RECEIVED_AMT , new { @id = &quot;LoanRec&quot;, @class=&quot;Loan&quot; })
&lt;/div&gt;
&lt;/td&gt;
&lt;td id=&quot;LoanUR&quot;&gt;
@{decimal ur = Model.RecoveriesLoan[i].TXR_RECEIVABLE_AMT - Model.RecoveriesLoan[i].TXR_RECEIVED_AMT;}
@ur
&lt;/td&gt;
&lt;/tr&gt;
}

Here's how I tried to implement it using javascript:

&lt;script&gt;
$(&#39;#RecoveriesCont_0__TXR_RECEIVED_AMT&#39;).change(function () {
var conRecv = $(&#39;#RecoveriesCont_0__TXR_RECEIVABLE_AMT&#39;).val();
var conRec = $(&#39;#RecoveriesCont_0__TXR_RECEIVED_AMT&#39;).val();
var conUR = 0;
console.log(conRecv);
conUR = conRecv - conRec;
console.log(&quot;conUR:&quot;, parseFloat(conUR).toFixed(2));
$(&#39;#ContUR&#39;).val(parseFloat(conUR).toFixed(2));
});
$(&#39;#LoanRec&#39;).change(function () {
var loanRecv = $(&#39;#LoanRecv&#39;).val();
var loanRec = $(&#39;#LoanRec&#39;).val();
var loanUR = 0;
loanUR = loanRecv - loanRec;
$(&#39;#LoanUR&#39;).val(loanUR.toFixed(2));
});
&lt;/script&gt;

As you can see I have only tried to achieve only for the first row of the table. But I need to edit all the rows accordingly. The above code didn't work at all.

How do I achieve this using javascript. Help would be appreciated.

答案1

得分: 0

通过它们的类选择器获取输入,使用关键词 "this" 获取特定输入的值,然后遍历到父元素,并通过其类找到所需的单元格。

您还需要消除重复的ID,因为它们应该在页面中是唯一的。

<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-js -->

$(".input").change(function() {
  var inputVal = parseInt($(this).val());
  var anotherVal = parseInt($(this).closest("tr").find(".first").text());
  
  var sum = anotherVal - inputVal;
  
  $(this).closest("tr").find(".third").text(sum)
})

<!-- 语言:lang-html -->

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
  <td class="first">100</td>
  <td class="second"><input type="text" class="input"></td>
  <td class="third">0</td>
</tr>
</table>

<!-- 结束代码片段 -->

请注意,我已经省略了不需要翻译的部分,并按您的要求只返回翻译好的内容。

英文:

Get inputs by their class selectors, use this keyword to get this certain input's value, traverse to parent and find cell you need by it's class.

You also need to get rid of duplicated ids as every one of them should be unique to the page.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&quot;.input&quot;).change(function() {
var inputVal = parseInt($(this).val());
var anotherVal = parseInt($(this).closest(&quot;tr&quot;).find(&quot;.first&quot;).text());
var sum = anotherVal - inputVal;
$(this).closest(&quot;tr&quot;).find(&quot;.third&quot;).text(sum)
})

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td class=&quot;first&quot;&gt;100&lt;/td&gt;
&lt;td class=&quot;second&quot;&gt;&lt;input type=&quot;text&quot; class=&quot;input&quot;&gt;&lt;/td&gt;
&lt;td class=&quot;third&quot;&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月3日 19:04:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577457.html
匿名

发表评论

匿名网友

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

确定