I have a custom component which by the means of a button add a row to my table. Now my needs are to add a component to each entry (tr) of the table.

This is my controller:

        var tBody = document.getElementById("tableQuoteLinesBody");

    var row = document.createElement("tr");

    var detail = document.createElement("td");
    detail.appendChild(document.createTextNode("RFQ detail"));

    var product = document.createElement("td");

    var scale = document.createElement("td");

    var dDelete = document.createElement("td");


            "placeholder":"Select an option",
            "errorMessage":"Invalid input"
            var bdy = cmp.get("v.body");

While this is my component:

    <table id="tableQuoteLines">
                RFQ Detail name
    <tbody id="tableQuoteLinesBody">

<lightning:button variant="brand" label="Add Product" title="Add Product" onclick="{! c.addProduct }" />

now obviously the v.body should be placed inside the tr, namely I would need 3 facet for each rows.

I have no idea how to programmatically add and remove facets or if this is the best way of doing this operation.

Any suggestion?


This is not the "Lightning" way. The only time you should be using document methods at all is when you're doing fancy stuff in a renderer. The standard method is to use automatic rendering. That's really simple:

<tbody id="tableQuoteLinesBody">
  <aura:iteration items="{!v.quotelines}" var="line">

To add a new row, it's now incredibly simple:

addProduct: function(component, event, helper) {
  var lines = component.get("v.quotelines");
  lines.push({ rfqDetail: "", product: "", scale: "", deleteItem: "" });
  component.set("v.quotelines", lines);

Obviously, this is simplified to get you started, but this is the standard Lightning design pattern. Note that you don't do any rendering of your own, you simply set the data attributes, and Lightning does all the heavy lifting for you.

  • Ok this seems much more clear! Nor my problem is that I get undefined when I do component.get("v.quotelines"), thus I cannot push new lines. Any idea? Anyway Thanks for now!! – Zeno Trevisan Nov 20 '18 at 8:58
  • @ZenoTrevisan You need to have a value to begin with. I usually start off like this: <aura:attribute name="quotelines" type="List" default="[]" /> so that my attribute will have a default value (an empty list). – sfdcfox Nov 20 '18 at 14:04

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.