7

Using a row variable to get data dynamically and set the table row.

var row = '<tr>'+
      '<td>'+obj.Message+'</td>'+
      '<td>'+obj.Error+'</td>'+
      '<td>'+obj.Detail+'</td>'+
      '</tr>';

Based a value I tried to add another <td> to the row object but it didn't work.

if(obj.Type=='Error') {
  $(row).find('td:last').append('<td>'+ obj.ErrorCode+'</td>');
}
$('table> tbody:last').append(row);

First I tried to just append the <td> to row but that didn't work as well.

$(row).append('<td>'+ obj.ErrorCode+'</td>');
2
  • 2
    It is == if(obj.Type=='Error') { ... not = Commented Oct 31, 2013 at 15:04
  • 2
    It's worth mentioning to him that that wouldn't stop his code trying to add the cell. It would make it try to add the cell every time, regardless of obj.Type. Commented Oct 31, 2013 at 15:09

6 Answers 6

10

You're not storing the last row.

When you call $(row).find(...).append(...) the result is not being stored in a variable. The best solution is probably keeping a jQuery object from the start:

//         v--- use a jQuery object here
var $row = $('<tr>'+
      '<td>'+obj.Message+'</td>'+
      '<td>'+obj.Error+'</td>'+
      '<td>'+obj.Detail+'</td>'+
      '</tr>');    
if(obj.Type=='Error') {
    $row.append('<td>'+ obj.ErrorCode+'</td>');
}
$('table> tbody:last').append($row);

See fiddle

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

1 Comment

@HimanshuYadav I made a typo which has been corrected (line 1).
4

Instead of append use after and use === (or ==) instead of =

  if(obj.Type === 'Error') {
    $(row).find('td:last').after('<td>'+ obj.ErrorCode+'</td>'); //This will add the new td after the last one
   ..

or just append it to the row:

   $(row).append('<td>'+ obj.ErrorCode +'</td>'); //this will do the same thing, less code and append this as last td of your row.

Comments

0

Your appending INSIDE a td, you want .after

if(obj.Type=='Error') {
    $(row).find('td:last').after('<td>'+ obj.ErrorCode+'</td>');
}

And the whole, == over = is an issue as well.

Comments

0

You have done mistake here:

 obj.Type='Error' 

(=)act as assignment operator here.

Check with

  if(obj.Type=='Error'){
   //hence code.
  }

2 Comments

That was a typo. I have fixed it.
So you can accept answer from above which you feel appropriate :)
0

use == comparision operator for comparision: To insert new cell use .after() or you can just append whole row using .append()

if(obj.Type=='Error') {
   $(row).append('<td>'+ obj.ErrorCode+'</td>');
}

Comments

0

Don't you have to add row to the DOM before you can search it and add another <td>?

Solution 1

var row = '<tr>'+
          '<td>'+obj.Message+'</td>'+
          '<td>'+obj.Error+'</td>'+
          '<td>'+obj.Detail+'</td>';

if(obj.Type=='Error') {
    row += '<td>'+ obj.ErrorCode+'</td>';
}

row += '</tr>';

$('table> tbody:last').append(row);

Solution 2

var row = '<tr>'+
          '<td>'+obj.Message+'</td>'+
          '<td>'+obj.Error+'</td>'+
          '<td>'+obj.Detail+'</td>'+
          '</tr>';

$('table').append(row);

if(obj.Type=='Error') {
    $('table').find('td:last').after('<td>'+ obj.ErrorCode+'</td>');
}

Solution 3

var row = '<tr>'+
          '<td>'+obj.Message+'</td>'+
          '<td>'+obj.Error+'</td>'+
          '<td>'+obj.Detail+'</td>'+
          '</tr>';

$('table').append(row);

if(obj.Type=='Error') {
    $('table tr').append('<td>'+ obj.ErrorCode+'</td>');
}

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.