I'm trying to hide a column in List View Web Part using JS Link with Client Side Rendering. I'm able to color or make formatting changes to the column, but not able to hide it. Below is the code snippet that I have tried but it just does not displays the value rather than hiding the whole column. Here I am trying to hide column field2
var is_display;
(function() {
function registerMyListTemplate(){
var is_display = '';
var fieldContext = {};
fieldContext.Templates = {};
fieldContext.Templates.Fields = {
"field1":{
"View": changeView
},
"field2":{
"View": hideField
}
};
fieldContext.Templates.Item = renderListItemTemplate;
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(fieldContext);
}
ExecuteOrDelayUntilScriptLoaded(registerMyListTemplate, 'clienttemplates.js');
})();
function hideField(ctx){
var op = '';
if (is_display !== ""){
op = "";
}else{
op = '<div>' + ctx.CurrentItem.field2 + '</div>';
}
return op;
}
I also checked out a code which helps to hide the column in the PostRender function but it doesn't work if the List View Web Part has paging. It only works for the first page of the List View Web Part.
function postRenderHandler(){
["field2"].forEach(function(name) {
var header = document.querySelectorAll("[displayname=" + 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";
}
});
}