1

The interface for my Lightning component is a very dynamic table that must be built according to data that is retrieved from a remote source. To create this table, inside of my helper class, I'm using the standard javascript functions (createElement(), appendChild(), etc.). I then look up my main div element (which was defined in the component markup in the usual way: ) and append the table to it via appendChild().

Inside the table, I am creating a link element with something like the following code:

var docTable = component.find("docTableCont").getElement(); // standard div devined in component markup
var aLink= document.createElement('a');
aLink.setAttribute("onclick", "alert('hello'); sforce.console.getEnclosingPrimaryTabId(function(result){ sforce.console.openSubtab(result.id, " + myCustomURL + ", true);}); return false;");
aLink.setAttribute("href", "#");
var txtLabel = document.createTextNode("View");
aLink.appendChild(txtLabel);
docTable.appendChild(aLink);

As you can see, I'm trying to open 'myCustomURL' in a new subtab. However, my onclick handler is never getting called -- also notice that I added an alert('hello') to the handler code so that I can know if the handler is being called -- no 'hello' alert is ever generated when clicking on the link.

I've done a good bit of searching and I see that I can add event handlers in my helper class but those handlers never get called either. I'm guessing that my problem is that my handlers are getting over-ridden by some low-level code in the framework. However, I can't find a better way to create dom elements in such a dynamic way. I also can't find a more documented method for opening a URL in a subtab than by using the sforce.console library.

5

I was able to get something to work by calling addEventlistener rather than setting an attribute. However, I wasn't able to add a regular Lightning controller function via component.getReference() - I'm going to investigate that further.

However, for now, this should work as long as the sforce.console is defined (I would have thought it wouldn't be available, but I haven't worked with the console and lightning, so I really can't speculate on this).

Here's what I got to work:

var docTable = component.find("docTableCont").getElement(); 
var aLink = document.createElement('a');

aLink.addEventListener("click", function(){alert("test");});
aLink.setAttribute("href", "#");

var txtLabel = document.createTextNode("View");
aLink.appendChild(txtLabel);

docTable.appendChild(aLink);
  • Ah. Good idea. I'll try it out and let you know how it works. Thanks. – Cory Newey Aug 22 '17 at 17:53
  • How did it go? Did it work? – Caspar Harmer Aug 23 '17 at 21:17
  • Sorry. It didn't really work for me. I'm trying to refactor the code to do things more as Salesforce wants things done: building the table with dynamic tags in the component markup. – Cory Newey Aug 24 '17 at 22:28
  • True. But did it specifically answer the question that you asked? - That is my primary consideration. ie, if you ran this code, would the alert fire? If so, then this answer is valid (and may be useful for others) – Caspar Harmer Aug 24 '17 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.