立诚勿怠,格物致知
It's all about connecting the dots

Enable jqgrid to do/cancel selecting only selectable items

I had this requirement these days: We have a table with many rows among which some are selectable and some are not selectable, and there is also a selecting-all button which should support do/cancel selecting all selectable rows. But after glancing over the old project, I found no similar ready-made code, so I search for this by Baidu, and found solution like below (modified and beautified):

var $jqGrid = $('#grid-table');
options.multiselect = true;
// 点击全选时触发事件
options.onSelectAll = function (rowid, status) {
  // 获取jqgrid中所有数据行的id
  var rowIds = $jqGrid.jqGrid('getDataIDs');
  for (var k = 0; k < rowIds.length; k++) {
    // 获取指定id所在行的所有数据
    var curRowData = $jqGrid.jqGrid('getRowData', rowIds[k]);
    if (如果curRowData中的数据表明该行不允许进行选中的话) {
      // 设置改行不能被选中
      $jqGrid.jqGrid('setSelection', rowIds[k], false);
    }
  }
}

// 选择某行时触发事件
options.onSelectRow = function (id) {
  var curRowData = $jqGrid.jqGrid('getRowData', id);
  if (如果curRowData中的数据表明该行不允许进行选中的话) {
    // 设置改行不能被选中
    $jqGrid.jqGrid('setSelection', id, false);
  }
}

The above code seems to work well at first attempt. But you will find that you can not undo the selecting-all operation. I cannot use google for several months, therefore I use bing to search for other solutions, and thank goodness, here are the solution:

options.multiselect = true;
options.rowattr = function (item) {
    if (getString(item.status) !== '1') {
        return {'class': 'ui-state-disabled ui-jqgrid-disablePointerEvents'};
    }
};
options.beforeSelectRow = function (rowid, e) {
    return !$(e.target).closest('tr.jqgrow').hasClass('ui-state-disabled');
};

Haha! I really think the api document for jqgrid plugin is too complicated that it scares me to earnestly read it.

赞(0) 打赏
文章名称:《Enable jqgrid to do/cancel selecting only selectable items》
文章链接:https://www.orzzone.com/enable-jqgrid-to-do-cancel-selecting-only-selectable-items.html
商业联系:yakima.public@gmail.com

本站大部分文章为原创或编译而来,对于本站版权文章,未经许可不得用于商业目的,非商业性转载请以链接形式标注原文出处。
本站内容仅供个人学习交流,不做为任何投资、建议的参考依据,因此产生的问题需自行承担。

评论 抢沙发

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力提供更多优质内容!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册