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

发布于 2024-10-13 09:37:05 字数 1290 浏览 2 评论 0原文

我有一个 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 加载页面后,如何返回并删除此特定表中所有完全空的行,因此而不是看到一个小小的空行,它只是不存在。

I have an HTML table:

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

Some of the cells in the table only contain strings depending on specific permissions. For example, here is one row that only puts a label and data in each cell if the user has a specific permission:

<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>

How do I go back through and delete and all completely empty rows in this SPECIFIC table after the page loads using JQuery, so instead of seeing a tiny little empty row, it's just not there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗笺 2024-10-20 09:37:05

使用 jQuery:

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

尽管如果您可以将您的 asp 修改为仅在用户具有正确权限时输出行,那就更好了;)

With jQuery:

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

Although it would be much better if you could modify your asp to only output rows when the user has the right permissions ;)

回忆追雨的时光 2024-10-20 09:37:05

您应该编写一个合适的 HTML 帮助器。与 Web 表单相反,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>

You should write a proper HTML helper. Contrary to web forms, MVC gives you full control over the generated HTML and is totally inexcusable to use javascript to fix broken HTML.

So:

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;
}

and then:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文