element-ui 实现多日期选择

x33g5p2x  于2021-11-11 转载在 其他  
字(3.3k)|赞(0)|评价(0)|浏览(263)

一 前端代码

  1. <template>
  2. <div class="app-container">
  3. <!-- 选择一个或多个日期 -->
  4. <el-date-picker
  5. ref="datesRef"
  6. type="dates"
  7. v-model="searchObj.dateArr"
  8. :editable="false"
  9. format="yyyy-MM-dd"
  10. value-format="yyyy-MM-dd"
  11. placeholder="选择一个或多个日期"
  12. ></el-date-picker>
  13. <!-- 打印选择日期 -->
  14. <el-button type="primary" @click="clickBtn" class="btn">打印选择的时间</el-button>
  15. <div style="margin-top: 20px">
  16. <span>打印区</span>
  17. <el-input type="textarea" :rows="2" v-model="printStr"></el-input>
  18. </div>
  19. <!-- 条件查询 -->
  20. <el-form :inline="true" class="demo-form-inline">
  21. <el-form-item>
  22. <el-input v-model="searchObj.name" placeholder="名称" />
  23. </el-form-item>
  24. <el-button type="primary" icon="el-icon-search" @click="getList()">搜索</el-button>
  25. </el-form>
  26. <!-- 列表 -->
  27. <el-table :data="list" stripe style="width: 100%">
  28. <el-table-column type="index" width="50" />
  29. <el-table-column prop="name" label="名称" />
  30. <el-table-column prop="startTime" label="开始时间" />
  31. <el-table-column prop="endTime" label="结束时间" />
  32. </el-table>
  33. </div>
  34. </template>
  35. </div>
  36. </template>
  37. <script>
  38. // 引入接口定义的 js 文件
  39. import taskDistribution from "@/api/taskDistribution";
  40. export default {
  41. // 定义变量和初始值
  42. data() {
  43. return {
  44. printStr: "",
  45. current: 1, // 当前页
  46. limit: 20, // 每页显示记录数
  47. searchObj: {}, // 条件封装对象
  48. list: [] // 每页数据集合
  49. };
  50. },
  51. // 在页面渲染之前执行,一般调用 methods 中定义的方法,得到数据
  52. created() {
  53. //this.getList();
  54. },
  55. mounted: function() {
  56. //为了解决bug,所以默认值放在了这里
  57. this.$nextTick(function() {
  58. this.dateArr = [];
  59. this.$refs.datesRef.showPicker();
  60. this.$refs.datesRef.hidePicker();
  61. });
  62. },
  63. methods: {
  64. clickBtn: function() {
  65. this.printStr = this.searchObj.dateArr ? this.searchObj.dateArr.join() : "";
  66. },
  67. // 定义方法,进行请求接口调用
  68. // 拜访列表
  69. getList(page = 1) {
  70. // 添加当前页参数
  71. this.current = page;
  72. taskDistribution
  73. .getVisitList(this.current, this.limit, this.searchObj)
  74. .then(response => {
  75. // response 是接口返回数据
  76. this.list = response.data;
  77. }) // 请求成功
  78. .catch(error => {});
  79. } // 请求失败
  80. }
  81. };
  82. </script>

二 后端代码

  1. @Override
  2. public List<Visit> selectPage(Integer page, Integer limit, VisitVo visitVo) {
  3. Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
  4. // 0为第一页
  5. Pageable pageable = PageRequest.of(page - 1, limit, sort);
  6. Visit visit = new Visit();
  7. BeanUtils.copyProperties(visitVo, visit);
  8. // 创建匹配器,即如何使用查询条件
  9. ExampleMatcher matcher = ExampleMatcher.matching() // 构建对象
  10. .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) // 改变默认字符串匹配方式:模糊查询
  11. .withIgnoreCase(true); //改变默认大小写忽略方式:忽略大小写
  12. // 创建实例
  13. Example<Visit> example = Example.of(visit, matcher);
  14. Page<Visit> pages = visitRepository.findAll(example, pageable);
  15. List<Visit> contentList = pages.getContent();
  16. List<Visit> visitList = new ArrayList<>();
  17. contentList.stream().forEach(item -> {
  18. List<String> dateArr = visitVo.getDateArr();
  19. for (String date : dateArr) {
  20. Date startDate = item.getStartTime();
  21. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  22. String startDatestr = dateFormat.format(startDate);
  23. if(startDatestr.contains(date)){
  24. visitList.add(item);
  25. break;
  26. }
  27. }
  28. });
  29. return visitList;
  30. }

三 测试效果

1 选择8号和11号

2 点击确认,然后点击打印选择的时间

3 数据展示,仅展示8号和11号的数据

相关文章