0

I have ajax response code here that dataCompetence has value 2,5. I Tried to find same id in column comp_id and then set checkbox checked. Here is my html code:

<table id="competence_list" class="table table-striped table-bordered table-hover">
  <thead>   
     <tr>                                
        <th width="1%"><input name="select_all_competence" class="case" id="example-select-all-competence" type="checkbox"></th>
        <th hidden>ID</th>
        <th width="10%">Aspect</th>
        <th width="40%">Description</th>
    </tr>
 </thead>
 <tbody>         
   <?php
    $i = 1;
    if ($this->competenceList) {
     foreach ($this->competenceList as $data) {
   ?>
   <tr>                                        
      <td><input type="checkbox" name="idcheckbox_competence" id="idcheckbox_competence" class="case"></td>
      <td hidden id="comp_id"><?php echo $data['competence_id']; ?></td>
      <td><?php echo $data['aspect']; ?></td>
      <td><?php echo $data['descriptions']; ?></td>
  </tr>
  <?php
    $i++;
    }
  }
  ?>
  </tbody>
</table>

And this what i tried to set checkbox checked :

var dataCompetence = jsonData.dataCompetence;
if(jsonData.success){                      
      $.gritter.removeAll(); 
      $.each(dataCompetence.split(","), function(i,e){
         $("#competence_list input[value='" + e + "']").prop("checked", true);
      });

I don't know ow to find the same value and then set the checkbox checked, help me. Thanks

1 Answer 1

1

@Nike please try and follow these steps.......

Html

<div id="dvCheckBoxListControl"></div>

Jquery

<script>
$(document).ready(function () {
    PopulateCheckBoxList();
})

function PopulateCheckBoxList() {
    $.ajax({
        type: "POST",
        url: '@Url.Action("GetCheckBoxDetails", "Home")',
        contentType: "application/json; charset=utf-8",
        data: "{}",
        dataType: "json",
        success: AjaxSucceeded,
        //error: AjaxFailed
    });
}

 function AjaxSucceeded(result) {
    BindCheckBoxList(result);
 }
function BindCheckBoxList(result) {
    CreateCheckBoxList(result);
 }

function CreateCheckBoxList(checkboxlistItems) {
    var table = $('<table></table>');
    var counter = 0;
    $(checkboxlistItems).each(function () {
        table.append($('<tr></tr>').append($('<td></td>').append($('<input>').attr({
            type: 'checkbox', name: 'chklistitem', value: this.Value, id: 'chklistitem' + counter, checked:this.IsSelected
        })).append(
        $('<label>').attr({
            for: 'chklistitem' + counter++
        }).text(this.Name))));
    });

    $('#dvCheckBoxListControl').append(table);
}

Please create a model CheckBoxItem in your project like this..

Model

 public class CheckBoxItem
{
    public string Name { get; set; }
    public string Value { get; set; }
    public bool IsSelected { get; set; }
}

Controller

 [HttpPost]
    public ActionResult GetCheckBoxDetails()
     {
         List<CheckBoxItem> chkListAppointments = new List<CheckBoxItem>(){

             new CheckBoxItem{ Value="1",Name="Jaipur", IsSelected=true},
             new CheckBoxItem{ Value="2",Name="Ajmer",IsSelected=false},
             new CheckBoxItem{ Value="3",Name="Sikar",IsSelected=true},
         };
         return Json(chkListAppointments, JsonRequestBehavior.AllowGet);
     }
Sign up to request clarification or add additional context in comments.

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.