Thymeleaf select 实现默认选中,使用 th:field 字符串无效问题

该博客围绕SpringBoot项目展开,前端采用Thymeleaf,旨在实现select下拉控件自动选中功能。后端需传递下拉列表数据集合与当前选中值,前端用Thymeleaf的th:field判断选中。还给出错误代码(用字符串传值判断无效)和正确代码(用对象传值并取属性值)。

SpringBoot项目,前端使用Thymeleaf,要实现一个select下拉控件自动选中的功能。具体步骤是后端传递下拉列表需要加载的数据集合,以及当前需要被选中的值,前端select中使用Thymeleaf的th:field自动判断并选中下拉控件:

 

错误代码(使用字符串传递值userId,这里前端判断无效):

	/**
	 * 新增员工
	 */
	@GetMapping("/add")
	public String add(ModelMap modelMap)
	{
		modelMap.put("userId", "test_id_00001");//直接返回字符串
		List<Media> mediaList = sysUserService.selectMediaList();
		modelMap.put("mediaList", mediaList);
	    return prefix + "/add";
	}

前端使用 th:field 进行自动选中的判断(这里使用字符串userId无效):

<div class="col-sm-8">
	<select name="media_id" id="media_id" class="selectpicker" title="请选择媒体" data-live-search="true" >
		<option th:each="media : ${mediaList}" th:text="${media.mediaName}"
				th:value="${media.mediaId}" th:field="${userId}" ></option>
	</select>
</div>

 

正确代码(使用对象user传值,前端使用对象属性取值):

	/**
	 * 新增员工
	 */
	@GetMapping("/add")
	public String add(ModelMap modelMap)
	{
		SysUser tempUser = new SysUser();
		tempUser.setUserId("test_id_00001");
		modelMap.put("user", tempUser);
		List<Media> mediaList = sysUserService.selectMediaList();
		modelMap.put("mediaList", mediaList);
	    return prefix + "/add";
	}

前端使用 th:field 进行自动选中的判断(使用user对象属性取值user.userId ): 

<div class="col-sm-8">
	<select name="media_id" id="media_id" class="selectpicker" title="请选择媒体" data-live-search="true" >
		<option th:each="media : ${mediaList}" th:text="${media.mediaName}"
				th:value="${media.mediaId}" th:field="${user.userId}" ></option>
	</select>
</div>

 

<!DOCTYPE html> <html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"> <head> <th:block th:include="include :: header('历史记录')"/> <th:block th:include="include :: layui-css"/> </head> <body class="gray-bg"> <div class="container-div"> <div class="row"> <div class="col-sm-12 select-table table-striped"> <table id="bootstrap-table"></table> </div> </div> </div> <th:block th:include="include :: footer"/> <th:block th:include="include :: upload-js"/> <script th:inline="javascript"> //获取应用路径 var ROOT = [[${#servletContext.contextPath}]]; //获取默认语言 var LANG_COUNTRY = [[${#locale.language+'_'+#locale.country}]]; //初始化i18n插件 i18nInitialization(ROOT,LANG_COUNTRY); var prefix = ctx + "asset/space/history"; var spaceId = [[${spaceId}]]; var options; $(function () { options = { url: prefix + "/list?spaceId=" + spaceId, modalName: "历史记录列表", showPageGo: true, columns: [ { field: 'id', // title: '历史记录ID', title: i18n('task.spaceHistory.id'), visible: false }, { field: 'spaceId', // title: '空间信息ID', title: i18n('task.spaceHistory.spaceId'), visible: false }, { field: 'updateContent', // title: '变更内容', title: i18n('task.spaceHistory.updateContent'), width: 600 }, { field: "createByName", // title: '修改者', title: i18n('task.spaceHistory.createByName') }, { field: "createTime", // title: '修改时间', title: i18n('task.spaceHistory.createTime') } ] }; $.table.init(options); }); </script> </body> </html> 这是我的一个页面,其中 { field: 'updateContent', // title: '变更内容', title: i18n('task.spaceHistory.updateContent'), width: 600 }, 它的值(字符串)可能会很长,但是又有一定规律,我希望按“, ”换行
最新发布
11-14
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值