1

I have a table with several columns, not all columns contain data. So I've been using this script to hide them:

$(document).ready(function () {
    $('table.ms-listviewtable').each(function(a, tbl) {
        var currentTableRows = $(tbl).find('tbody tr').length;
        $(tbl).find('th').each(function(i) {
            var remove = 0;
            var currentTable = $(this).parents('table.ms-listviewtable');

            var tds = currentTable.find('tr td:nth-child(' + (i + 1) + ')');
            tds.each(function(j) { if ($(this).text().trim() === '') remove++; });

            if (remove == currentTableRows) {
                $(this).hide();
                tds.hide();
            }
        });
    });
});

This works great for a basic list view table. However a new requirement is asking me to group based on one of the columns and once I do that the code stops working. I'm assuming it's because SharePoint put in that extra collapsible row when you group items. Example:

    <tbody id="titl48-1_" groupstring="%3B%232019%3B%23"><tr><td class="ms-gb" nowrap="nowrap" colspan="100">
    <a onclick="javascript:ExpCollGroup('48-1_', 'img_48-1_',event, false);return false;" href="javascript:">
    <span class="ms-commentcollapse-iconouter">
    <img class="ms-commentcollapse-icon" id="img_48-1_" alt="collapse" src="/_layouts/15/images/spcommon.png?rev=23"></span>Year</a> : <span>2019</span>
  <span style="font-weight: lighter; display: inline-block;">(5)</span></td></tr></tbody>

Could anyone help me fix the JS to ignore that new row?

  • Is deleting the empty column not an option? Otherwise you could use a filter to filter out any blanks. – Tally May 16 at 13:34
  • No, this list is for tracking purposes, with one view based on who's looking at it. Because of this the column data will change based on the person and what they enter. Creating view's for each person that may change is not time feasible. – Kyler Hunter May 16 at 14:00
  • hmm, then I don't think configuring Audiences would help either... – Tally May 16 at 14:08
  • No, I think what needs to happen is the code needs to ignore any tbody>tr>td with the class that contains "ms-gb". But I don't know how to write it or if it's even possible. – Kyler Hunter May 16 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.