【问题标题】:How to select jqGrid row on checkbox click?如何在复选框单击时选择 jqGrid 行?
【发布时间】:2011-09-21 22:40:33
【问题描述】:

下面是我的 jqGrid 代码,当我 check 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});

那么如何在选中的复选框上选择当前行?

【问题讨论】:

  • 嗨,你试过我的代码了吗?

标签: jquery jqgrid


【解决方案1】:

把它放在你的 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/

编辑: 每次添加新行时也应该这样做。 让我知道是否要修复其他问题;]

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我应该在哪里添加multiselect:true?我也有 CRUD,所以当我点击更新按钮并且选择了超过 1 个复选框时,它应该提示用户,即它不应该进行更新,在这种情况下我该如何实现?
    • @Ricky:您应该在 jqGrid 定义的同一位置包含 multiselect:true,在该位置使用所有其他 jqGrid options,例如 datatyperowNum 等。你可以找到here 一些演示如何使用多选。关于更新按钮的第二个问题,我不确定我是否正确理解您的意思。也许您可以更详细地解释您需要什么?您打算使用哪种编辑模式?
    【解决方案3】:

    一种简单的方法:

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

    【讨论】:

      【解决方案4】:

      在您的 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
      }
      

      【讨论】:

      • you have to find out the current row here 是什么意思我需要在哪里使用当前行?
      • @Ricky:我不熟悉 jqgrid,但是我编写的代码会在每一行中为您提供选中的复选框(我想),现在是您,必须找到行(this. closet('').css(), 类似这样的东西) 并将 css 应用于该行...很抱歉没有在您的上下文中回答。 :(
      猜你喜欢
      • 1970-01-01
      • 2015-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多