Gaurav
Gaurav

Reputation: 33

Iterating Ajax response and creating Dynamic table on some condition

I am trying to create a dynamic table with ajax response. And want to put condition on check box like if item.statuscheck =1 will print checked Box with name and if item.statuscheck =0 unchecked box will display with same name. i am able to display name dynamically. But problem is coming with check box. Here is the code i am trying with. Please help me on this issue.

  success(response)
{
    $.each(response,function(index,item){
        var status;
        if(index >=0 and index <= 4)
    {
        if(item.statusCheck = 1)
        {
            status = <input type ="checked" checked/>
        }
        else{
            status =  <input type="checked" />
        }   
        var content = '<td>'+ status + '</td>'
                 +<td>' + item.fieldName +'<td>';
            $("#tableId").appent(content)
    }

  }
}

Upvotes: 0

Views: 93

Answers (4)

Jack jdeoel
Jack jdeoel

Reputation: 4584

compare operator is == not = assign.Also careful 'and "

 success(response)
{
    $.each(response,function(index,item){
        var status;
        if(0 >= index <= 4)
    {
        if(item.statusCheck == 1)
        {
            status = '<input type ="checkbox" checked/>';
        }
        else{
            status =  '<input type="checkbox" />';
        }   
        var content = '<td>'+ status + '</td>'
                 +'<td>' + item.fieldName +'<td>';
            $("#tableId").append(content);
    }

  }
}

Upvotes: 1

Manish
Manish

Reputation: 247

Its not type = "checked" its type = "checkbox"

success(response)
{
    $.each(response,function(index,item){
        var status;
        if(index >=0 and index <= 4)
    {
        if(item.statusCheck = 1)
        {
            status = <input type ="checkbox" checked/>
        }
        else{
            status =  <input type="checkbox"/>
        }   
        var content = '<td>'+ status + '</td>'
                 +<td>' + item.fieldName +'<td>';
            $("#tableId").appent(content)
    }

  }
}

Upvotes: 0

Khoda
Khoda

Reputation: 953

var content = item.statusCheck == 1 ? '<td><input type="checkbox" checked="checked" />'+item.fieldName+'</td>' : '<td><input type="checkbox" />'+item.fieldName+'</td>';
$("#tableId").append($(content));

Upvotes: 0

Piyush.kapoor
Piyush.kapoor

Reputation: 6803

("#tableId").append(content)

Change appent to append

Upvotes: 1

Related Questions