1

I have an array of javascript objects, that I'm using to dynamically populate an html table. I iterate through each object and all is good. However, one of the keys has a value of true/false, and I need to replace the value with a checkbox for each object in the array. How can I do that?

The checkbox needs to be ticked for false and not ticked for true, also the checkboxes need to be disabled, as we don't want a user interaction.

// Draw table from 'products' array of objects 
function drawTable(tbody) {
  var tr, td;
  tbody = document.getElementById(tbody);
  for (var i = 0; i < products.length; i++) // loop through data source
  {
    tr = tbody.insertRow(tbody.rows.length);
    td = tr.insertCell(tr.cells.length);
    td.innerHTML = products[i].ProductName;
    td = tr.insertCell(tr.cells.length);
    td.innerHTML = products[i].UnitsInStock;
    td = tr.insertCell(tr.cells.length);
    td.innerHTML = products[i].UnitPrice;
    td = tr.insertCell(tr.cells.length);
    td.innerHTML = products[i].Discontinued;
    td = tr.insertCell(tr.cells.length);
    td.innerHTML = document.createElement('button');
  }
}

drawTable('table-data')
<h2>Products</h2>

<div class="table-wrapper">
  <table id="display-table" class="fl-table">
    <thead>
      <tr>
        <th>Product Name</th>
        <th>Units In Stock</th>
        <th>Unit Price</th>
        <th>Discontinued</th>
      </tr>
    </thead>
    <tbody id="table-data">

    </tbody>
  </table>
</div>

3 Answers 3

1

Here is how to add a checkbox column based off a field called 'checkbox' that is in your array and is true/false. I'm just matching your code here so it's easy to follow:

    td = tr.insertCell(tr.cells.length);
    checkbox = document.createElement('input');
    checkbox.setAttribute("type", "checkbox");
    checkbox.disabled = true;
    checkbox.checked = products[i].checkbox;
    td.appendChild(checkbox);
Sign up to request clarification or add additional context in comments.

1 Comment

This worked out great, thanks! The only thing that I changed though is to replace products[i].checkbox; with products[i].Discontinued;
0

You can use innerHTML. If the value is true display <input type="checkbox" checked /> otherwise if it is false remove "checked" from the tag.

You also may want to attach an onclick function if the checkbox will be clickable so that the value updates.

Comments

0

What you need to do is check what the value of Discontinued for each product like so:

if(products[i].Discontinued = true) {
  td.innerHTML = "<input type='checkbox' checked />";
} else {
  td.innerHTML = "<input type='checkbox' />";
}

This will create a checkbox no matter what, and tick it if Discontinued is true. You can check the snippet below to see it in action.

// Draw table from 'products' array of objects 
function drawTable(tbody) {
  var tr, td;
  var product = new Product("Product 1", 2, 10, true);
  var products = [new Product("Product 1", 2, 10, true), new Product("Product 2", 30, 50, false)];
  tbody = document.getElementById(tbody);
  for (var i = 0; i < products.length; i++) // loop through data source
  {
    tr = tbody.insertRow(tbody.rows.length);
    td = tr.insertCell(tr.cells.length);
    td.innerHTML = products[i].ProductName;
    td = tr.insertCell(tr.cells.length);
    td.innerHTML = products[i].UnitsInStock;
    td = tr.insertCell(tr.cells.length);
    td.innerHTML = products[i].UnitPrice;
    td = tr.insertCell(tr.cells.length);
    if (products[i].Discontinued = true) {
      td.innerHTML = "<input type='checkbox' checked />";
    } else {
      td.innerHTML = "<input type='checkbox' />";
    }

    td = tr.insertCell(tr.cells.length);
    td.innerHTML = document.createElement('button');
  }
}

drawTable('table-data')

function Product(name, stock, price, discontinued) {
  this.ProductName = name;
  this.UnitsInStock = stock;
  this.UnitPrice = price;
  this.Discontinued = discontinued;
}
<h2>Products</h2>

<div class="table-wrapper">
  <table id="display-table" class="fl-table">
    <thead>
      <tr>
        <th>Product Name</th>
        <th>Units In Stock</th>
        <th>Unit Price</th>
        <th>Discontinued</th>
      </tr>
    </thead>
    <tbody id="table-data">

    </tbody>
  </table>
</div>

Note: I added some code to create two products for this example, don't include it in your project.

1 Comment

Thanks! I tested out your solution and it did added the checkboxes, however all of them were checked, regardless of the true/false value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.