Search code examples
javascriptsharepoint-2013

How to remove headers on all occurrences of the same Web-part list


I have a SharePoint page where I display 15 different lists and there is one list called Links that I would like to remove the header from the first two of the three occurrences. The code I have only removes the first occurance of the list(s) that is passed in.

On the page, I have the following include. The code below only removes the header for the first occurrence for Links and Contacts.

<script type="text/javascript" data-lists="Links, Contacts, Links" src="../SiteAssets/js-enterprise/HideHeaders.js"></script>

My script is the following:

$(document).ready(function() {  

    // Get a list of views to turn off the headers
    var this_js_script = $('script[src*=HideHeaders]');
    var lists = this_js_script.attr('data-lists'); 

    var str_array = lists.split(',');

    for(var i = 0; i < str_array.length; i++) {
       // Trim the excess whitespace.
       str_array[i] = str_array[i].replace(/^\s*/, "").replace(/\s*$/, "");
       // Add additional code here, such as:
       //alert(str_array[i]);
       $("table[summary='"+str_array[i]+"'] tr:eq(0)").hide();
    }
});

Solution

  • Modify the JavaScript code as below.

    $(document).ready(function() {  
        // Get a list of views to turn off the headers
        var this_js_script = $('script[src*=HideHeaders]');
        var lists = this_js_script.attr('data-lists'); 
    
        var str_array = lists.split(',');
    
        for(var i = 0; i < str_array.length; i++) {
            // Trim the excess whitespace.
            str_array[i] = str_array[i].replace(/^\s*/, "").replace(/\s*$/, "");
            // Add additional code here, such as:
            //alert(str_array[i]);
            $("table[summary='"+str_array[i]+"']>thead").each(function(){
                $(this).hide();
            });
        }
    });