简体   繁体   English

仅使用 Javascript 在表格的最后一行显示按钮

[英]Show Button in Last Row of the Table only using Javascript

I have a 4-column table with the last column containing a "+" button.我有一个 4 列的表格,最后一列包含一个“+”按钮。 its function is to add additional column when clicked.它的功能是在点击时添加额外的列。 However it is being shown in all of the rows, I want it to be displayed only on the last row of the table.但是它显示在所有行中,我希望它只显示在表格的最后一行。

Here's the code这是代码

  <table class="table table-responsive table-bordered" style="width:100%" id="HeightConfig">
    <tbody id="HeightConfigBody">
@if (Model != null)
{
@foreach (var data in Model.Reverse())
{
     <tr>
                         <td style="width:40%">
  <label><b>Recorded Time:</b> <input type="text" id="recordDate"  class="recordDate"   
  value="@data.recordDate.ToString("yyyy-MM-dd")" disabled></label>
                      </td>



                       <td style="width:40%">
                         <label><b>Height (CM):</b> <input type="text" id="ColumnTypeData" class="ColumnTypeData" 
                          value="@data.ColumnTypeData" ></label>
                      </td>

                      <td style="width:40%">
                           <a class="btn btn-primary btn-default" title="delete"
                            data-target="#modal-container" data-toggle="modal" data-align="center">
                            <div id="minusHeight"><i class="fa fa-minus" aria-hidden="true"></i></div>
                            </a>


     <a class="btn btn-primary btn-default" title="add"  >
                              <div id="addHeight" ><i class="fa fa-plus" aria-hidden="true"></i></div>
     </a>
                       </td>

</tr>

}
}

     </tbody>
  </table>

Just showed the for loop inside the table because it is populating data.刚刚在表中显示了 for 循环,因为它正在填充数据。

Here's the screenshot这是屏幕截图

在此处输入图片说明

What I want is for the plus button to be displayed only at the last row of the table我想要的是加号按钮只显示在表格的最后一行

In your codes, you are generating rows based on the template which contains "+" button.在您的代码中,您将根据包含“+”按钮的模板生成行。 You should take this part out of the template:你应该把这部分从模板中去掉:

<a class="btn btn-primary btn-default" title="add">
    <div id="addHeight" ><i class="fa fa-plus" aria-hidden="true"></i>
    </div>
</a>

Instead, you should have:相反,您应该:

  1. a function checking/finding the place/row number/element id of the newest row.检查/查找最新行的位置/行号/元素 ID 的函数。 Something like this: document.getElementById("mytable").rows.length will return number of rows existing in the table.像这样: document.getElementById("mytable").rows.length将返回表中存在的行数。 Be careful of indexing though.不过要小心索引。

  2. another function to insert/append the "+" button to specific row.将“+”按钮插入/附加到特定行的另一个功能。 This could give you some insights. 可以给你一些见解。

  3. A way to delete the "+" button and reinsert since you have "-" button which would delete rows in the middle of the table.一种删除“+”按钮并重新插入的方法,因为您有“-”按钮可以删除表格中间的行。 Button position requires update too.按钮位置也需要更新。

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

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