In jqgrid how to check checkbox on row select?

35,233

To your onSelectRow function add:

var ch =  jQuery(this).find('#'+id+' input[type=checkbox]').prop('checked');
if(ch) {
      jQuery(this).find('#'+id+' input[type=checkbox]').prop('checked',false);
} else {
      jQuery(this).find('#'+id+'input[type=checkbox]').prop('checked',true);                       
}

Edit: Put this at the and of your JS code in order to run selection when clicking a checkbox

$("#list1").find('input[type=checkbox]').each(function() {
    $(this).change( function(){
        var colid = $(this).parents('tr:last').attr('id');
        if( $(this).is(':checked')) {
           $("#list1").jqGrid('setSelection', colid );
           $(this).prop('checked',true);
        }
        return true;
    });
});

UPDATED: http://jsfiddle.net/tr5RA/14/

Share:
35,233
Admin
Author by

Admin

Updated on June 27, 2020

Comments

  • Admin
    Admin almost 4 years

    I have the below code for my jqGrid, I want to select a row and the corresponding checkbox to be checked, if i click on the same row again, the checkbox should be disabled, how can i achieve this? I am also providing a snapshot.

    var xmlDoc = $.parseXML(xml); 
             $('#configDiv').empty();
                $('<div width="100%">')
                .attr('id','configDetailsGrid')
                .html('<table id="list1" width="100%"></table>'+
                        '<div id="gridpager"></div>'+
                    '</div>')       
                .appendTo('#configDiv');    
    
                var grid = jQuery("#list1");
    
                grid.jqGrid({
    
                  datastr : xml,
                  datatype: 'xmlstring',
                  colNames:['cfgId','','Name', 'Host', 'Description','Product', 'Type', 'Last Updated Time','Last Updated By',''],
                  colModel:[
                      {name:'cfgId',index:'cfgId', width:90, align:"right", hidden:true},
                      {name:'',index:'', width:15, align:"right",edittype:'checkbox',formatter: "checkbox",editoptions: { value:"True:False"},editable:true,formatoptions: {disabled : false}},
                      {name:'cfgName',index:'cfgName', width:90, align:"right"},
                      {name:'hostname',index:'hostname', width:90, align:"right"},
                      {name:'cfgDesc',index:'cfgDesc', width:90, align:"right"},
                      {name:'productId',index:'productId', width:60, align:"right"},
                      {name:'cfgType',index:'cfgType', width:60, align:"right"},
                      {name:'updateDate',index:'updateDate',sorttype:'Date', width:120, align:"right"},
                      {name:'emailAddress',index:'emailAddress', width:120, align:"right"},
                      {name:'absolutePath',index:'absolutePath', width:90, align:"right", hidden:true},
                  ],
                  pager : '#gridpager',
                  rowNum:10,
                  scrollOffset:0,
                  height: 'auto',
    
                  autowidth:true,
                  viewrecords: true,
                  gridview: true,
                  xmlReader: {
                      root : "list",
                      row: "com\\.abc\\.db\\.ConfigInfo",
                      userdata: "userdata",
                      repeatitems: false
                  },
                  onSelectRow: function(id,status){
                      var rowData = jQuery(this).getRowData(id); 
                      configid = rowData['cfgId'];
                      configname=rowData['cfgName'];
                      configdesc=rowData['cfgDesc'];
                      configenv=rowData['cfgType'];
    
                      if(status==true)
                      {
    
                      }
    
                      rowChecked=1;
                      currentrow=id;
                      },
                  onCellSelect: function(rowid, index, contents, event) {
                      if(index==2)
                      {
    
                            $(xmlDoc).find('list com\\.abc\\.db\\.ConfigInfo').each(function()
                            {
                                //alert($(this).find('cfgId').text()+" "+configid);
                                if($(this).find('cfgId').text()==configid)  
                                {
                                    configname=$(this).find('cfgName').text(); 
                                    configdesc=$(this).find('cfgDesc').text();
                                    configenv=$(this).find('cfgType').text();
                                    filename=$(this).find('fileName').text();
                                    updatedate=$(this).find('updateDate').text();
                                    absolutepath=$(this).find('absolutePath').text();
                                    productname=productMap[$(this).find('productId').text()];
                                }
                            });
    
                      }
                   }
    
                });
                grid.jqGrid('navGrid','#gridpager',{edit:false,add:false,del:false});
    

    enter image description here

    Updated

    my onSelectRow

    var ch =  jQuery(this).find('#'+id+' input[type=checkbox]').prop('checked');
    if(ch) {
      jQuery(this).find('#'+id+' input[type=checkbox]').prop('checked',false);
    } else {
          jQuery(this).find('#'+id+'input[type=checkbox]').prop('checked',true);                       
    }
    
  • Admin
    Admin almost 13 years
    @WooDzu: it says Object dosen't support this property or method, please see my updated question
  • WooDzu
    WooDzu almost 13 years
    if you are not using newest jQuery, change .prop() to .attr()
  • WooDzu
    WooDzu almost 13 years
    sorry, I wasn't on computer for a while. Is it working for you now?
  • Admin
    Admin almost 13 years
    @WooDzu: ya it is working.. also if you could help me again. I need to jqGrid to select the current row if checkbox is cilcked, can you help me with that too here is the link? Thanks