后端(将所有对应关系以Map形式传入前端):
// 将所有unitId与unitName查询出来绑定在map里
Map<String, String> unitInfoMap = new HashMap<>();
List<UnitInfo> unitInfoList = unitInfoService.list();
for (UnitInfo unitInfo : unitInfoList) {
String opsIdStr = String.valueOf(unitInfo.getUnitid());
String opsName = unitInfo.getUnitname();
if (opsName != null && opsIdStr != null) {
unitInfoMap.put(opsIdStr, opsName);
}
}
mmap.put("unitInfo", unitInfoMap);
前端:
var unitInfo = [[${unitInfo}]];
...
{
field: 'opsId',
title: '单位',
formatter: function (value, row, index) {
var displayText = unitInfo[value] || ''; // 如果找不到对应的字典值,显示默认值 ''
return displayText;
},
},
...
<label class="col-sm-4 control-label is-required">送审单位:</label>
<div class="col-sm-8">
<select id="manageId" name="manageId" required class="form-control m-b" th:with="opsArray=${unitInfo}">
<option th:each="op : ${opsArray}" th:value="${op.key}" th:text="${op.value}"></option>
</select>
</div>