BeyondAdmin、Bootstrap样式的权限树选择结构(JAVA版)

  • 1.前台HTML

<div class="form-group">
<label for="rules"
class="col-sm-2 control-label no-padding-right">模块</label>
<div class="col-sm-6">
<div class="well with-header  with-footer">
<div class="header bg-darkorange">请选择模块</div>
<table
class="table table-hover table-striped table-bordered table-condensed">
<tbody>
<c:forEach var="m" items="${modules }">
<tr>
<td><label> 
<c:if test="${m.parentId != '0' }">
                                                       
                                                        <c:forEach begin="0" end="${m.layerNum * 3 }" step="1">
                                                        &nbsp;
                                                       </c:forEach>
                                                       </c:if>
<input name="modulesId" value="${m.id }" dataid="id-${m.cwhich }" class="checkbox-parent <c:if test="${m.layerNum != 0 }">checkbox-child</c:if>" type="checkbox">
<span class="text">${m.name }</span>
</label></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
<p class="help-block col-sm-4 red">* 必填</p>

</div>

效果图如下:

BeyondAdmin、Bootstrap样式的权限树选择结构(JAVA版)

  • 2.前台JS

/* 权限配置 */
   $(function () {
       //动态选择框,上下级选中状态变化
       $('input.checkbox-parent').on('change', function () {
           var dataid = $(this).attr("dataid");
           $('input[dataid^=' + dataid + ']').prop('checked', $(this).is(':checked'));
       });
       $('input.checkbox-child').on('change', function () {
           var dataid = $(this).attr("dataid");
           dataid = dataid.substring(0, dataid.lastIndexOf("-"));
           var parent = $('input[dataid=' + dataid + ']');
           if ($(this).is(':checked')) {
               parent.prop('checked', true);
               //循环到顶级
               while (dataid.lastIndexOf("-") != 2) {
                   dataid = dataid.substring(0, dataid.lastIndexOf("-"));
                   parent = $('input[dataid=' + dataid + ']');
                   parent.prop('checked', true);
               }
           } else {
               //父级
               if ($('input[dataid^=' + dataid + '-]:checked').length == 0) {
                   parent.prop('checked', false);
                   //循环到顶级
                   while (dataid.lastIndexOf("-") != 2) {
                       dataid = dataid.substring(0, dataid.lastIndexOf("-"));
                       parent = $('input[dataid=' + dataid + ']');
                       if ($('input[dataid^=' + dataid + '-]:checked').length == 0) {
                           parent.prop('checked', false);
                       }
                   }
               }
           }
       });

   });

  • 3.后台JAVA代码
/**
* 获取所有的模块 排序
*/
@Override
public List<Module> getAllModules() {

ModuleQuery query = new ModuleQuery();
query.setOrderByClause("order_no ASC");
// Criteria criteria = query.createCriteria();
// criteria.andStateNotEqualTo(0);// 状态为0排除

List<Module> list = moduleDao.selectByExample(query);
if(list != null && list.size() > 0) {
List<Module> sort = sort(list, new ArrayList<>(), "0", 0);

return sort;
}
return list;
}


// 递归排序
private List<Module> sort(List<Module> list, List<Module> result, String pid, int level){

for (Module module : list) {
if(pid.equals(module.getParentId())) {
module.setLayerNum(level);

// 设置cwhich字段为该元素的所有父ID  以“-”分割
String ids = getParentIds(module.getId());

module.setCwhich(ids);

result.add(module);

// 递归
sort(list, result, module.getId(), level+1);
}

}

return result;
}

// 获取父IDS
private String getParentIds(String id) {

// 获取所有的模块
ModuleQuery query = new ModuleQuery();
query.setOrderByClause("order_no DESC");
List<Module> list = moduleDao.selectByExample(query);
List<String> arrayList = _getParentIds(list, id, new ArrayList<String>());
Collections.reverse(arrayList);
String ids = StringUtils.join(arrayList.toArray(), "-");

return ids;
}

// 递归获取所有父IDS
private List<String> _getParentIds(List<Module> list, String id, ArrayList<String> arrayList) {

for (Module m : list) {

if(id.equals(m.getId())) {
arrayList.add(m.getId());
_getParentIds(list, m.getParentId(), arrayList);

}

}

return arrayList;
}