Skip to content

Instantly share code, notes, and snippets.

@cgillis-aras
Created May 8, 2019 15:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save cgillis-aras/c7bb77b297a772bc71b42607beca5deb to your computer and use it in GitHub Desktop.
Sample code to set the color of a relationship row based on property values.
// We want to style the rows of some of our relationships differently based on property value
// We're putting this in a Builder Method of a CommandBarSection so that our method is called each time the user
// clicks on a new tab.
var relationshipWeCareAbout = "159C6D88795B4A86864420863466F728"; // Put ID of relationship here
var currentRelationship = parent.relationships.currTabID;
// If we show any other relationship, don't do any additional processing
if (currentRelationship !== relationshipWeCareAbout)
{
return;
}
colorRelRows = function()
{
// Check if the relationship grid is loaded
if (!isRelGridReady()) {
setTimeout(colorRelRows, 50);
return;
}
var relFrame = parent.frames[currentRelationship];
var relGrid = relFrame.contentWindow.grid;
// Check that all items are loaded into the grid
var relationshipName = aras.getRelationshipTypeName(currentRelationship);
var parentRelItems = aras.getRelationships(parent.item, relationshipName);
var parentRelationshipCount = parentRelItems.length;
if (!areAllItemsLoaded(relGrid, parentRelationshipCount)) {
setTimeout(colorRelRows, 50);
return;
}
// Update the rows to set the background color
for (var i = 0; i < parentRelationshipCount; i++)
{
var relationshipItem = parentRelItems[i];
if (rowShouldBeColored(relationshipItem))
{
setRowBgColor(relGrid, relationshipItem.id, "red");
}
}
}
isRelGridReady = function()
{
var relFrame = parent.frames[currentRelationship];
var isReady = Boolean(!!relFrame &&
!!relFrame.contentWindow &&
relFrame.contentWindow.gridReady);
return isReady;
}
areAllItemsLoaded = function(relGrid, parentRelationshipCount)
{
var relationshipName = aras.getRelationshipTypeName(currentRelationship);
// This should probably be "relGrid.GetAllItemIds()", but we're trying to get the count of the relationships to compare them.
// items_Experimental.getAllId() returns an array. GetAllItemIds() calls this function and then does a join. To get the count
// of the relationships we would have to do a split after that join which seems wasteful when we can just get the array directly.
// If we were aiming for maintainability, we would want to use relGrid.GetAllItemIds() in case the implementation changes.
var gridRelItemIds = relGrid.items_Experimental.getAllId();
var itemsLoaded = Boolean(parentRelationshipCount === gridRelItemIds.length);
return itemsLoaded;
}
// Put the logic to determine which rows should change color here
rowShouldBeColored = function(relItem)
{
var relatedItem = aras.getRelatedItem(relItem);
var relatedItemState = aras.getItemProperty(relatedItem, "state");
return Boolean(relatedItemState === "Preliminary");
}
// To set the background color of a row, we need to set the background color of all of it's cells individually
setRowBgColor = function(relGrid, itemId, bgColor)
{
var rowIndex = relGrid.getRowIndex(itemId);
var cellCount = relGrid.GetColumnCount();
for (var i = 0; i < cellCount; i++)
{
var rowCell = relGrid.Cells(itemId, i);
if (rowCell)
{
rowCell.SetBgColor(bgColor);
}
}
}
setTimeout(colorRelRows, 50);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment