【问题标题】:How to develop a search option to search by column in MVC View table如何开发搜索选项以在 MVC 视图表中按列搜索
【发布时间】:2018-05-04 21:11:10
【问题描述】:

是否有一种按列搜索 .cshtml 表中数据的正确方法。每列必须有一个文本框。 我用javascript试过了,下面是我的代码。但它不起作用。谁能指导我正确地做到这一点?

        function searchSup()
    {
        $('#example thead th').each
            ( function ()
                {
                    var title = $(this).text();
                    $(this).html('<input type="text" id="SupInput" placeholder="Search ' + title + '" class="form-control"/>');
                } 
            );

        var input, filter, table, tr, td, i;
        input = document.getElementById("SupInput");
        filter = input.value.toUpperCase();
        table = document.getElementById("SupTable");
        tr = table.getElementsByTagName("tr");
            for (i = 0; i < tr.length; i++) 
                {
                    var supname = tr[i].cells[0].textContent.toUpperCase();
                    var contact = tr[i].cells[1].textContent.toUpperCase();
                    var address = tr[i].cells[2].textContent.toUpperCase();
                    var telephone = tr[i].cells[3].textContent.toUpperCase();
                    if (td) 
                        {
                            if (supname.indexOf(filter) > -1 || contact.indexOf(filter) > -1 || address.indexOf(filter) > -1 || telephone.indexOf(filter) > -1) 
                                {
                                    tr[i].style.display = "";
                                } 
                                else 
                                {
                                    tr[i].style.display = "none";
                                }
                        }
                }   
    }

我在 tr = table.getElementsByTagName("tr");getElementsByTagName value is null 这一行中遇到错误

谁能帮帮我?

【问题讨论】:

  • 请根据minimal reproducible example提供示例html。另请注意,您不能在页面中重复 id ......根据定义它们是唯一的

标签: javascript jquery asp.net asp.net-mvc null


【解决方案1】:

您可以使用我成功集成到一些 ASP.NET MVC 项目的jQuery DataTable with Custom Column Search 没有问题。

还有一些其他的演示,但首先前面的页面可能对你来说已经足够了。

DataTable (Server side)

希望这会有所帮助...

【讨论】:

  • 我正在关注来自this 网站的代码。后来我在W3学校发现了类似的东西并将两者结合起来。如果我可以像上面的链接那样开发我的 MVC 应用程序搜索功能,那将很有帮助。但我收到一条错误消息,说 DataTable() 不是函数,我无法解决它。我发布了here,但没有得到答复。请帮帮我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多