index.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <template>
  2. <div class="myDatePicker">
  3. <div class="date">
  4. <el-date-picker
  5. v-if="type"
  6. prefix-icon="el-icon-date"
  7. v-model="type.time[0]"
  8. :type="type.type"
  9. :format="type.format"
  10. :value-format="type.format"
  11. :placeholder="type.placeholder"
  12. @change="changeTime"
  13. style="width:100%"
  14. >
  15. </el-date-picker>
  16. <template v-if="type && type.range">
  17. <div class="font">
  18. </div>
  19. <el-date-picker
  20. v-if="type"
  21. prefix-icon="el-icon-date"
  22. v-model="type.time[1]"
  23. :type="type.type"
  24. :format="type.format"
  25. :value-format="type.format"
  26. :placeholder="type.placeholder2"
  27. @change="changeTime"
  28. style="width:100%">
  29. </el-date-picker>
  30. </template>
  31. </div>
  32. <div class="icon">
  33. <i class="el-icon-setting" @click="open"></i>
  34. </div>
  35. <el-dialog
  36. title="设置"
  37. :visible.sync="dialogVisible"
  38. width="30%"
  39. :before-close="handleClose">
  40. <div>
  41. <el-radio-group v-model="radio">
  42. <div v-for="item in typeList" :key="item.value" class="radio">
  43. <el-radio :label="item.value">{{item.label}}</el-radio>
  44. </div>
  45. </el-radio-group>
  46. </div>
  47. <span slot="footer" class="dialog-footer">
  48. <el-button @click="handleClose">取 消</el-button>
  49. <el-button type="primary" @click="submit">确 定</el-button>
  50. </span>
  51. </el-dialog>
  52. </div>
  53. </template>
  54. <script>
  55. import moment from 'moment'
  56. export default {
  57. components: {},
  58. props: {
  59. value:{
  60. type:[String,Array],
  61. default:()=>{
  62. return []
  63. }
  64. }
  65. },
  66. data() {
  67. return {
  68. dialogVisible:false,
  69. typeList:[
  70. {
  71. value:'1',
  72. label:'查询某年月到某年月的数据',
  73. time:[],
  74. type:'month',
  75. range:true,
  76. format:'yyyy-MM',
  77. placeholder:'开始日期',
  78. placeholder2:'结束日期'
  79. },
  80. {
  81. value:'2',
  82. label:'查询某年到某年的数据',
  83. time:[],
  84. type:'year',
  85. range:true,
  86. format:'yyyy',
  87. placeholder:'开始日期',
  88. placeholder2:'结束日期'
  89. },
  90. {
  91. value:'3',
  92. operator:'<=',
  93. label:'查询某年前所有数据',
  94. time:[],
  95. type:'year',
  96. format:'yyyy',
  97. placeholder:'请选择年'
  98. },
  99. {
  100. value:'4',
  101. operator:'=',
  102. label:'查询某一整年的数据',
  103. time:[],
  104. type:'year',
  105. format:'yyyy',
  106. placeholder:'请选择年'
  107. },
  108. ],
  109. type:{
  110. value:'3',
  111. time:[],
  112. type:'year',
  113. format:'yyyy',
  114. placeholder:'请选择年'
  115. },
  116. radio:'3',
  117. };
  118. },
  119. watch: {
  120. value(val){
  121. if(val){
  122. this.getValue()
  123. }
  124. }
  125. },
  126. computed: {},
  127. created() {},
  128. mounted() {
  129. this.getValue()
  130. },
  131. methods: {
  132. //获取数据
  133. getValue(){
  134. var val = this.value
  135. if(val){
  136. if(typeof val == 'object'){
  137. this.type.time = val
  138. }else{
  139. this.type.time = [val]
  140. if(this.type.range){
  141. this.type.time[1] = moment().format('yyyy-MM-DD')
  142. this.changeTime()
  143. }
  144. }
  145. }
  146. },
  147. changeTime(){
  148. if(this.type.range){
  149. this.$emit('input',this.type.time)
  150. this.$emit('operator','')
  151. }else{
  152. this.$emit('input',this.type.time[0])
  153. this.$emit('operator',this.type.operator)
  154. }
  155. },
  156. //打开弹窗
  157. open(){
  158. this.radio = this.type.value
  159. this.dialogVisible = true
  160. },
  161. //关闭弹窗
  162. handleClose(){
  163. this.dialogVisible = false
  164. },
  165. //确定选择
  166. submit(){
  167. this.type = null
  168. this.$nextTick(()=>{
  169. this.type = this.typeList.find(item=>{
  170. return item.value == this.radio
  171. })
  172. this.getValue()
  173. this.handleClose()
  174. })
  175. }
  176. },
  177. };
  178. </script>
  179. <style lang="scss">
  180. .myDatePicker{
  181. .el-icon-date{
  182. font-size: 22px;
  183. color: var(--fontcolor);
  184. }
  185. }
  186. </style>
  187. <style lang="scss" scoped>
  188. .myDatePicker{
  189. --fontcolor:#73767d;
  190. width: 500px;
  191. display: flex;
  192. align-items: center;
  193. .date{
  194. width: 100%;
  195. display: flex;
  196. align-items: center;
  197. .font{
  198. padding: 0 5px;
  199. color: var(--fontcolor);
  200. }
  201. }
  202. .icon{
  203. margin-left: 5px;
  204. i{
  205. font-size: 26px;
  206. color: var(--fontcolor);
  207. }
  208. }
  209. .radio{
  210. height: 30px;
  211. }
  212. }
  213. </style>