使用JavaScript在第三列进行HTML表格搜索

系统三重

我正在尝试在表empTable中使用IsProtected搜索存在的任何员工状态。如果存在任何行,则变量ProtectedIsfound必须为true

    <table id="empTable" >
    <tr>
        <th>
            Employee Name
        </th>
        <th>
            Designation
        </th>
     <th>
            Status
        </th>
        
    </tr>
for (i = 0; i < Model.attendanceLogList.Count; i++)
{
    <tr>
               <td>@Model.attendanceLogList[i].EmployeeName</td>
               <td>@Model.attendanceLogList[i].Designation</td>
           <td>@Model.attendanceLogList[i].IsProtected</td>  // it is boolean in model
       
      </tr>
}


</table>
<script>
    var ProtectedIsfound = True // if any record exist with the value is Protected in the column status in the table empTable 
</script>
吕志

我正在尝试在表empTable中使用IsProtected搜索存在的任何员工状态。如果存在任何行,则变量ProtectedIsfound必须为true

您是说每一行,如果IsProtected为true,那么ProtectedIsFound必须为true?在这种情况下,请参考以下方法:

  1. 使用JavaScript或JQuery方法:遍历表行和单元格并检查IsProtected是否为true,然后根据结果更改ProtectedIsFound值。

     @*JQuery reference*@
     <script src="~/lib/jquery/dist/jquery.js"></script>
     <script type="text/javascript">
         var ProtectedIsfound = false // default value. 
         //JavaScript method:
         document.addEventListener("DOMContentLoaded", function (event) {
             //loop through the tr and td, then based on the IsProtected value to change the ProtectedIsFound value.
             var trlist = document.getElementById("empTable").getElementsByTagName("tr");
             for (var i = 1; i < trlist.length; i++) {
                 if (trlist[i].getElementsByTagName("td")[2].innerText.toLowerCase() == "true") { 
                     ProtectedIsfound = true;
                     break;
                 }
             }
             console.log(ProtectedIsfound);
         });
         //JQuery script
         $(function () {
             $("#empTable").find("tr:not(:first-child)").each(function (index, item) {
                 if ($(item).find("td")[2].innerText.toLowerCase() == "true") {
                     ProtectedIsfound = true;
                     return false;
                 }
             });
             console.log(ProtectedIsfound);
         });
     </script>
    
  2. 使用Where和Select语句,代码如下:

     <script type="text/javascript">
         var ProtectedIsfound = @Model.attendanceLogList.Where(c => c.IsProtected ==true).Select(c=>c).ToList().Count > 0 ? true : false;
         console.log(ProtectedIsfound); 
     </script>
    

HTML页面元素如下(Asp.net核心Razor页面应用程序):

    @page
    @model RazorWebApplication5.Pages.HtmlTableModel
    @{ 
    }
        <table id="empTable">
            <tr>
                <th>
                    Employee Name
                </th>
                <th>
                    Designation
                </th>
                <th>
                    Status
                </th>

            </tr>
            @for (var i = 0; i < Model.attendanceLogList.Count; i++)
            {
                    <tr>
                        <td>@Model.attendanceLogList[i].EmployeeName</td>
                        <td>@Model.attendanceLogList[i].Designation</td>
                        <td>@Model.attendanceLogList[i].IsProtected</td>  @*// it is boolean in model*@

                    </tr>
            }
        </table>

.cshtml.cs文件:

    public class HtmlTableModel : PageModel
    {
        public List<AttendanceLog> attendanceLogList { get; set; }
        public void OnGet()
        {
            //initial data
            attendanceLogList = new List<AttendanceLog>()
            {
                new AttendanceLog(){ EmployeeName="Tom", Designation="A", IsProtected=false},
                new AttendanceLog(){ EmployeeName="Jack", Designation="B", IsProtected=false},
                new AttendanceLog(){ EmployeeName="Lucy", Designation="CC", IsProtected=true},
                new AttendanceLog(){ EmployeeName="Daive", Designation="D", IsProtected=false},
                new AttendanceLog(){ EmployeeName="Hanke", Designation="E", IsProtected=false}
            };
        }
    }

    public class AttendanceLog
    {
        public string EmployeeName { get; set; }
        public string Designation { get; set; }
        public bool IsProtected { get; set; }
    }

测试结果:

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 Javascript 函数搜索 HTML 表格时,表格标题消失

使用javascript对HTML表格的项目进行分类

使用JavaScript对HTML表格进行排序

在第三列中合并表格的两列

如何使用 MySQL 在第三列中对具有最大值的 2 列进行分组?

使用第三方集成工具对HTML进行双重编码

使用部分匹配过滤/搜索表格 - HTML 和 Javascript

如何使用 JavaScript 在 HTML 表格中搜索单元格?

如何使用grep提取第三列?

excel匹配2列以对第三列进行排序

在HTML表格中使用Javascript进行下拉式多重过滤

使用 JavaScript 对 HTML 表格进行类似 Excel 的过滤器

如何使用javascript在html中进行三级选择

如何使用共享的第三列(从最低到最高)在第四列中对ID的组合进行排序?

将我的 HTML 表格中的两个单元格相乘,并使用 javascript 自动将结果放在第三个单元格中。没有 Jquery

使用第三个列表对列表进行排序

使用第三方库进行编译

无法使用Try,Except进入第三URL进行查询

使用第三方库进行Python部署

使用 JavaScript 的 HTML 表格

如何在没有第三方的情况下使用Angular / Typescript对HTML表进行排序?

使用由两个列表排序产生的排序来对第三列表进行排序?

为什么我的HTML不显示第三列

使用MySQL计算2列的差等于第三列

使用不同的表格列进行狮身人面像搜索权重排序

如何自动从第三方网站对网站进行数据库搜索?

如何在第二和第三定界符之间进行搜索

根据Google表格中的两列匹配项返回第三列的值

在Microsoft Excel 2013中搜索两列,并将第三列的值设置为特定值