Using jQuery to remove empty rows from HTML tables

I have an HTML table:

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

Some cells in the table contain only rows, depending on the specific permissions. For example, here is one line that only places the 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 can I go back and delete all completely empty rows in this SPECIFIC table after loading the page using jQuery, so instead of seeing a small small empty row, it just does not exist.

+3
source share
2 answers

Using jQuery:

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

Although it would be much better if you could change your asp to only output strings when the user has access rights;)

+10

HTML. -, 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>
0

Source: https://habr.com/ru/post/1786942/


All Articles