11/29/2014

Check if list is switched to quick edit mode

In the last project I was working on, I've stucked in the problem that SharePoint Client Side Object Model and javascript variables do not contain any information about the list mode: view or quick edit. And Google keeps silence about the way to get it.
So I've analyzed a mark-up and here are some ways to handle the switch to quick edit mode.

First thing that I've tried was jquery selectors to find the "edit" link. I don't like this method because you can't sure that nobody will add an element on the page that will have the same class, attribute or even id. But it the case this approach doesn't work anyway because there is no any specific class or attribute you can use to find the elements by selector correctly.
Then I've dug a little bit deeper. The onclick handler for the "edit" link looks like that:
onclick="EnsureScriptParams('inplview', 'InitGridFromView', '{5C96BEED-04A9-4A4B-AF91-FA3EEE1C2C76}'); return false;"
So maybe I can just change the EnsureScriptParams function to add some custom code (Thanks to Brendan Eich that we can do that in javascript). But unfortunately this function is used in many cases to ensure that some script is loaded and the function exists.
In case of "Edit" link it used to ensure inplview script and InitGridFromView function.
This is it! The solution is to change the InitGridFromView. This function will be called when any list on the page is switched to the quick edit mode.
So the result code is simple (one more thanks to Brendan):
//
// We have to be sure that the script is loaded
//
SP.SOD.executeFunc('inplview.js', 'InitGridFromView', function () { });
SP.SOD.executeOrDelayUntilScriptLoaded(function () {

    if (window.InitGridFromView) {
        var baseInitGridFromView = window.InitGridFromView;
        window.InitGridFromView = function (a, c) {
            baseInitGridFromView(a, c);
            console.log('hello from grid init');
        };
    }
}, 'inplview.js');
If you know any other and more simple way to track the mode change please leave a comment.
Have fun!

No comments:

Post a Comment