goupan
2024-04-03 5506e9a45e717ffcb67ec313b5a4e8206d9b3a39
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package cn.stylefeng.roses.kernel.system.integration.core.tag;
 
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.dict.modular.entity.SysDict;
import cn.stylefeng.roses.kernel.dict.modular.entity.SysDictType;
import lombok.extern.slf4j.Slf4j;
 
import java.io.IOException;
import java.util.List;
 
/**
 * checkbox字典组件
 *
 * @author liuhanqing
 * @date 2021/1/16 19:06
 */
@Slf4j
public class SysDictCheckBoxTag extends SysDictBaseTag {
 
    @Override
    public void render() {
        // 初始属性
        initAttr();
        StringBuilder sb = new StringBuilder();
        // 当字典类型编码不为空
        if (StrUtil.isNotBlank(this.getDictTypeCode())) {
            // 根据字典类型编码去查询字典类型
            SysDictType dictType = getDictType();
            // 判断字典类型不为空
            if (dictType != null) {
                List<SysDict> lst = getDictList();
                // 默认选中值
                String defaultValue = this.getDefaultValue();
                // 循环字典列表
                for (SysDict option : lst) {
                    // 生成input的id
                    String id = this.getType() + "_" + this.getName() + "_" + option.getDictCode();
                    // 拼接html
                    sb.append(" <input type='checkbox' name='").append(this.getName())
                            .append("' id='").append(id)
                            .append("' title='").append(option.getDictName())
                            .append("' value='").append(option.getDictCode())
                            .append("'");
                    // layui 元素的风格
                    if (StrUtil.isNotBlank(this.getLaySkin())) {
                        sb.append(" lay-skin='").append(this.getLaySkin()).append("'");
                    }
                    // layui 事件过滤器
                    if (StrUtil.isNotBlank(this.getLayFilter())) {
                        sb.append(" lay-filter='").append(this.getLayFilter()).append("'");
                    }
                    // layui 表单验证
                    if (StrUtil.isNotBlank(this.getLayVerify())) {
                        sb.append(" lay-verify='").append(this.getLayVerify()).append("'");
                    }
                    // 默认选中值
                    if (StrUtil.isNotBlank(defaultValue) && defaultValue.equals(option.getDictCode())) {
                        sb.append(" checked ");
                    }
                    sb.append(" /> ");
                }
            }
        }
        try {
            this.ctx.byteWriter.writeString(sb.toString());
        } catch (IOException e) {
            log.error("checkbox字典初始化异常:",e);
        }
    }
}