如何在Datatable中设置从Ajax响应中选中的复选框?

时间:2016-05-16 10:42:12

标签: javascript jquery ajax checkbox

我这里有ajax响应代码,dataCompetence的值为2,5。我试图在列comp_id中找到相同的ID,然后设置复选框。这是我的HTML代码:

<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>

这就是我试图设置复选框的选项:

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

我不知道要找到相同的值,然后设置复选框选中,帮帮我。感谢

1 个答案:

答案 0 :(得分:1)

@Nike请尝试按照以下步骤操作.......

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

请在项目中创建一个模型CheckBoxItem,如下所示..

模型

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

控制器

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