2

I have a hidden field with some values, I have to append these values in HTML table.The number of columns in table is fixed. I have appended successfully,but after first row,it should append data in new row,instead it is appending in the same row. This is how I am doing

$("#btntbl").click(function () {
                debugger
                var tabl = $("#testTable");
                var vals = $("#txthidden").val();
                for (var i = 0; i < vals.split(";").length; i++) {                       
                        for (var j = 0; j < vals.split(";")[i].split(",").length; j++) {
                        tabl.append("<td>" + vals.split(";")[i].split(",")[j] + "</td>");
                    }
                }
            });

Also note that some users dont have value of disabled column JS Fiddle How can I add new row each time clicking the button?

5 Answers 5

2

You need to split twice and create tr for each row:

$("#btntbl").click(function () {

  var tabl = $("#testTable"),
      vals = $("#txthidden").val(),
      rows = vals.split(';'),
      columns, i;

  for (i = 0; i < rows.length; i++) {
    columns = rows[i].split(',');
    tabl.append(
      '<tr>' + 
          '<td>' + columns[0] + '</td>' + 
          '<td>' + columns[1] + '</td>' + 
          '<td>' + (columns[2] || '') + '</td>' + 
      '</tr>'
    );
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" id="btntbl" value="Export to table">
<input type="hidden" value="User1,pwd1;User2,pwd2,disabled;User3,pwd3,disabled;User4,pwd4" id="txthidden" />
<table id="testTable" border="2">
    <thead valign="top">
        <tr>
            <th>User</th>
            <th>Password</th>
            <th>Disabled</th>
        </tr>
    </thead>
</table>

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

2 Comments

@ dsfq : Hi dont you think for templating ,we should use Angular js or Mustache , Handle bars instead of Speghetti jQuery code ?
@jQuery.PHP.Magento.com For more complex applications for sure. But in this case I' don't know OP's set up, so I can't introduce one more dependency without real need. For this simple example I would go with string concatenation.
1

Just change target by adding a row

Change

var tabl = $("#testTable");

To

var tabl = $('<tr>');
$("#testTable").append( tab1);

Comments

1

Here is how you can do it

var convertToTable = function (val) {
   val = val.split(';');
   val = val.map(function (v) {
      v = v.split(',');
      if (v.length === 2) v[v.length] = 'NA';
      return '<td>' + v.join('</td><td>') + '</td>';
   });
   val = '<tr>' + val.join('</tr><tr>') + '</tr>';
   return val;
}

and then

tabl.html(convertToTable(vals));

Demo here

Comments

1

jsFiddle demo

$("#btntbl").click(function () {                  
    var parts = $("#txthidden").val().split(";"), i=0;
    for (;i<parts.length;) {
        var j=0, tr="<tr>", subParts=parts[i++].split(",");          
        for (;j<3;)  tr += "<td>" + (subParts[j++]||"") +"</td>"; // concatenate
        $("#testTable").append( tr +"</tr>" );                    // Append once
    }
});

2 Comments

its working but its appending User1, Pwd1 then User2,pwd2 in same row, I need User1,PWd1 in first row,then User2,Pwd2 in second row
@SyedSalmanRazaZaidi edited. (moved "<tr>" stuff inside the for loop)
0

You forgot about TD tag, just use open tag before TD and close it in the end

Comments

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.