thymeleaf 下拉选择框 处理 For LayUI

  • Post author:
  • Post category:其他


实体

package com.yanzhen.po;

import lombok.Data;

@Data
public class Dic {
    private Integer id;
    private String name;
    private String pName;
}

Dao

package com.yanzhen.dao;

import com.yanzhen.po.Dic;

import java.util.List;

public interface DicDao {
    List<Dic> findTest();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yanzhen.dao.DicDao">
    <select id="findTest" resultType="com.yanzhen.po.Dic">
        SELECT
        a.id,
        b.`name` as pname,
        a.`name`
        from
        sysdictionaries as a
        LEFT JOIN sysdictionaries as b on b.id=a.pid
        where a.pid!=0
        ORDER BY a.pid
    </select>
</mapper>

service

package com.yanzhen.service;

import com.yanzhen.po.Dic;

import java.util.List;

public interface DicService {
    List<Dic> findTest();
}
package com.yanzhen.service;

import com.yanzhen.dao.DicDao;
import com.yanzhen.po.Dic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service("dic")
public class DicServiceImpl implements DicService{
    @Autowired
    private DicDao dicDao;
    @Override
    public List<Dic> findTest() {
        return dicDao.findTest();
    }
}

实现

/******
     * 页面的渲染使用
     */
    @RequestMapping("/test")
    public String test(Model model){
        List<Dic> list=dicService.findTest();
        model.addAttribute("dicList",list);//下拉框内容
        model.addAttribute("reserveId",21);//默认选中项
        return "pages/test";
    }

前端

<div class="layui-form-item">
        <select data-placeholder="请选择"  id="laboratoryId" name="laboratoryId"  class="form-control chosen-select" style="width: 100%; "  required >
            <option   value=""   selected>请选择</option>
            <option   th:each="laboratoryDO:${dicList}"
                      th:value="${laboratoryDO.id}"
                      th:text="${laboratoryDO.name}"
                      th:selected="${reserveId == laboratoryDO.id}"
            ></option>
        </select>
    </div>

效果

在这里插入图片描述



版权声明:本文为weixin_44690195原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。