CRM 2011: busque la cuadrícula actual en JavaScript de cinta

This one is driving me mad....

I have several subgrids in a form, all of the same entity and filtered to different fields. A button in the grid's ribbon calls a function that needs to know the exact grid "instance" to make a decision - and this is where I am completely lost.

In my ribbon definition, I pass the CrmParameter SelectedControl to the called function. This is supposed to be the active grid - and it apparently is, in a way at least.

What I would like to do in my JavaScript code is get one of my grids via its name and then compare it to the object that's passed into my function - only that this is some completely different object, and you can do next to nothing with it. You can't get its name, ID, label whatever; of all the methods listed for controls in the SDK, only four work: getVisible(), setVisible(), setFocus() and refresh(). These are not very useful for what I need to do.

A colleague then told me to try Xrm.Page.ui.getCurrentControl() - but that shows the exact same behavior, although funnily the two objects aren't even equal.

I found something vague through Google that used the .control property of a control retrieved via .getControl(), so I compared the one obtained via .getCurrentControl() to that - and they matched. Unfortunately, the "current control" matches the .control properties of todos subgrids.

Some code to make it clearer what is what:

function ribbonAction(param) // param is the SelectedControl parameter
{
    var current = Xrm.Page.ui.getCurrentControl();
    var grid1 = Xrm.Page.getControl("grid1");
    var grid2 = Xrm.Page.getControl("grid2");

    alert(param == current);            // false
    alert(param == grid1);              // false
    alert(param == grid1.control);      // false
    alert(current == grid1);            // false
    alert(current == grid1.control);    // true
    alert(current == grid2.control);    // true

    alert(current.getName());           // throws an error
    alert(param.getName());             // throws an error

    current.refresh();                  // refreshes the correct grid
    param.refresh();                    // refreshes the correct grid
}

The comments tell what happens when the ribbon button is clicked while grid1 is active.

I would be very grateful for any hints on how to really identify the active subgrid control in that situation.

preguntado el 09 de marzo de 12 a las 14:03

Is that normal that you set grid2 with a control called "grid1"? That would explain what it also returns true for the 6th expression. -

No, that just came from copying that line; this is only example code to sum up all my observations, my real controls have distinct names not as easily mixed up. Thank you for pointing out that typo. -

1 Respuestas

This was driving me mad too! Here is how I cracked it:

Specify a global variable in a JavaScript library for the entity form in which your sub grids reside.

var SelectedSubGrid;

In the same library add a function called TagGrid as follows....

function TagGrid(SubGridName)
{
            if(document.getElementById(SubGridName + "_d") != null)
            {
                        document.getElementById(SubGridName + "_d").onclick = function () { SelectedSubGrid = SubGridName };
            }
}

Then add a second function called GetCurrentGrid as follows....

function GetCurrentGrid()
{
            // add code here to check current grid and execute differently as required...
            alert('Current Grid is ' + SelectedSubGrid);          
}

Include a call to the TagGrid function for each subgrid of the same entity type. In my example below I have two sub grids of custom entity type 'Business Pitch Event', one grid named 'DocumentationEvents' and the other 'BusinessPitchEventEvents'.

function OnLoad()
{
            TagGrid("DocumentationEvents");
            TagGrid("BusinessPitchEventEvents");
}

Then on your sub grid ribbon button, call the GetCurrentGrid function!

respondido 02 nov., 12:13

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.