1

I have 3 list webparts on my home page. I've used this script to hide 2 columns on page load and changes (sorting, filtering).

// Override list views to hide language columns based on locale
listOverride: {
    set: function () {
        var overrideCurrentContext = {};
        overrideCurrentContext.Templates = {};


        overrideCurrentContext.OnPostRender = listOverride.hideLanguageColumns;
        SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCurrentContext);
    },
    hideLanguageColumns: function () {
        // Loop through columns which need to be hidden and hide from view
        cca.locale.hiddenLangColumns.map(function (column) {
            // Get column position
            var columnIndex = $("tr.ms-viewheadertr th:contains('" + column + "')").first().index() + 1;
            // Find and Hide column
            $('th:nth-child(' + columnIndex + '),td:nth-child(' + columnIndex + ')').hide();
        })
        console.log("list post render done")
    }
},

I'm experiencing two problems:

  1. The CSR do not do anything on page load. I need to call listOverride.hideLanguageColumns explicitly on page load for it to take effect.
  2. When I upload documents through drag n drop, the row Cells for the columns I've hidden are displayed again for the new item, but the columns are still hidden so I've got a table now with less columns then row cells.

I'm a bit stuck on both issues.

  • If you don't want some columns in the view, then you could just change the view in SharePoint & hide those columns using modify view button. Then add that view as webpart (sorry if i am wrong). – Vishnu S May 8 '17 at 7:06
  • @Batman Why do you want to hide columns dynamically as there is a way to create views OOTB in SharePoint?Do you want to hide columns in all 3 views? – SynozeN Technologies May 8 '17 at 7:25
  • I want to hide the columns in all three webpart views. I need to manage the columns shown on the page dynamically based on various conditions. So the modify view isn't helpful here. – Batman May 8 '17 at 7:26
0

1. You are missing half the boilerplate CSR code,

always use the Chrome Browser & the Cisar Extension to WYSIWYG edit CSR code

2. Your CSR does not execute for the new Row

Rethink how to solve this. You are now trying to update the UI with JavaScript, which is a fine approach if you have to do one update.

CSS is the Browser technology that always updates the UI

So instead of deleting/hiding the columns with JavaScript

Use CSS to hide TDs and THs with CSS

You can add <STYLE> tags with JavaScript

  • Hi Danny, I don't see what I'm missing, it executes on sorting/filtering so I'm not sure why it would't on page load or what I could add that would make it do that. Thanks for the extension, I'll check it out. 2. It doesn't execute on new Row, but I couldn't find examples or documentation on an event I could register on new row so I thought OnPostRender would take care of that. – Batman May 8 '17 at 7:32
  • 1) Then you are not comparing your code with a correct CSR Template that also deals with the MDS feature (note that most blogs are incomplete, Cisar gives you the correct boilerplate. 2) That is why I suggest to do it with CSS, as that is automatically applied to all new HTML being added to a page.. yes you can program that behavior with JS.. but why write 10 lines of code if the Browser does it for you? – Danny '365CSI' Engelman May 8 '17 at 8:26
  • 1) I see, but I don't have an MDS issue, it's disabled and the code runs on sorting/filtering. That's why I'm a little confused as to why that could be the issue. 2) I don't see how adding CSS compared to calling .hide() would be different here. Are you saying instead of doing .hide() replace it with .addClass("ms-hide")? – Batman May 8 '17 at 14:27
  • I don't see how what I'm doing is different than what's in this sample: code.msdn.microsoft.com/office/… – Batman May 8 '17 at 16:24
  • I said: Most blogs are incorrect. So is that code posted on MSDN.. it is incomplete to work with the MDS feature. The Cisar extension gives you the correct boilerplate code – Danny '365CSI' Engelman May 8 '17 at 16:40
0

Well, this got bumped to the top by Community, so I guess I'll throw my two cents in here...

Now, this is completely untested, but since I have found that you can add fake columns that aren't actually present in the list to a view by altering the ctx.ListSchema.Field collection in onPreRender, I'm guessing that the reverse might be possible - in the onPreRender stage, find the column you want to hide in the ctx.ListSchema.Field collection, and just splice it out.

Then the CSR engine won't render it, because it won't know about it.

Your Answer

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

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