Search

Thursday, January 21, 2010

Default multi column sorting

Being able to sort by multiple columns in CRM is quite handy, but unfortunately there are no mechanisms for configuring a default multi-column sort. Until now. The following code will illustrate how to accomplish it:

For grid in main form(public view/associated view in main form)
var stageFrame = document.getElementsByName("stage")[0]; 
var stageFrameDoc = stageFrame.contentWindow.document; 

crmGrid = stageFrameDoc.all.crmGrid; 
crmGrid.all.divGridProps.children["sortColumns"].value = "fieldname:0;fieldname:1"; 
crmGrid.Refresh();

For grid in left nav(public view/associated view)
GetIframe("new_incident_new_entityname");

function GetIframe(loadAreaId) {
    var isNativeEntity = false;
    var navElement = document.getElementById('nav_' + loadAreaId);
    if (navElement == null) {
        navElement = document.getElementById('nav' + loadAreaId);
        isNativeEntity = true;
    }

    if (navElement != null) {
        navElement.onclick = function LoadAreaOverride() {
            // Call the original CRM method to launch the navigation link and create area iFrame 
            if (isNativeEntity) {
                loadArea('area' + loadAreaId);
                Iframe = document.getElementById('area' + loadAreaId + 'Frame');
                //put code to get gridobject here (grid from main form)
            }
            else {
                loadArea(loadAreaId);
                Iframe = document.getElementById(loadAreaId + 'Frame');
                //put code to get gridobject here (grid from left nav)

                function IframeReady() {
                    if (Iframe.readyState != 'complete') {
                        return;
                    }
                    var stageFrameDoc = Iframe.contentWindow.document;
                    crmGrid = stageFrameDoc.all.crmGrid;
                    crmGrid.all.divGridProps.children["sortColumns"].value = "fieldname>:0;fieldname>:0";
                    crmGrid.Refresh();
                }

                Iframe.onreadystatechange = IframeReady;
            }
        }
    }
}

Notes:
  1. Change the value for the "sortColumns" property to ":[; :]" where: is the name of the sorted column is 1 (descending) or 0 (ascending).
  2. Repeat the bracketed block as necessary, being sure to place a semicolon (;) between each field/sort pair.
There must be NO SPACES. Finally, call a Refresh() on the grid. The downside to this code, is that it does not update the sorting icon on the columns. There's a call being made somewhere, and I think it has to do with analyzing the keypress+click event, that updates the image... whereas my code does not follow that code path.



    No comments:

    Post a Comment