views:

52

answers:

1

I have a textfield where the user enters some number and clicks the "search" button. On clicking the "Search" buttton it displays all the associated JSON records with that number. How do I construct the dynamic HTML table by looping through each record? Here is my JSON structure returned from PHP:

[{"number":"ABC123-product1","value":"HN895","status":true},
{"number":"AYD223-product2","value":"JU226","status":true},
{"number":"AXU323-product3","value":"OL223","status":true}]  

$('#button').click(function(e){
    if($("#txt1field")!=''){
      $.getJSON("student.php",{'no' : $("#txt1field").attr('value'),'search' :''},
        function(data){
           $.ajaxSetup ({ cache: false}); 
           var i=0;
           $.each(data, function(number,value) {
        alert(" Number=="+data[i].number+"value==="+data[i].value);
        i++;
           //How do i construct HTML TABLE and put this items  in to a table with each row having the 
          radiobutton , number,value
       });
     });
   }
  }); 

How do I construct an HTML table and put these items into it, with each row having a radio button, number, and value?Please Help Me

+2  A: 
$('#button').click(function(e){
    if($("#txt1field")!=''){
      $.getJSON("student.php",{'no' : $("#txt1field").attr('value'),'search' :''},
        function(data){
           $.ajaxSetup ({ cache: false});
           var t;
           $.each(data, function(number,item) {
              t += '<tr><td><input type="radio" ... /></td><td>'+item.number+'</td><td>'+item.value+'</td></tr>';
           });


           //insert a new table
           t = '<table ...>'+t+'</table>';
           $('div#output').html(t); 


           //or "append" if you wish to insert the rows in an existing table
           $('table#output').append(t)
        });
   }
});

Good luck!

Mouhannad
@Mouhannad: what is that $("#output").where do we see it
Someone
lol sorry. i just assumed you have a div or something similar where you want the new table to be inserted into. I just called it "output". //<div id="output"></div>
Mouhannad
@Mouhannad:It worked out but small problem var i=0; var rdb = "<input id=RadioButton" + i + " type=radio name=accnt=" + i + " />"; $.each(data, function(number,item) { t += '<tr><td>'+rdb+'</td><td>'+item.number+'</td><td>'+item.value+'</td></tr>'; i++; }); using Radio button means where we can select one row and it is not happening in my case where iam able to select all the radiobuttons for all the rows in the table . I should be able to select one row
Someone
that won't work because you create the rdb variable before $.each() loop. try this instead -> t += '<tr><td><input type="radio" id="RadioButton'+number+'" name="accnt'+number+'" /></td><td>'+item.number+'</td><td>'+item.value+'</td></tr>';
Mouhannad
@Mouhannad: Sorry that was my Mistake of wrong typing and i have put it in the each only. and i have the name attribute for the radio button differently .So that is the Reason i was able to select all
Someone
im glad its solved!
Mouhannad
@mouhannad: Iam still having problem How do i see that atleast one row is selected in the table by using the Radio button.if one row is selected How do i get the Selected Row Values .Please Help me
Someone
that depends really. do you want the value in jquery, asp.net, php, etc? do you have a form with submit button? i can't help without seeing what you've done. i also suggest that you put this in another question to be honest
Mouhannad
Hi "Someone" hope you are not using so many string operations to construct your HTML!! Just a performance tip :D
Ajaxe