默认情况下,若依会根据当前的查询条件导出所有数据(会把查询参数请求到后台),但是忽略了界面的行选择信息;因此需要手工处理导出选中行的功能;
后端代码
- Mapper中新增根据ID查找记录的语句,因为单个分页记录数不多,直接用IN查询:
<select id="selectSysStudentByStudentIds" resultMap="SysStudentResult">
<include refid="selectSysStudentVo"/>
where student_id in
<foreach collection="array" item="id" index="index" open="(" separator="," close=")">
#{id}
</foreach>
</select>
- 在Service及Mapper代码中增加相应的接口:
/**
* 查询学生信息
*
* @param ids 学生信息主键
* @return 学生信息
*/
public List<SysStudent> selectSysStudentByStudentIds(String[] ids);
- 修改Controller,增加一个单独的选择导出方法(其它都和默认导出一样,只修改路径和参数):
/**
* 导出选中学生信息列表
*/
@PreAuthorize("@ss.hasPermi('app:student:export')")
@Log(title = "学生信息", businessType = BusinessType.EXPORT)
@PostMapping("/exports")
public void export(HttpServletResponse response, @RequestParam(value = "ids") String ids)
{
List<SysStudent> list = sysStudentService.selectSysStudentByStudentIds(ids.split(","));
ExcelUtil<SysStudent> util = new ExcelUtil<SysStudent>(SysStudent.class);
util.exportExcel(response, list, "学生信息数据");
}
前端代码
- 前端的this.ids变量保存了选中记录的ID信息;
- 前端页面中,增加分支处理选择导出:
if(this.ids.length > 0){
this.download('app/student/exports', {'ids':this.ids.join(',')}, `student_${new Date().getTime()}.xlsx`)
}else {
this.download('app/student/export', {
...this.queryParams
}, `student_${new Date().getTime()}.xlsx`)
}
至于,为何将ids拼装后再拆分使用,是因为若依的通用download方法会将请求参数进行转换,会将数组对象拆开,后端不方便处理,所以前端将数组拼装成字符串,后端再拆分使用。