1

its possible to hide empty column(s) from view(webpart) in Sharepoint 2013? I was looking for any javasript solution, but unfortunately i have found only solution for boxed style. Hide columns from allitems.aspx view if column is empty (SharePoint 2013)

Is there any way for default style?

Thanks a lot. Dominik

4
  • The question you linked to hides an entire row if the column value is empty for that row. You want to hide an entire column if there are no values any of the rows?
    – Erin L
    May 2, 2016 at 21:26
  • Do you mean hide the entire column? Because if one item has value, that would shift the rest of the item's columns out of align with the header.
    – Mike
    May 2, 2016 at 21:38
  • Yes, I want to hide whole column, when this column is empty. May 2, 2016 at 22:01
  • Its because Im using diferent columns for Folders and Files in Library. I want to hide columns which are not used inside folders. May 2, 2016 at 22:02

4 Answers 4

2

What you want to achieve can be done using OOTB SharePoint functionality. Have a look at "Folders" section when creating a view. This allows you to create views for your folders (not including the empty columns) and for the files within the folder (showing all columns). Nice blog about this option: https://camerondwyer.wordpress.com/2014/01/06/harness-sharepoint-default-views-at-different-navigation-hierarchy-levels-to-build-more-appealing-solutions/#folder

2
  • Thanks. This is great idea for me, but is it appliacable when im using library in webpart? May 3, 2016 at 12:19
  • Sorry, Everythnig is allright when i have refreshed my webpart. Thanks All May 3, 2016 at 12:22
1

if you need javascript solution you can try this:

$('a[name^="SPBookmark_{colname}"]').parent().parent().parent().hide();

Replace {colname} with desired column name which you want to hide.

0

You can use ctx.ListData.Row to loop the rows, either in a CSR OnPostRender or any script after the View has loaded.

Thing is,

If you hide empty TD cells, the column header will still be there, messing up the layout.

So you need to hide the TH column header as well.

    var fieldname = 'DueDate' ,
        label     = document.querySelector("a[id$='" + fieldname + "']") ,
        colnr     = GetAncestor( label , 'TH' ).cellIndex + 1 ,
        selector  = String.format("table TH:nth-child({0}),table TD:nth-child({0})", colnr);
        colcells  = document.querySelectorAll( selector ); 

    colcells.forEach(function( cell ){
        cell.style.backgroundColor = 'pink';    
    });

I am sorry, I could not find any reason to load an extra 90KB jQuery library and use $

0

I found this code that works great for lists set to Basic Table style

$(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 way the column appears if data is found and hidden if there isn't. Not sure if this will help you.

Downside is that if you group items in the view the code stops working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.