如何在复选框单击时选择jqGrid行?

3
以下是我针对jqGrid的代码,我想在选中jqgrid行内特定复选框时选择该行或突出显示当前行。目前,在onSelectRow中,我正在使复选框被选中。
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});

当复选框被选中时,如何选择当前行?


嗨,你试过我的代码了吗? - WooDzu
4个回答

5
在JS代码结尾处添加以下内容,以便在单击复选框时运行选择操作。
$("#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;
    });
});

示例更新: http://jsfiddle.net/vCWNP/

编辑: 每次添加新行时也应该执行此操作。 如果还有其他需要修复的问题,请告诉我;]


4

在我看来,你可以非常容易地解决你的问题。你尝试做的事情已经在jqGrid中实现了。如果你删除具有空的name,这是不被允许的的列name:'',index:'',并包括一个额外的jqGrid参数multiselect:true,所有的东西都将按照你的需要工作。


我应该在哪里添加 multiselect:true?另外,我有CRUD,所以当我点击更新按钮并且选择了多个复选框时,它应该提示用户,即它不应该进行更新。在这种情况下,我该如何实现? - user801116
@Ricky:你应该在 jqGrid 的定义中和其他所有 jqGrid 选项,如 datatyperowNum等一起包含 multiselect:true。你可以在这里找到一些多选的演示示例。关于你第二个问题中的更新按钮,我不确定我是否正确理解你的意思。可能你能更详细地解释一下你需要什么?你计划使用哪种编辑模式? - Oleg

1

一个简单的方法是:

jQuery(".jqgrow td input").each(function () {
    jQuery(this).click(function () {
        $("#grid").jqGrid('setSelection', $(this).parents('tr').attr('id'));
    });
});

-1

在你的onSelectRow函数中添加:

var flag = jQuery(this).find('#'+id+' input[type=checkbox]').prop('checked');
if(flag){
      jQuery(this).css('background-color', 'red' )// row will be in red if checkbox is selected, you have to find out the current row here
}

你说的“你必须在这里找出当前行”是什么意思?我需要在哪里使用当前行? - user801116
@Ricky:我不熟悉jqgrid,但是我编写的代码将在每一行中给你选中的复选框(我想),现在轮到你找到行(this.closet('<tr>').css()之类的东西)并将CSS应用于该行...很抱歉没有回答你的问题。 :( - Vivek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接