用JS过滤表隐藏行

Filter table hiding rows with JS

本文关键字:隐藏 过滤 JS      更新时间:2023-09-26

我在razor中有一个表:

<table id="tblCaseTeam">
    <thead>
        <tr>
            <th>
            </th>
            <th style="width: 30em;">
                @Html.DisplayColumnNameFor(Model, m => m.Action)
            </th>
            <th style="width: 7em;">
                @Html.DisplayColumnNameFor(Model, m => m.Owner)
            </th>
            <th style="width: 7em;">
                @Html.DisplayColumnNameFor(Model, m => m.Deadline)
            </th>
            <th style="width: 15em;">
                @Html.DisplayColumnNameFor(Model, m => m.Status)
            </th>
            <th style="width: 15em;">
                @Html.DisplayColumnNameFor(Model, m => m.Completed)
            </th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { Id = item.ID })
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Action)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Owner)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Deadline)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Status)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Completed)
                </td>
            </tr>
        }
    </tbody>
</table>

DDL有3个值,完成,全部和不完整。表的最后一列是一个布尔值,在表中显示为复选框。

我正在寻找一个JS,过滤行(隐藏和显示)取决于DDL的选择如果"全部",那么就全部显示出来。如果是"Complete",那么只显示最后一列中Complete值为true的行。如果"不完整",则只显示最后一列中完整值为false的行。

我尝试了一些JQuery,但不知道怎么做

$table.find("tr").hide().filter(function () { return XXXX }).show();

我的ddl。

<div id="dropdownlist">
    @Html.DropDownList("Filter", new SelectList(ViewBag.SearchOptionsList, "Value", "Text"))
</div>

和知道所选值的代码:

var sel = $("#Filter option:selected").text();

问题2:是否有可能,而不是这个JS过滤,对模型进行客户端过滤,比如根据DDL的值在模型的表中放入Where子句。但是表会相应地刷新吗?

谢谢

你可以这样做

 <tbody>
        @foreach (var item in Model)
        {
            <tr class='@item.Completed ? "complete" : "incomplete"'>

然后在下拉菜单中输入

$( "#dropdownlist" ).change(function() {
  if ($( this ).val() == 'complete'){
    $(".incomplete").hide();
    $(".complete").show();
  }
  else if ($( this ).val() == 'incomplete'){
    $(".incomplete").show();
    $(".complete").hide();
  }
  else {
    $(".incomplete").show();
    $(".complete").show();
  }
});