You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

296 lines
7.6 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. <template>
  2. <div class="field-box">
  3. <el-avatar class="avater" :icon="avaterCpd.icon" :style="{backgroundColor:avaterCpd.color}">{{avaterCpd.innerText}}</el-avatar>
  4. <div class="field-info">
  5. <slot name="field-info" :value="dateRange">
  6. <span class="field-value" v-if="!avaterCpd.isNull">{{avaterCpd.innerText}} </span>
  7. <span class="field-value" v-else><span class="label-font-color"></span></span>
  8. <slot name="label">
  9. <span class="field-label">{{label}}</span>
  10. <div v-if="disabled!==true" class="my-select" name="select" :value="dateRange">
  11. <el-date-picker :type="type" :style="styleObj" v-model="dateRange" :value-format="valueFormat" :format="format"
  12. unlink-panels
  13. :range-separator="rangeSepaSrator"
  14. :start-placeholder="startPlaceholder"
  15. :end-placeholder="endPlaceholder"
  16. :default-range="[-30,0]"
  17. @change="onDateRangeChange"
  18. :picker-options="pickerOptions"
  19. ></el-date-picker>
  20. </div>
  21. </slot>
  22. </slot>
  23. </div>
  24. </div>
  25. </template>
  26. <script>
  27. import util from '@/common/js/util'
  28. export default {
  29. name: 'date-range',
  30. components: { },
  31. computed: {
  32. avaterCpd(){
  33. var isEmpty=this.dateRange==null ||this.dateRange.length==0
  34. var formatDate=isEmpty?"":this.formatDateRange(this.dateRange);
  35. var obj={isNull:isEmpty,icon:'el-icon-date',color:'#E4E7ED',innerText:formatDate}
  36. if(this.getColor||this.color){
  37. if(this.getColor){
  38. obj.color= this.getColor(this.dateRange)
  39. }else{
  40. obj.color=this.color
  41. }
  42. }else{
  43. if(!isEmpty){
  44. obj.color= util.getColor(this.dateRange)
  45. }
  46. }
  47. if(this.getIcon||this.icon){
  48. if(this.getIcon){
  49. obj.icon= this.getIcon(this.dateRange)
  50. }else if(this.icon){
  51. obj.icon=this.icon
  52. }
  53. }
  54. return obj;
  55. }
  56. },
  57. data(){
  58. return {
  59. dateRange:[],
  60. }
  61. },
  62. watch:{
  63. dateRange(){
  64. },
  65. value:{
  66. deep:true,
  67. handler(){
  68. this.initData();
  69. }
  70. }
  71. },
  72. props: {
  73. disabled:{
  74. type:Boolean,
  75. default:false,
  76. },
  77. value: {
  78. },
  79. label:{
  80. type:String,
  81. default:'起止时间',
  82. },
  83. type: {
  84. type: String,
  85. default: 'daterange'
  86. },
  87. startKey: {
  88. type: String,
  89. default: 'startTime'
  90. },
  91. styleObj:{
  92. typeof:Object,
  93. default:function(){return { maxWidth:'100%' }}
  94. },
  95. endKey: {
  96. type: String,
  97. default: 'endTime'
  98. },
  99. valueFormat: {
  100. type: String,
  101. default: 'yyyy-MM-dd HH:mm:ss'
  102. },
  103. format: {
  104. type: String,
  105. default: 'yyyy-MM-dd'
  106. },
  107. startPlaceholder: {
  108. type: String,
  109. default: '开始日期'
  110. },
  111. endPlaceholder: {
  112. type: String,
  113. default: '结束日期'
  114. },
  115. rangeSepaSrator:{
  116. type: String,
  117. default: '~'
  118. },
  119. pickerOptions:{
  120. typeof:Object,
  121. default:function(){return util.getPickerOptions('datarange')}
  122. },
  123. autoDefault:{
  124. type:Boolean,
  125. default:true,
  126. },
  127. defaultRange:{
  128. type:Array,
  129. default:function(){
  130. return [-15,15]
  131. }
  132. }
  133. },
  134. methods: {
  135. formatDateRange(dateRange){
  136. if(!dateRange||dateRange.length==0){
  137. return ""
  138. }else{
  139. if(dateRange.length==1){
  140. return util.formatDate(new Date(dateRange[0]),this.format)
  141. }else if(dateRange.length==2){
  142. return util.formatDate(new Date(dateRange[0]),this.format)+this.rangeSepaSrator+util.formatDate(new Date(dateRange[1]),this.format)
  143. }
  144. }
  145. },
  146. initData(){
  147. this.dateRange=[];
  148. if(this.value instanceof Array){
  149. this.dateRange=this.value
  150. }else if(this.value instanceof Object){
  151. if(this.value && this.value[this.startKey] && this.value[this.endKey]){
  152. this.dateRange=[this.value[this.startKey],this.value[this.endKey]]
  153. }
  154. }
  155. if( !this.dateRange || this.dateRange.length===0){
  156. if(this.autoDefault===true || (this.autoDefault!==false && this.defaultRange && this.defaultRange.length==2)){
  157. var now=new Date();
  158. var start=new Date();
  159. var end=new Date();
  160. start.setTime(now.getTime() + 3600 * 1000 * 24 * this.defaultRange[0]);
  161. end.setTime(now.getTime() + 3600 * 1000 * 24 * this.defaultRange[1]);
  162. this.dateRange.push(util.formatDate(start,this.valueFormat))
  163. this.dateRange.push(util.formatDate(end,this.valueFormat))
  164. this.onDateRangeChange(this.dateRange);
  165. }
  166. }
  167. },
  168. onDateRangeChange(dates){
  169. if(this.value && this.value instanceof Object){
  170. if(dates && dates.length===2){
  171. this.value[this.startKey]=dates[0]
  172. this.value[this.endKey]=dates[1]
  173. }else if(dates && dates.length===1){
  174. this.value[this.startKey]=dates[0]
  175. this.value[this.endKey]=null
  176. }else if(dates && dates.length===0){
  177. this.value[this.startKey]=null
  178. this.value[this.endKey]=null
  179. }
  180. this.$emit('input',this.value)
  181. }else if(this.value instanceof Array){
  182. this.$emit('input',dates)
  183. }
  184. this.$emit('change',dates)
  185. },
  186. getMyAvaterInfo(item){
  187. if(!item||item.length==0){
  188. return ""
  189. }else{
  190. return item[0]
  191. }
  192. },
  193. getMyColor(item){
  194. if(this.getColor){
  195. return this.getColor(item)
  196. }
  197. if(item && item.length>0){
  198. return util.getColor(item[0])
  199. }else{
  200. return util.getColor("")
  201. }
  202. },
  203. getMyIcon(item){
  204. if(this.getIcon){
  205. return this.getIcon(item)
  206. }else{
  207. return "el-icon-date";
  208. }
  209. },
  210. },
  211. mounted(){
  212. this.initData();
  213. }
  214. }
  215. </script>
  216. <style lang="scss" scoped>
  217. .field-box {
  218. display: flex;
  219. margin-right:5px;
  220. align-items: center;
  221. cursor: pointer;
  222. height: 40px;
  223. line-height: 40px;
  224. .avater {
  225. background-color:#FF9F73;
  226. }
  227. .field-info {
  228. height: 40px;
  229. line-height: 40px;
  230. margin-left: 10px;
  231. display: flex;
  232. flex-direction: column;
  233. .field-value {
  234. height: 20px;
  235. line-height: 20px;
  236. font-size: 0.75rem;
  237. }
  238. .field-label{
  239. height: 20px;
  240. line-height: 20px;
  241. font-size: 0.75rem;
  242. color: #C0C4CC;
  243. }
  244. }
  245. .my-select{
  246. height: 20px;
  247. line-height: 20px;
  248. margin-left: 5px;
  249. margin-right:5px;
  250. max-width: 250px;
  251. display: none;
  252. }
  253. }
  254. .field-box:hover .field-label{
  255. display: none;
  256. }
  257. .field-box:hover .my-select{
  258. height: 20px;
  259. margin-left: 5px;
  260. display: inline;
  261. }
  262. .dashed-circle{
  263. width:40px;
  264. height:40px;
  265. border:2px dashed #000000;
  266. border-radius:40px/40px;
  267. }
  268. .field-box:hover .dashed-circle{
  269. border:2px dashed #409EFF;
  270. }
  271. </style>