rattenking-dtpicker.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <template>
  2. <picker class='rui-picker rui-class' mode="multiSelector" :range="times" :value="timesIndex" :disabled="curDisabled" @change='changeDate' @cancel="cancelDate" @columnchange="columnchangeDate">
  3. {{curValue}}
  4. </picker>
  5. </template>
  6. <script>
  7. import GetDate from './GetDate.js';
  8. export default {
  9. name: 'rattenking-dtpicker',
  10. props: {
  11. /**
  12. * picker允许选中的最小值
  13. */
  14. start: {
  15. type: String,
  16. default: '2000-01-01'
  17. },
  18. /**
  19. * picker允许选中的最大值
  20. */
  21. end: {
  22. type: String,
  23. default: '2100-12-31'
  24. },
  25. /**
  26. * picker默认展示的值
  27. */
  28. value: {
  29. type: String,
  30. default: ''
  31. },
  32. /**
  33. * picker的时间粒度
  34. */
  35. fields: {
  36. type: String,
  37. default: 'day'
  38. },
  39. /**
  40. * picker是否禁止
  41. */
  42. disabled: {
  43. type: Boolean,
  44. default: false
  45. },
  46. /**
  47. * 是否显示中文
  48. */
  49. isShowChinese: {
  50. type: Boolean,
  51. default: true
  52. }
  53. },
  54. data() {
  55. return {
  56. times:[],
  57. timesIndex: [],
  58. timesString: null,
  59. curValue: null,
  60. startValue: null,
  61. endValue: null,
  62. curDisabled: null,
  63. cancel: null,
  64. lens: {
  65. year: 1,
  66. month: 2,
  67. day: 3,
  68. hour: 4,
  69. minute: 5,
  70. second: 6
  71. }
  72. }
  73. },
  74. watch: {
  75. curDisabled(val){
  76. this.curDisabled = val;
  77. },
  78. curValue(val) {
  79. this.curValue = val;
  80. this.$emit('change', val);
  81. },
  82. times(val){
  83. this.times = val;
  84. },
  85. timesIndex(val){
  86. this.timesIndex = val;
  87. },
  88. cancel(val) {
  89. this.$emit('cancel', val);
  90. }
  91. },
  92. created() {
  93. this.initData();
  94. this.judgeTimeFields();
  95. this.judgeStartEndTime();
  96. this.updateTimesAndIndex();
  97. },
  98. methods: {
  99. initData(){
  100. // 初始化默认时间和是否禁止
  101. this.curValue = this.value;
  102. this.curDisabled = this.disabled;
  103. },
  104. judgeStartEndTime(){
  105. // 判断开始和结束时间大小
  106. let starttimestamp = GetDate.getTimes(this.start);
  107. let endtimestamp = GetDate.getTimes(this.end);
  108. if(endtimestamp <= starttimestamp){
  109. this.curTimes = GetDate.getCurrentTimes(starttimestamp);
  110. this.endTimes = GetDate.getCurrentTimes(starttimestamp);
  111. this.startTimes = GetDate.getCurrentTimes(starttimestamp);
  112. this.startValue = this.start;
  113. this.endValue = this.start;
  114. } else {
  115. this.endTimes = GetDate.getCurrentTimes(endtimestamp);
  116. this.startTimes = GetDate.getCurrentTimes(starttimestamp);
  117. this.startValue = this.start;
  118. this.endValue = this.end;
  119. }
  120. },
  121. judgeTimeFields(){
  122. // 处理默认显示时间
  123. let fields = this.fields;
  124. let curTimes = GetDate.getCurrentTimes();
  125. let curtimestamp = +new Date(this.curValue);
  126. if(curtimestamp === 0 || curtimestamp){
  127. curTimes = GetDate.getCurrentTimes(curtimestamp);
  128. }
  129. this.curTimes = curTimes;
  130. },
  131. updateTimesAndIndex(){
  132. // 更新times和index
  133. let opts = {
  134. startTimes: this.startTimes.detail,
  135. endTimes: this.endTimes.detail,
  136. curTimes: this.curTimes.detail,
  137. fields: this.fields
  138. }
  139. let times = GetDate.getDateTimes(opts);
  140. this.times = times;
  141. let curtimes = Object.values(this.curTimes.detail);
  142. this.timesIndex = times.map((cur,index) => {
  143. var idn = cur.findIndex((cu,idx) => curtimes[index] == cu);
  144. return idn > -1 ? idn : 0;
  145. })
  146. },
  147. getRealCurValue(){
  148. // 获取当前选中的时间字符串
  149. var arr = this.timesIndex.map((cur, index) => {
  150. return this.times[index][cur]
  151. })
  152. return GetDate.format(arr);
  153. },
  154. changeDate(e){
  155. // 确认选中时间
  156. let values = e.detail.value;
  157. this.timesIndex = values;
  158. this.curValue = this.getRealCurValue();
  159. },
  160. columnchangeDate(e){
  161. // 滑动更新times和index
  162. let column = e.detail.column;
  163. let value = e.detail.value;
  164. this.timesIndex[column] = value;
  165. for(let i = 0; i < this.lens[this.fields]; i++){
  166. let realvalue = this.getRealCurValue();
  167. this.curTimes = GetDate.getCurrentTimes(GetDate.getTimes(realvalue));
  168. this.updateTimesAndIndex();
  169. }
  170. },
  171. cancelDate(e){
  172. this.cancel = e
  173. }
  174. }
  175. }
  176. </script>
  177. <style>
  178. </style>