使用JQuery从HTML表格中删除空白行

3

我有一个HTML表格:

<table id="indicationResults" class="resultsGrid" cellpadding="2" cellspacing="0" >

表格中的某些单元格仅包含字符串,取决于特定权限。例如,这是一行仅在用户拥有特定权限时才在每个单元格中放置标签和数据的示例:

<tr>
    <td id="DV01Label" class="resultsCell">
        <%= CustomHelpers.StringWithPermission("DV01", new string[] { PERMISSIONS.hasALM })%>
    </td>
    <td id="DV01Value" class="alignRight">
        <%= CustomHelpers.StringWithPermission(Model.Results.DV01.ToString(Chatham.Web.Data.Constants.Format.CurrencyCentsIncludedFormatString), new string[] { PERMISSIONS.hasALM })%>
    </td>
    <td id="DV01Fixed" class="alignRight">
        <%= CustomHelpers.StringWithPermission(Model.Results.FixedComponent().DV01.ToString(Chatham.Web.Data.Constants.Format.CurrencyCentsIncludedFormatString), new string[] {PERMISSIONS.hasALM})%>
    </td>
    <td id="DV01Floating" class="alignRight">
        <%= CustomHelpers.StringWithPermission(Model.Results.FloatingComponent().DV01.ToString(Chatham.Web.Data.Constants.Format.CurrencyCentsIncludedFormatString), new string[] { PERMISSIONS.hasALM })%>
    </td>
</tr>

在页面加载后,如何使用JQuery返回到这个特定的表格并删除所有完全为空的行,以便不再看到一个微小的空行而是完全没有了。

2个回答

11

使用jQuery:

$('#indicationResults tr').each(function () {
     if (!$.trim($(this).text())) $(this).remove();
});

尽管如果您能修改您的ASP只在用户拥有合适权限时输出行将更好 :)


先治病,后医好,正如人们所说的 :) - nik0lai
1
+1 对于在服务器端解决问题的评论。这不应该是一个 JavaScript 解决方案。 - JasCav
那个修改一定会发生,即使要我拼命也没问题,哈哈。不过得等到下一个迭代才行 =/ - slandau

0
你应该编写一个合适的HTML帮助器。与Web Forms不同,MVC使您完全控制生成的HTML,并且使用JavaScript修复损坏的HTML是完全不能容忍的。
因此:
public static MvcHtmlString RowWithPermission(
    this HtmlHelper htmlHelper, 
    string cssClass, 
    string id, 
    string value, 
    string[] permissions
)
{
    if (HasPermissions(permissions))
    {
        var td = new TagBuilder("td");
        td.AddCssClass(cssClass);
        td.GenerateId(id);
        td.InnerHtml = value;
        return MvcHtmlString.Create(td.ToString());
    }
    return MvcHtmlString.Empty;
}

然后:

<tr>
    <%= Html.RowWithPermission("resultsCell", "DV01Label", "DV01", new string[] { PERMISSIONS.hasALM }) %>
    <%= Html.RowWithPermission("alignRight", "DV01Value", Model.Results.DV01.ToString(Chatham.Web.Data.Constants.Format.CurrencyCentsIncludedFormatString), new string[] { PERMISSIONS.hasALM }) %>
    ...
</tr>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接