Appframe Knowledge Base


1 hits
0

Show a link in a grid that triggers a Javascript function when clicked

// Attach to onFormatCellValue to produce an anchor element with a class
gridName.attachEvent("onFormatCellValue", function(pCell) {
    var vEscaped = af.common.stringifyValue(pCell.dataRow.Name);
	pCell.value = '<a class="myLink" href="#">' + vEscaped '</a>');
});

// You can abstract out the things you want to do when you click the link
// in a separate function that takes the arguments you need
function sayHi(pName) {
	alert('Hello there from row with name ' + pName + '!');
}

// Create a jQuery delegate/event on the root element of the grid
// and have the selector for the anchor element with that class
$(gridName.getElement()).on("click", "a.myLink", function(e) {
    // Go from the triggering element up to the nearest <tr>, and determine the index
    var vTr = $(this).closest("tr"),
        vDataIndex = vTr.data("index"),
        vDataObject = myGrid.getDataObject(),
        vRowData;
    
    // If you only need one field it is adviced to use getData(index, fieldName)
    sayHi(vDataObject.getData(vDataIndex, "Name"));
    
    // If you need many fields, you can exclude the fieldName parameter and get the whole row object
    vRowData = vDataObject.getData(vDataIndex);
    sayHi(vRowData.Name, vRowData.SomeOtherColumn);    
        
    return false; // Prevents browser from following the link
});

components web · Perma link post comment Posted by: Jarl Erik Schmidt (19-apr-2012)