2

I want to create a dynamic table in jQuery. But my code creates a table for each of my element.

var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
var str = dataItem.Description;
var spl = str.split(','), part;
var spl2 = "";
for (var i = 0; i < spl.length; i++) {
    part = spl[i].split(':');
    content = "<table>" + "<tr><td>" + part[0] + "</td>" + "<td>" + part[1] + "</td></tr>";
    spl2 += content;
}
content += "</table>"
var desc = "<div id='openModal' class='modalDialog'>" + "<span>" +
    spl2
    + "</span><br/>" +
    +  "</div>";

Which part of my code is wrong?

7
  • its because you are creating your table tag inside the for loop. put the `<table>' tag before the loop like you are closing the table tag Commented Apr 25, 2016 at 7:04
  • @AnoopLL I tried this code before,but my code is like this <table> <tbody> <tr> <td>{"Id"</td> <td>1</td> </tr> </tbody> </table> <table> <tbody> <tr> <td>{"Id"</td> <td>1</td> </tr> <tr> <td>"FirstName"</td> <td>null</td> </tr> </tbody> </table> Commented Apr 25, 2016 at 7:13
  • you got the solution? Commented Apr 25, 2016 at 7:23
  • @AnoopLL No.I have 13 parameters.first this code creates a table with the firs parameter,second time it creates a table with my first parameter+second parameter.at the end I have 13 table that my 13 table contains all the parameter Commented Apr 25, 2016 at 7:28
  • @balouchi..just write spl2 = content; instead of spl2 += content; in for loop. as i mentioned below in answer. it will solve your problem Commented Apr 25, 2016 at 7:31

2 Answers 2

1

as you were adding table in for loop, so it was creating table for each item.Try this:

var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
var str = dataItem.Description;
var spl = str.split(','), part;
var spl2 = "";
content ="<table>"
 for (var i = 0; i < spl.length; i++) {
     part = spl[i].split(':');
     content += "<tr><td>" + part[0] + "</td>" + "<td>" + part[1] + "</td></tr>";
     spl2 = content;
 }
content += "</table>"

// you can also assign spl2 = content; over here.
    var desc = "<div id='openModal' class='modalDialog'>" + "<span>" +
        spl2
        + "</span><br/>" +
        +  "</div>";
Sign up to request clarification or add additional context in comments.

Comments

0
    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
var str = dataItem.Description;
var spl = str.split(','), part;
var spl2 = "";
var content = "<table>";
 for (var i = 0; i < spl.length; i++) {
     part = spl[i].split(':');
     content += "<tr><td>" + part[0] + "</td>" + "<td>" + part[1] + "</td></tr>";
     spl2 += content;
 }
content += "</table>"
var desc = "<div id='openModal' class='modalDialog'>" + "<span>" +
    spl2
    + "</span><br/>" +
    +  "</div>";

Updated your script

1 Comment

check it as the <table> tag is outside the for loop this should not happen ,can you check again with the above code

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.