0

I have a lightning component that dynamically creates a lightning:datatable when apex returns a list of records. For the most part its working, but im trying to add an onclick event to the row, but however I try it its failing.

$A.createComponent(
        "lightning:datatable",
        {
            "data":component.get("v.results"),
            "columns":component.get("v.mycolumns")
            //,"onclick": "{!c.onrowselection}"
            //,"onrowselection": "{!c.onrowselection}"

        },
        function(datatable){                
            if (component.isValid()) {
                var targetCmp = component.find('datatable');
                var body = targetCmp.get("v.body");
                body.push(datatable);
                targetCmp.set("v.body", body); 
            }
        }
    ); 

You can see the commented code, if i uncomment it the component will not be created. If you have an idea please post a comment or answer. Thank you.

2 Answers 2

2

If you read the component documentation for the Lightning:datatable you might notice that onclick is not a supported attribute.

When creating lightning components, if you declare unsupported attributes, they will not be created.

3
  • not only onclick is not supported, also any other attempts to use actions is not working now, although it will in the future - see the answer to my question Commented Nov 29, 2017 at 8:14
  • Thanks. I read the question and answers there. Can you please see my comment on the answer there.
    – user8310
    Commented Nov 29, 2017 at 8:22
  • So if I go back to using document.createElement('tr').. its working fine. if I add an inline function. please see my code, the first addeventlistner works, however if i try replacing it with the controller function it will not work(The page does load correctly though, just no click functionality). var tableRow = document.createElement('tr'); tableRow.addEventListener("click", function(){ alert('Clicked!'); }); var cmpRef = component.getReference("c.onrowselection"); tableRow.addEventListener("click", cmpRef);
    – user8310
    Commented Nov 29, 2017 at 8:36
1

use component.getReference to call the controller method while creating component dynamically,and as @glls says onclick is not supported attribute

$A.createComponent(
        "lightning:datatable",
        {
            "data":component.get("v.results"),
            "columns":component.get("v.mycolumns")
            "onrowselection": component.getReference("c.onrowselection")

        },
        function(datatable){                
            if (component.isValid()) {
                var targetCmp = component.find('datatable');
                var body = targetCmp.get("v.body");
                body.push(datatable);
                targetCmp.set("v.body", body); 
            }
        }
    );
2
  • Thanks. I just tried it but it wont work. I made sure to change "cmp " to "component". Any other ideas?
    – user8310
    Commented Nov 28, 2017 at 18:02
  • ya its "component".can you point out what error is showing?
    – vignesh
    Commented Nov 28, 2017 at 18:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .