Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 使用JavaScript在第三列上搜索HTML表_Asp.net Core_Html Table - Fatal编程技术网

Asp.net core 使用JavaScript在第三列上搜索HTML表

Asp.net core 使用JavaScript在第三列上搜索HTML表,asp.net-core,html-table,Asp.net Core,Html Table,我正在尝试搜索任何在表格中受保护且可清空的员工状态。如果存在任何行,则变量ProtectedIsfound必须为true <table id="empTable" > <tr> <th> Employee Name </th> <th> Designation </th>

我正在尝试搜索任何在表格中受保护且可清空的员工状态。如果存在任何行,则变量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>

员工姓名
任命
地位
对于(i=0;i
  • 将for循环更改为: “对于(i=0;i 我正在尝试搜索任何存在iProtected in的员工状态 tabl为空。如果存在任何行,则变量 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>
    
    您的意思是,对于每一行,如果IsProtected为true,则ProtectedIsFound必须为true?如果是这种情况,请参考以下方法:

  • 使用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>
    
    @*JQuery参考*@
    var ProtectedIsfound=false//默认值。
    //JavaScript方法:
    document.addEventListener(“DOMContentLoaded”),函数(事件){
    //循环通过tr和td,然后根据iProtected值更改ProtectedIsFound值。
    var trlist=document.getElementById(“可空”).getElementsByTagName(“tr”);
    对于(变量i=1;i
  • 使用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>
    
    
    var ProtectedIsfound=@Model.attendanceglist.Where(c=>c.IsProtected==true)。选择(c=>c.ToList()。计数>0?true:false;
    console.log(ProtectedIsfound);
    
  • Html页面元素如下(Asp.net核心Razor页面应用程序):

    @page
    @模型RazorWebApplication5.Pages.HtmlTableModel
    @{ 
    }
    员工姓名
    任命
    地位
    @对于(变量i=0;i
    .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; }
        }
    
    公共类HtmlTableModel:PageModel {
    public List

    这是一个非常简单的逻辑,但是您需要包括一个数据库,并使用存储过程拉写数据(这是一种方法),因此状态可以保存在某个位置。当我在Javascript中应用linq查询时,显示以下错误System.ArgumentNullException:值不能为null。参数名称:System.linq.Enumerable.Where[TSource](IEnumerable
    1 source,Func
    2谓词)在我上面的示例中,我使用的是asp.net core Razor页面,我已经发布了整个代码,请检查。根据错误消息,在JavaScript脚本中,“Model”似乎为空。如果您没有使用asp.net core Razor页面,请尝试使用JavaScript/JQuery方法循环遍历表并更改ProtectedIsFound值。
        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; }
        }