layui默认选中table的CheckBox复选框

方法一:如何根据条件判断是否默认选中table表格前面的复选框


 
 
  1. table.render({
  2. elem: '#userTable'
  3. , url: '../sysRole/getUserList'
  4. , title: '用户列表'
  5. , page: true //开启分页
  6. , cols: [[
  7. { type: 'checkbox'}
  8. , { field: 'userName', sort: true, title: '用户名称'}
  9. , { field: 'account', sort: true, title: '登录账户'}
  10. , { field: 'deleteFlg', sort: true, title: '是否启用'}
  11. ]]
  12. , done: function(res, page, count){
  13. //可以自行添加判断的条件是否选中
  14. //这句才是真正选中,通过设置关键字LAY_CHECKED为true选中,这里只对第一行选中
  15. res.data[ 0][ "LAY_CHECKED"]= 'true';
  16. //下面三句是通过更改css来实现选中的效果
  17. var index= res.data[ 0][ 'LAY_TABLE_INDEX'];
  18. $( 'tr[data-index=' + index + '] input[type="checkbox"]').prop( 'checked', true);
  19. $( 'tr[data-index=' + index + '] input[type="checkbox"]').next().addClass( 'layui-form-checked');
  20. }
  21. });

方法二:checkFuntion()当翻页或加载时判断是否选中复选框,input第一行禁用,第二行选中,第三行未选中


 
 
  1. <script>
  2. //存储选中的设备Id
  3. var checkId=[];
  4. </script>
  5. <table class= "layui-hide" id= "deviceList" lay-filter= "deviceFilter"> </table>
  6. //模板页定义复选框,可以直接写js控制是否选中
  7. <script type= "text/html" id= "checkboxTpl">
  8. {{#
  9. function checkFuntion(arry){
  10. var isTrue= false;
  11. for( var index in arry){
  12. if(arry[index]==d.id){
  13. isTrue= true;
  14. break;
  15. }
  16. }
  17. return isTrue;
  18. };
  19. if(d.deviceAreaId!= null){ }}
  20. <input type= "checkbox" lay-skin= "primary" value= "{{d.id}}" lay-filter= "deviceIdFilter" disabled>
  21. {{# } else {
  22. if(checkFuntion(checkId)){ }}
  23. <input type= "checkbox" lay-skin= "primary" id= "{{d.id}}" isCheck= "true" value= "{{d.id}}" lay-filter= "deviceIdFilter" checked>
  24. {{# } else { }}
  25. <input type= "checkbox" lay-skin= "primary" id= "{{d.id}}" isCheck= "false" value= "{{d.id}}" lay-filter= "deviceIdFilter">
  26. {{# } }}
  27. {{# } }}
  28. < /script>
  29. table.render({
  30. elem: '#deviceList'
  31. , url: '../devices/findALL ' //数据接口
  32. , title: '仪表表 '
  33. ,height:' 480px '
  34. ,where:{"gatewaySN":selectPid,"channel":searchId}
  35. , page: true //开启分页
  36. , cols: [[ //表头
  37. {field:'id ',width: ' 5% ',templet: '#checkboxTpl ',title: '<div id= "checkAll" "selectAll()" all= "false" class= "layui-unselect layui-form-checkbox" lay-skin= "primary"> <i class="layui-icon layui-icon-ok"></i> </div> </div> '}
  38. // ,{field: 'id ', title: 'ID ', width: ' 5% ', sort: true, title: 'ID '}
  39. , {field: 'deviceName ', width: ' 25% ', sort: true, title: '仪表名称 '}
  40. , {field: 'type ', width: ' 15% ', sort: true, title: '仪表类型 '}
  41. , {field: 'entryName ', width: ' 15% ', sort: true, title: '能耗分项 '}
  42. , {field: 'deviceDesc ', width: ' 40% ', sort: true, title: '仪表描述 '}
  43. ]]
  44. ,done: function(res, page, count){
  45. //每次翻页或者重载时判断是否全选
  46. if ( $('input[isCheck= "false"] ').length==0){
  47. $("#checkAll").attr('all ',' true ');
  48. $("#checkAll").addClass('layui-form-checked ');
  49. }else {
  50. $("#checkAll").attr('all ',' false ');
  51. $("#checkAll").removeClass('layui-form-checked ');
  52. }
  53. }
  54. });
  55. //全选按钮事件
  56. function selectAll() {
  57. var checkAll = $("#checkAll");
  58. if (checkAll.attr('all ') == "false") {
  59. console.log($('input[isCheck= "false"] '));
  60. $('input[isCheck= "false"] ').each(function () {
  61. $(this).next().click();
  62. });
  63. checkAll.attr('all ', ' true ');
  64. checkAll.addClass('layui-form-checked ');
  65. } else if (checkAll.attr('all ') == "true") {
  66. $('input[isCheck= "true"] ').each(function () {
  67. $(this).next().click();
  68. });
  69. checkAll.attr('all ', ' false ');
  70. checkAll.removeClass('layui-form-checked ');
  71. }
  72. }

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值