0

I have here a number of checkboxes. just like this:

<div id = 'fCheck'> <input type="checkbox" id="mango" value="mango" /> <label>MANGO</label><br> <input type="checkbox" id="santol" value="santol" /> <label>SANTOL</label><br> <input type="checkbox" id="guava" value="guava" /> <label>GUAVA</label><br> <input type="checkbox" id="lomboy" value="lomboy" /> <label>LOMBOY</label><br> <input type="checkbox" id="apple" value="apple" /> <label>APPLE</label><br> <input type="checkbox" id="orange" value="orange" /> <label>ORANGE</label><br> </div> 

and I also have this data:

SupplierID fruit_name Granted 10792 "mango" "Y" 10792 "santol" "Y" 10792 "guava" "N" 10792 "lomboy" "N" 10792 "apple" "Y" 10792 "orange" "Y" 

Now, what Im trying to do is that everytime I input that supplierID 10792 (through ajax call), all the fruits that has a Y in granted field, will be shown in the through the checkbox above, while the N will left unchecked. Can please help me out? Checking the checkboxes based on granted yes or no is problem. thanks

My ajax call:

var params = { "SessionID": $.cookie("SessionID"), "dataType":"data" }; $.ajax({ type: 'GET', url: 'processjson.php?' + $.param({path:'supplier/view',json:JSON.stringify(params)}), dataType: primeSettings.ajaxDataType, success: function(data) { if ('error' in data) { showMessage('ERROR: ' + data["error"]["msg"]); } else{ $.each(data['result']['main']['rowdata'], function(rowIndex, rowDataValue) { var groupFlag=0; $.each(rowDataValue, function(columnIndex, rowArrayValue) { var fldName = data['result']['main']['metadata']['fields'][columnIndex].name; if (fldName == 'supplier_id'){ supplierID = rowArrayValue; //alert(rightCode); if (supplierID == SupID){//check if the supplier found groupFlag=1; } } if (fldName == 'fruit_name'){ fruitname = rowArrayValue; } if (fldName == 'granted'){ grant = rowArrayValue; if (groupFlag ==1){ if (hasRight == 'y') $('#dialogUserGroupEdit').append('<input type="checkbox" id="'+ rightGroupCode +'" value="'+ rightDesc +'" /> <label>'+rightDesc+'</label><br>'); //must check the checkbox here that have the same fruit_name in the html } } }); }); } } }) 
4
  • 1
    huh? where is this table? how are you getting the information. etc... Commented Aug 2, 2011 at 15:00
  • What's your ajax returning? A JSON object? Commented Aug 2, 2011 at 15:03
  • @ReX, yes, its json object. But my check box is only html. Commented Aug 2, 2011 at 15:07
  • how is your <del>table</del> data returned? can you post a raw output of your XHR response? Commented Aug 2, 2011 at 15:22

5 Answers 5

1

I have figured it out now. Here's what happen. Everytime I input the 'supplier ID', it will call my ajax and check my supplier table (only under that ID) if the fruits is granted or not. If i see a fruit that is granted, it will loop all the checkboxes I have and check the fruitsID if it exist. If found, then check box will be check.

here's my answer that greatly work for me:

 $("input:checkbox").each(function(){ if ($(this).attr("id") == fruitName){ $(this).attr("checked",true); } }); 

And here is my complete ajax call.

var params = { "SessionID": $.cookie("SessionID"), "dataType":"data" }; $.ajax({ type: 'GET', url: 'processjson.php?' + $.param({path:'supplier/view',json:JSON.stringify(params)}), dataType: primeSettings.ajaxDataType, success: function(data) { if ('error' in data) { showMessage('ERROR: ' + data["error"]["msg"]); } else{ $.each(data['result']['main']['rowdata'], function(rowIndex, rowDataValue) { var groupFlag=0; $.each(rowDataValue, function(columnIndex, rowArrayValue) { var fldName = data['result']['main']['metadata']['fields'][columnIndex].name; if (fldName == 'supplier_id'){ supplierID = rowArrayValue; //alert(rightCode); if (supplierID == SupID){//check if the supplier found groupFlag=1; } } if (fldName == 'fruit_name'){ fruitname = rowArrayValue; } if (fldName == 'granted'){ grant = rowArrayValue; if ((groupFlag ==1) && (granted == 'Y')){ $("input:checkbox").each(function(){ if ($(this).attr("id") == fruitName){ $(this).attr("checked",true); } }); } } }); }); } } }) 
Sign up to request clarification or add additional context in comments.

Comments

0

While creating the markup for these checkboxes you can add a selected="selected attribute in the input tag for those items where Granted == "Y". You dont have to do this through jquery on page load.

Comments

0
IF Granted="Y" <input checked="checked" /> 

just add checked="checked" wherever you want the checkbox to be checked by default.

1 Comment

I hope its easy as that. But I have to look back again in my html and check the ID that have the same as my table...I dont know to code that.
0

You could try this (not tested) -

 $.each(results, function() { if (this.Granted === 'Y') $('input:checkbox[id=' + this.fruit_name + ']').attr('checked',true); }); 

Comments

0

You will need to scan your table and check the box for each found row.

$('#table tr').each(function() { var name = $(this).find('td.eq(2)').text(); var granted = $(this).find('td.eq(3)').text(); var chk = $('#fCheck #'+name); //$('#fCheck input[value="'+name+'"]'); if (granded.toLowerCase() == 'y') { chk.attr('checked', true); } else { chk.removeAttr('checked'); } }); 

Note that this selects every row of the table, you will need to narrow this to the rows only containing your supplier id in the first column if the table contains more rows.

2 Comments

my table is not displayed. I just have to put an ID in a text box and call an ajax. Then look for the fruits that have a y. if found, then check the checkbox in my html. My table is not shown here
then you should have mentionned "data" instead of "table"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.