1

Hiding columns in normal view is ok using either of the functions:

function hideColumn()
{ 
var header = document.querySelectorAll("[displayname="Column Name"]")[0].parentNode;
var index = [].slice.call(header.parentNode.children).indexOf(header) + 1;
header.style.display = "none";
for (var i = 0, cells = document.querySelectorAll("td:nth-child(" + index + ")"); i < cells.length; i++) {
    cells[i].style.display = "none";}

}
// _spBodyOnLoadFunctionNames.push('hideColumn');

or

function hideColumns()
{ 
["col1", "col2","col3"].forEach(function(col) {
var header = document.querySelectorAll("[displayname='"+ col +"']")[0].parentNode;
var index = [].slice.call(header.parentNode.children).indexOf(header) + 1;
header.style.display = "none";
for (var i = 0, cells = document.querySelectorAll("td:nth-child(" + index + ")"); i < cells.length; i++) {
    cells[i].style.display = "none";}
});
}
//  _spBodyOnLoadFunctionNames.push('hideColumns');

but when I click on Edit, the view switches to a spreadsheet/edit view with all columns.

How to keep the columns hidden in a spreadsheet view?

2
  • 1
    If you want to hide the columns from view then why don't you remove those columns from view settings... Or is there anything else is related to this like permissions or something else? Commented Dec 19, 2018 at 2:26
  • It is down to permissions or who should see what. I have several pages with several different views on one page, but this makes all pages too big so I just want one view and switch between different columns using buttons.
    – Maciek
    Commented Dec 20, 2018 at 14:15

1 Answer 1

1

For Edit view try this:

function hideColumn()
{ 
    var header = document.querySelectorAll("th[title='Column Name']")[0];
    var index = [].slice.call(header.parentNode.children).indexOf(header) + 1;
    header.style.display = "none";
    for (var i = 0, cells = document.querySelectorAll("td:nth-child(" + index + ")"); i < cells.length; i++) {
        cells[i].style.display = "none";}
}
2
  • Thanks @Slaven Semper, but it doesn't seem to be doing anything :( I've tried with the column name and the internal name.
    – Maciek
    Commented Dec 20, 2018 at 15:16
  • @Maciek I've tested this on SP 2013 (15.0.5085.1000) and it works fine. What version are you running? Are you able to debug it? Commented Dec 30, 2018 at 5:32

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.