0

Background

I am trying to generate a variable HTML table for use with DataTables.

I've built a web viewer with a couple of selectboxes which are dependable on each other. When you select an option in "Select tabel", "Select column" changes. When you alter "Select column", "Select attribute" changes. Etc.

The results from "Select attribute" are placed in an array, which I want to use to generate the tableheaders that DataTables need.

Example:

Say myArray looks something like ["aaa", "bbb", "ccc", "ddd", "eee"], the generated report should look something like the code below.

Question

Because myArray has no fixed size and content, I can't just make a static HTML table in my index file.

How do I make a function that loops through myArray and generates a report based on the data in myArray?

Edit:

I've got my code working in JSfiddle now, however when I try to incorporate it in my web viewer code, I get an error: enter image description here The report is messed up and looks like this:

enter image description here

What causes this and how do I get my code to work outside of JSfiddle?

Code

<div id="report_comes_here">
  <table id="report">
      <thead>
        <tr>
            <th>aaa</th>
            <th>bbb</th>
            <th>ccc</th>
            <th>ddd</th>       
            <th>eee</th>
        </tr>
      </thead>
    </table>
</div>
3
  • Are you willing to use table headers ? Commented Jun 1, 2016 at 10:17
  • I think that what you're looking for is datatables.net/reference/api/destroy(). This will allow you to update a table that has already been made into a datatable. @Ramkee's answer is grand but won't allow you to edit it easily after the table has been initialised once - not with different columns anyway. Commented Jun 1, 2016 at 11:38
  • I already use something like that to destroy and rebuild my current (static) report. Now I just need to make it dynamic. Commented Jun 2, 2016 at 9:26

1 Answer 1

2
var array = ["aaa","bbb","ccc","ddd"] ;
var count = array.length;

var TABLE = ' <table id="report" class= "table table-striped table-bordered">';
    TABLE += '<thead ><tr><th>Sample Head</th></tr></thead>'
    TABLE += '<tbody>';
    for (i = 0; i < count; i++) {
        TABLE += "<tr>";
        TABLE += "<td>" + array[i] + "</td>"
        TABLE += "</tr>";
    }
    TABLE += "</tbody></table>";

Now you can append TABLE variable to your required DIV ID using Jquery

$("#report_comes_here").html(TABLE);
$('#report').DataTable();

You can find Demo at JSfiddle

If you find any difficulties please let me know.

Sign up to request clarification or add additional context in comments.

1 Comment

I should have been a bit more clear. I wanted table headers, not table rows/data. I've edited your code a bit to generate what I meant. However, I can't get the code to work outside of JSfiddle, see my edited question.

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.