web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

How To Loop Rows (other than current row) in Subgrid using JS

(0) ShareShare
ReportReport
Posted on by 60

Hi, I am using JS in a Web Resource, and tying it to a Subgrid Control to be trigger upon onChange.

I am able to read the cell value which was triggered by the OnChange of the field.

Next, I would need to loop through the remaining rows in the subgrid and read each cell for every row.

May I know how can it be done?

Thank you.

Current code snippet:

function SetNA(context) {

    debugger;
    
    //id of the subgrid
    var id = formContext.data.entity.getId();

    // get the attribute which fired the onchange.
    var changedFirstNameAttr = context.getEventSource();

    // get the container for the attribute.
    var attrParent = changedFirstNameAttr.getParent();

    // var FirstName Field Attribute
    var changedFirstNameField = attrParent.attributes.get("firstName");

    // get the value of the changed first name value
    var changedFirstNameValue = changedFirstNameAttr.getValue();

    alert(changedFirstNameValue);

    if (changedFirstNameValue != null)
    {
        //loop through other rows in the subgrid, and read each cell value
    }
}

I have the same question (0)
  • David Jennaway Profile Picture
    14,065 on at

    You can access the rows via the getRows() method of the grid; this returns an array of GridRow objects

  • ruihaolrh Profile Picture
    60 on at

    Thank you David, I am implementing it. Is there a way to get the rowIndex of a subgrid.getSelectedRows(), assuming there is only 1 selected row, without going through a foreach?

  • cloflyMao Profile Picture
    25,210 on at

    Hi ruihaolrh,

    You can ignore any specific index row and get other remaining rows of subgrid with code below:

    (It will ignore the first record in subgrid.)

    var gridControl = formContext.getControl("Contacts").getGrid();
    var gridRows = gridControl.getRows();
    gridRows.forEach(function (row, i) {
        if (i !== 0) {
            var gridColumns = row.getData().getEntity().attributes;
            gridColumns.forEach(function (column, j) {
                console.log("Column: "   column.getName()   "   Value: "   column.getValue());
            });
        }
    });

    pastedimage1590136411202v2.png

    So if you could get the index number of onChange row, then it is easy to loop through the remaining rows in the subgrid.

    Regards,

    Clofly

  • cloflyMao Profile Picture
    25,210 on at

    Hi ruihaolrh,

    I think we couldn't get the row index from getSelectedRows() function.

    Because the function will only locate the selected row and its data collection, to get index of a selected row, it's still required to loop through all rows at least once.

    My suggestion is that you could get any value from getSelectedRows() function,

    e.g:

    Get any field value of selected row

    var fullname = formContext.getControl("Contacts").getGrid().getSelectedRows().getAll()[0.getAttribute("fullname").getValue();

    Ignore the row if its fullname attribute is equal to fullname of selected row.

    var gridControl = formContext.getControl("Contacts").getGrid();
    var gridRows = gridControl.getRows();
    gridRows.forEach(function (row, i) {
        var gridColumns = row.getData().getEntity().attributes;
        if (row.getAttribute("fullname").getValue() !== fullname) {
            gridColumns.forEach(function (column, j) {
                // perform your logic here
                console.log(column.getName()   " "   column.getValue());
            });
        }
    });
     

    Result:

    The selected row is not in final output:

    pastedimage1590140312957v1.png

    Regards,

    Clofly

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 61

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 52 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans