简体   繁体   中英

How To Set Checkbox Checked From Ajax Response In Datatable?

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

@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);
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM