前端页面:
<div class="layui-form-item">
<label class="layui-form-label" ><span
class="x-red">*</span>素材类型:</label>
<div class="layui-input-inline">
<!-- <input type="text" class="layui-input" th:value="${sourceMateria.name}" name="name" placeholder="修改素材类型" id="sourceId"/>-->
<select id="name" name="name" class="select" size="1" >
<option th:each="sourceMateriaTypes:${sourceMateriaTypes}" th:value="${sourceMateriaTypes.name}"
th:text="${sourceMateriaTypes.name}" th:selected="${sourceMateria.sourceId==sourceMateriaTypes.id}"></option>
</select>
</div>
</div>
后端代码:
/**
* 添加跳转页面
* @return
*/
@GetMapping("editBefore/{id}")
public String editBefore(Model model,@PathVariable("id")Long id){
SourceMateria sourceMateria= sourceMateriaService.getById(id);
//获取类型名用以回显数据
String name=sourceMateriaTypeService.selectById(id).getName();
sourceMateria.setName(name);
List<SourceMateriaType> sourceMateriaTypes=sourceMateriaService.selectAllType();
model.addAttribute("sourceMateriaTypes",sourceMateriaTypes);
model.addAttribute("sourceMateria",sourceMateria);
return prefix+"edit";
}
/**
* 添加
* @param sourceMateria
* @return
*/
@PostMapping("edit")
@ResponseBody
public AjaxResult edit(SourceMateria sourceMateria,
@RequestParam(required = false,value = "videoFile") MultipartFile videoFile,
@RequestParam(required = false,value = "imageFile") MultipartFile imageFile,
@RequestParam(required = false) String name,
@RequestParam(required = false) String title,
@RequestParam(required = false) String content,
@RequestParam(required = false) Integer traffic){
sourceMateria.setTitle(title);
sourceMateria.setContent(content);
sourceMateria.setTraffic(traffic);
//根据素材类型查素材id并加入sourceMateria进行更改数据
QueryWrapper<SourceMateriaType> queryWrapper = new QueryWrapper<SourceMateriaType>();
SourceMateriaType sourceMateriaType = sourceMateriaTypeService.selectOne(queryWrapper.eq("name", name));
sourceMateria.setSourceId(sourceMateriaType.getId());
//上传文件
if (imageFile != null){
String upload = null;
upload = OssFileUtils.uploadSingleFile(imageFile);
sourceMateria.setIcon(upload);
}
if (videoFile != null){
String upload = null;
upload = OssFileUtils.uploadSingleFile(imageFile);
sourceMateria.setVideoUrl(upload);
}
return toAjax(sourceMateriaService.updateById(sourceMateria));
}