date-filter.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. (function(){
  2. Vue.component('datefilter', {
  3. template: '<div class="bgc-f2f3f5 pt20 pb10 plr15 flex">\
  4. <div v-if="showDiv.indexOf(\'year\')>-1">\
  5. <label class="c-333 c-f14" for="">年份:</label>\
  6. <el-select v-model="chooseYear" @change="selectChange" style="width:120px;" class="pr20">\
  7. <el-option v-for="(item , index) in years" :key="index" :label="item" :value="item"></el-option>\
  8. </el-select>\
  9. </div>\
  10. <div v-if="showDiv.indexOf(\'month\')>-1">\
  11. <label class="c-333 c-f14" for="">月份:</label>\
  12. <el-select v-model="chooseMonth" @change="selectChange" style="width:120px;" class="pr20">\
  13. <el-option v-for="(item , index) in months" :key="index" :label="item.name" :value="item.code" v-show="!(nowyear==chooseYear&&item.value>nowmonth)"></el-option>\
  14. </el-select>\
  15. </div>\
  16. <div v-if="showDiv.indexOf(\'custom\')>-1">\
  17. <label class="pl20 c-333 c-f14" for="">自定义:</label>\
  18. <el-date-picker v-model="start" type="date" unlink-panels placeholder="开始日期" value-format="yyyy-MM-dd" @change="changeDate" style="width:140px;" :clearable="false" :picker-options="pickerOptions">\
  19. </el-date-picker>\
  20. <span class="c-999">-</span>\
  21. <el-date-picker v-model="end" type="date" unlink-panels placeholder="结束日期" value-format="yyyy-MM-dd" @change="changeDate" style="width:140px;" :clearable="false" :picker-options="pickerOptions">\
  22. </el-date-picker>\
  23. </div>\
  24. </div>',
  25. props:["show"],
  26. data: function(){
  27. return {
  28. showDiv:["custom","year","month"],
  29. start:"",
  30. end:"",
  31. months:[],
  32. years:[],
  33. chooseMonth:"",
  34. chooseYear:"",
  35. nowyear:'',
  36. nowmonth:'',
  37. pickerOptions:{
  38. disabledDate(time) {
  39. return time.getTime() > Date.now()
  40. },
  41. }
  42. }
  43. },
  44. mounted: function(){
  45. var vm=this
  46. EventBus.$on("init-FilterDate", function(arg){
  47. vm.initData(arg)
  48. })
  49. },
  50. methods: {
  51. initData:function(showDiv){
  52. var vm=this
  53. if(showDiv){
  54. vm.showDiv = showDiv
  55. }
  56. //初始化时间
  57. var now = new Date();
  58. vm.nowyear = vm.chooseYear = now.getFullYear()
  59. vm.nowmonth = now.getMonth()+1
  60. vm.years = []
  61. for(i=vm.nowyear; i>=2019; i--){
  62. vm.years.push(i)
  63. }
  64. vm.months = [{code:"00",name:"全部",value:0}]
  65. for(i=1; i<=12; i++){
  66. var month1= i<=9 ? "0"+i : i
  67. vm.months.push({code:month1,name:i+"月",value:i})
  68. }
  69. vm.selectChange()
  70. },
  71. changeDate:function(){
  72. var vm=this
  73. vm.chooseMonth = ''
  74. vm.chooseYear = ''
  75. console.log("changeDate")
  76. if(vm.start>vm.end){
  77. vm.$message({
  78. message: '开始时间应小于结束时间',
  79. type: 'warning'
  80. });
  81. }else{
  82. vm.initSearch()
  83. }
  84. },
  85. selectChange:function(){
  86. var vm=this
  87. if(vm.nowyear==vm.chooseYear&&vm.chooseMonth>vm.nowmonth){
  88. vm.chooseMonth = "00"
  89. }
  90. if(vm.chooseYear){
  91. if(!vm.chooseMonth) vm.chooseMonth ="00"
  92. if(vm.chooseMonth){
  93. if(vm.chooseMonth == "00"){
  94. if(vm.chooseYear == vm.nowyear){
  95. vm.start = vm.chooseYear+"-01-01"
  96. vm.end = new Date().format("yyyy-MM-dd")
  97. }else{
  98. vm.start = vm.chooseYear+"-01-01"
  99. vm.end = vm.chooseYear+"-12-31"
  100. }
  101. }else{
  102. if(vm.chooseMonth == vm.nowmonth){
  103. vm.start = vm.chooseYear+"-"+vm.chooseMonth+"-01"
  104. vm.end = new Date().format("yyyy-MM-dd")
  105. }else{
  106. var endDate = new Date(vm.chooseYear+"-"+vm.chooseMonth+"-01");
  107. endDate.setMonth(endDate.getMonth()+1);
  108. endDate.setDate(0);
  109. vm.start = vm.chooseYear+"-"+vm.chooseMonth+"-01"
  110. vm.end = new Date(endDate).format("yyyy-MM-dd")
  111. }
  112. }
  113. }
  114. vm.initSearch()
  115. }
  116. },
  117. initSearch:function(){
  118. var vm=this
  119. vm.$emit("initsearch",{year:vm.chooseYear,month:vm.chooseMonth,start:vm.start,end:vm.end})
  120. }
  121. },
  122. })
  123. })()