• 在uniapp中开发年份弹出选择组件


    多层组件嵌套

    1. <script lang="ts" setup>
    2. import { ref, reactive, watch, onMounted, computed } from "vue";
    3. import zqBodyPopup from '../zq-body-popup/zq-body-popup.vue'
    4. import YearTimeItem from './zq-select-year.vue'
    5. import { isArray } from "../../utils/tool";
    6. const zqBodyPopupRef = ref();
    7. const YearTimeItemRef = ref('')
    8. const open = () => {
    9. zqBodyPopupRef.value.open();
    10. };
    11. const emits = defineEmits(['update:modelValue', 'yeartimePickermodelValue'])
    12. const props = defineProps({
    13. quick: {
    14. type: Object,
    15. default: () => {
    16. return {}
    17. },
    18. },
    19. // 标记的日期
    20. markDays: {
    21. type: Array,
    22. default: () => {
    23. return []; //例如["2023-05-01", "2023-05-03", "2023-04-13"]
    24. }
    25. },
    26. modelValue: {
    27. type: Object,
    28. default: () => {
    29. return {}
    30. },
    31. },
    32. allValue: {
    33. type: Object,
    34. default: () => {
    35. return {}
    36. },
    37. },
    38. })
    39. // const localdata = ref([{
    40. // value: 'LE',
    41. // text: '小于等于',
    42. // },
    43. // {
    44. // value: 'GE',
    45. // text: '大于等于',
    46. // },
    47. // {
    48. // value: 'EQ',
    49. // text: '等于',
    50. // },
    51. // ])
    52. const modelV = ref([]);
    53. const popupShow = ref('')
    54. // const zqBodyPopupRef = ref()
    55. const dates = ref([]) // 当前月的日期数据
    56. const currentDataVal = ref('')
    57. const futureDataVal = ref('')
    58. const choose = ref('')
    59. // const valueStr = ref('')
    60. const showUpper = ref(false);
    61. const placeholderStr = ref('请选择年份');
    62. // yeartimeValue.value.operator=
    63. // modelV.value.yearhangeTime = props.quick.modelValue
    64. // modelV.value.operator = props.quick.gl ? props.quick.gl : "GE"
    65. const yearValue = ref('')
    66. const toggleContent = () => {
    67. // console.log(modelV.value, '===');
    68. yearValue.value = modelV.value
    69. choose.value = modelV.value
    70. let time = choose.value ? new Date(choose.value) : new Date()
    71. currentDataVal.value = Math.floor(time.getFullYear() / 10) * 10
    72. futureDataVal.value = currentDataVal.value * 1 + 9
    73. zqBodyPopupRef.value.open();
    74. // uni.hideKeyboard();
    75. }
    76. // computed
    77. const valueStr = computed(() => {
    78. console.log(props.allValue, '---props.allValue');
    79. if (props.allValue.operator === 'BETWEEN') {
    80. return props.allValue.rangeValue.join(' 至 ')
    81. } else {
    82. return props.allValue.dateValue
    83. }
    84. // if (modelV.value) {
    85. // return modelV.value?.join('-')
    86. // } else {
    87. // return ''
    88. // }
    89. })
    90. const datePlaceholder = computed(() => {
    91. if (props.allValue.operator === 'BETWEEN') {
    92. return `开始年份 - 结束年份`
    93. } else {
    94. return '选择年份';
    95. }
    96. })
    97. // watch
    98. watch(
    99. () => props.modelValue,
    100. (newV) => {
    101. console.log(newV,props.modelValue, '---newV');
    102. // if (newV && Object.keys(newV).length > 0) {
    103. if (isArray(newV)) {
    104. modelV.value = newV
    105. } else {
    106. console.error('zq-pagelist time 类型 value(modelValue) 值,传入应为 Array 类型')
    107. }
    108. // }
    109. },
    110. {
    111. deep: true,
    112. immediate: true,
    113. }
    114. )
    115. // 标记日期
    116. const isMarkDay = (y, key) => {
    117. console.log(y, key, '当前遍历的日期项', y, key);
    118. let flag = false;
    119. // console.log('传递过来需要标记的日期项', this.markDays);
    120. props.markDays.forEach(item => {
    121. let dy = `${y}`
    122. if (item == dy) {
    123. flag = true
    124. }
    125. })
    126. return flag;
    127. }
    128. // // 点击回调
    129. const selectOne = (i, event) => {
    130. let date = `${i + event}`;
    131. let selectD = new Date(date).getTime();
    132. let curTime = new Date().getTime();
    133. choose.value = date
    134. }
    135. watch(modelV.value, (newV) => {
    136. emits('yeartimePickermodelValue', newV);
    137. }, {
    138. deep: true,
    139. immediate: true,
    140. })
    141. const changeMonth = (type) => {
    142. if (type == 'pre') {
    143. currentDataVal.value = currentDataVal.value - 10;
    144. futureDataVal.value = futureDataVal.value - 10;
    145. } else {
    146. currentDataVal.value = currentDataVal.value + 10;
    147. futureDataVal.value = futureDataVal.value + 10;
    148. }
    149. }
    150. // 点击确定返回值
    151. const changeValueBtn = () => {
    152. modelV.value.yearhangeTime = tempYear.value
    153. // console.log(modelV.value,'---modelV.value');
    154. const temp = ['']
    155. // if(modelV.value.operator == 'LE') {
    156. temp[0] = modelV.value.yearhangeTime
    157. // }
    158. emits('update:modelValue', temp);
    159. zqBodyPopupRef.value.close()
    160. }
    161. //
    162. const tempYear = ref('')
    163. const onChangeYearItem = (e) => {
    164. console.log(e, '---onChangeYearItem');
    165. tempYear.value = e
    166. // modelV.value.yearhangeTime = e
    167. // emits('yeartimePickermodelValue', modelV.value);
    168. }
    169. const closeUpper = () => {
    170. zqBodyPopupRef.value.close();
    171. }
    172. // imitate-input中的clearable属性
    173. const onClearable = () => {
    174. emits('update:modelValue', ['']);
    175. }
    176. script>
    177. <style scoped lang="scss">
    178. .year-select {
    179. :deep(.uni-select) {
    180. background-color: var(--UI-BG-5);
    181. }
    182. }
    183. .zq-quicks-checkbox-popup-box {
    184. position: fixed;
    185. background-color: #fff;
    186. z-index: 999;
    187. right: 0;
    188. bottom: 0;
    189. margin-top: 10upx;
    190. // width: 100%;
    191. // width: 100vh !important;
    192. .checkbox-popup-con {
    193. background-color: rgba(0, 0, 0, 0.4);
    194. border-radius: 20upx 20upx 0 0;
    195. margin-bottom: 10upx;
    196. padding: 20upx;
    197. box-sizing: border-box;
    198. :deep(.checkbox__inner) {
    199. display: none !important;
    200. }
    201. :deep(.checklist-box) {
    202. width: 50% !important;
    203. display: flex;
    204. align-items: center;
    205. height: 3vh;
    206. margin: 0 !important;
    207. padding: 0 !important;
    208. }
    209. }
    210. }
    211. .uni-date-editor--x .uni-date__icon-clear[data-v-9802168a] {
    212. padding-right: 3px;
    213. display: flex;
    214. align-items: center;
    215. cursor: pointer;
    216. }
    217. .uni-icons[data-v-d31e1c47] {
    218. font-family: uniicons;
    219. text-decoration: none;
    220. text-align: center
    221. }
    222. .long-popup {
    223. ::deep .uni-scroll-view-content {
    224. transform: none !important;
    225. }
    226. }
    227. .calendar-wrapper {
    228. // width: 100vw;
    229. // color: #bbb7b7;
    230. font-size: 28rpx;
    231. text-align: center;
    232. background-color: #fff;
    233. padding-bottom: 10rpx;
    234. .header {
    235. display: flex;
    236. align-items: center;
    237. justify-content: center;
    238. // height: 88rpx;
    239. color: #42464A;
    240. font-size: 32rpx;
    241. font-weight: bold;
    242. .pre,
    243. .next {
    244. color: #4d7df9;
    245. font-size: 28rpx;
    246. font-weight: normal;
    247. padding: 8rpx 15rpx;
    248. border-radius: 10rpx;
    249. // border: 2rpx solid #dcdfe6;
    250. }
    251. .pre {
    252. margin-right: 30rpx;
    253. }
    254. .next {
    255. margin-left: 30rpx;
    256. }
    257. }
    258. }
    259. .choose {
    260. background-color: #4d7df9;
    261. color: #fff;
    262. }
    263. @mixin flex {
    264. /* #ifndef APP-NVUE */
    265. display: flex;
    266. /* #endif */
    267. flex-direction: row;
    268. }
    269. ::v-deep.is-input-border[data-v-09fd5285] {
    270. border: none;
    271. }
    272. .popup-content {
    273. @include flex;
    274. align-items: center;
    275. justify-content: center;
    276. background-color: #fff;
    277. }
    278. .content {
    279. position: relative;
    280. overflow: hidden;
    281. transition: height 0.4s ease;
    282. .days {
    283. transition: top 0.3s;
    284. display: flex;
    285. align-items: center;
    286. flex-wrap: wrap;
    287. position: relative;
    288. .item {
    289. position: relative;
    290. display: block;
    291. height: 120rpx;
    292. line-height: 120rpx;
    293. width: calc(100% / 4);
    294. .day {
    295. font-style: normal;
    296. display: inline-block;
    297. vertical-align: middle;
    298. width: 60rpx;
    299. height: 60rpx;
    300. line-height: 60rpx;
    301. // overflow: hidden;
    302. // border-radius: 60rpx;
    303. &.choose {
    304. background-color: #4d7df9;
    305. color: #fff;
    306. }
    307. }
    308. }
    309. }
    310. }
    311. .upper-close-icon {
    312. position: absolute;
    313. right: 10upx;
    314. top: 15px;
    315. z-index: 90;
    316. }
    317. style>
    1. <script lang="ts" setup>
    2. import { ref, reactive, watch, onMounted, computed } from "vue";
    3. import zqBodyPopup from '../../../../zq-body-popup/zq-body-popup.vue'
    4. import YearTimeItem from '../../../../zq-input-year/zq-select-year.vue'
    5. import { isArray } from "../../../../../utils/tool";
    6. const zqBodyPopupRef = ref();
    7. const YearTimeItemRef = ref('')
    8. const open = () => {
    9. zqBodyPopupRef.value.open();
    10. };
    11. const emits = defineEmits(['update:modelValue', 'yeartimePickermodelValue'])
    12. const props = defineProps({
    13. quick: {
    14. type: Object,
    15. default: () => {
    16. return {}
    17. },
    18. },
    19. // 标记的日期
    20. markDays: {
    21. type: Array,
    22. default: () => {
    23. return []; //例如["2023-05-01", "2023-05-03", "2023-04-13"]
    24. }
    25. },
    26. modelValue: {
    27. type: Object,
    28. default: () => {
    29. return {}
    30. },
    31. },
    32. allValue: {
    33. type: Object,
    34. default: () => {
    35. return {}
    36. },
    37. },
    38. })
    39. // const localdata = ref([{
    40. // value: 'LE',
    41. // text: '小于等于',
    42. // },
    43. // {
    44. // value: 'GE',
    45. // text: '大于等于',
    46. // },
    47. // {
    48. // value: 'EQ',
    49. // text: '等于',
    50. // },
    51. // ])
    52. const modelV = ref([]);
    53. const popupShow = ref('')
    54. // const zqBodyPopupRef = ref()
    55. const dates = ref([]) // 当前月的日期数据
    56. const currentDataVal = ref('')
    57. const futureDataVal = ref('')
    58. const choose = ref('')
    59. // const valueStr = ref('')
    60. const showUpper = ref(false);
    61. const placeholderStr = ref('请选择年份');
    62. // yeartimeValue.value.operator=
    63. // modelV.value.yearhangeTime = props.quick.modelValue
    64. // modelV.value.operator = props.quick.gl ? props.quick.gl : "GE"
    65. const yearValue = ref('')
    66. const toggleContent = () => {
    67. // console.log(modelV.value, '===');
    68. yearValue.value = modelV.value
    69. choose.value = modelV.value
    70. let time = choose.value ? new Date(choose.value) : new Date()
    71. currentDataVal.value = Math.floor(time.getFullYear() / 10) * 10
    72. futureDataVal.value = currentDataVal.value * 1 + 9
    73. zqBodyPopupRef.value.open();
    74. // uni.hideKeyboard();
    75. }
    76. // computed
    77. const valueStr = computed(() => {
    78. console.log(props.allValue, '---props.allValue');
    79. if (props.allValue.operator === 'BETWEEN') {
    80. return props.allValue.rangeValue.join(' 至 ')
    81. } else {
    82. return props.allValue.dateValue
    83. }
    84. // if (modelV.value) {
    85. // return modelV.value?.join('-')
    86. // } else {
    87. // return ''
    88. // }
    89. })
    90. const datePlaceholder = computed(() => {
    91. if (props.allValue.operator === 'BETWEEN') {
    92. return `开始年份 - 结束年份`
    93. } else {
    94. return '选择年份';
    95. }
    96. })
    97. // watch
    98. watch(
    99. () => props.modelValue,
    100. (newV) => {
    101. console.log(newV,props.modelValue, '---newV');
    102. // if (newV && Object.keys(newV).length > 0) {
    103. if (isArray(newV)) {
    104. modelV.value = newV
    105. } else {
    106. console.error('zq-pagelist time 类型 value(modelValue) 值,传入应为 Array 类型')
    107. }
    108. // }
    109. },
    110. {
    111. deep: true,
    112. immediate: true,
    113. }
    114. )
    115. // 标记日期
    116. const isMarkDay = (y, key) => {
    117. console.log(y, key, '当前遍历的日期项', y, key);
    118. let flag = false;
    119. // console.log('传递过来需要标记的日期项', this.markDays);
    120. props.markDays.forEach(item => {
    121. let dy = `${y}`
    122. if (item == dy) {
    123. flag = true
    124. }
    125. })
    126. return flag;
    127. }
    128. // // 点击回调
    129. const selectOne = (i, event) => {
    130. let date = `${i + event}`;
    131. let selectD = new Date(date).getTime();
    132. let curTime = new Date().getTime();
    133. choose.value = date
    134. }
    135. watch(modelV.value, (newV) => {
    136. emits('yeartimePickermodelValue', newV);
    137. }, {
    138. deep: true,
    139. immediate: true,
    140. })
    141. const changeMonth = (type) => {
    142. if (type == 'pre') {
    143. currentDataVal.value = currentDataVal.value - 10;
    144. futureDataVal.value = futureDataVal.value - 10;
    145. } else {
    146. currentDataVal.value = currentDataVal.value + 10;
    147. futureDataVal.value = futureDataVal.value + 10;
    148. }
    149. }
    150. // 点击确定返回值
    151. const changeValueBtn = () => {
    152. modelV.value.yearhangeTime = tempYear.value
    153. // console.log(modelV.value,'---modelV.value');
    154. const temp = ['']
    155. // if(modelV.value.operator == 'LE') {
    156. temp[0] = modelV.value.yearhangeTime
    157. // }
    158. emits('update:modelValue', temp);
    159. zqBodyPopupRef.value.close()
    160. }
    161. //
    162. const tempYear = ref('')
    163. const onChangeYearItem = (e) => {
    164. console.log(e, '---onChangeYearItem');
    165. tempYear.value = e
    166. // modelV.value.yearhangeTime = e
    167. // emits('yeartimePickermodelValue', modelV.value);
    168. }
    169. const closeUpper = () => {
    170. zqBodyPopupRef.value.close();
    171. }
    172. // imitate-input中的clearable属性
    173. const onClearable = () => {
    174. emits('update:modelValue', ['']);
    175. }
    176. script>
    177. <style scoped lang="scss">
    178. .year-select {
    179. :deep(.uni-select) {
    180. background-color: var(--UI-BG-5);
    181. }
    182. }
    183. .zq-quicks-checkbox-popup-box {
    184. position: fixed;
    185. background-color: #fff;
    186. z-index: 999;
    187. right: 0;
    188. bottom: 0;
    189. margin-top: 10upx;
    190. // width: 100%;
    191. // width: 100vh !important;
    192. .checkbox-popup-con {
    193. background-color: rgba(0, 0, 0, 0.4);
    194. border-radius: 20upx 20upx 0 0;
    195. margin-bottom: 10upx;
    196. padding: 20upx;
    197. box-sizing: border-box;
    198. :deep(.checkbox__inner) {
    199. display: none !important;
    200. }
    201. :deep(.checklist-box) {
    202. width: 50% !important;
    203. display: flex;
    204. align-items: center;
    205. height: 3vh;
    206. margin: 0 !important;
    207. padding: 0 !important;
    208. }
    209. }
    210. }
    211. .uni-date-editor--x .uni-date__icon-clear[data-v-9802168a] {
    212. padding-right: 3px;
    213. display: flex;
    214. align-items: center;
    215. cursor: pointer;
    216. }
    217. .uni-icons[data-v-d31e1c47] {
    218. font-family: uniicons;
    219. text-decoration: none;
    220. text-align: center
    221. }
    222. .long-popup {
    223. ::deep .uni-scroll-view-content {
    224. transform: none !important;
    225. }
    226. }
    227. .calendar-wrapper {
    228. // width: 100vw;
    229. // color: #bbb7b7;
    230. font-size: 28rpx;
    231. text-align: center;
    232. background-color: #fff;
    233. padding-bottom: 10rpx;
    234. .header {
    235. display: flex;
    236. align-items: center;
    237. justify-content: center;
    238. // height: 88rpx;
    239. color: #42464A;
    240. font-size: 32rpx;
    241. font-weight: bold;
    242. .pre,
    243. .next {
    244. color: #4d7df9;
    245. font-size: 28rpx;
    246. font-weight: normal;
    247. padding: 8rpx 15rpx;
    248. border-radius: 10rpx;
    249. // border: 2rpx solid #dcdfe6;
    250. }
    251. .pre {
    252. margin-right: 30rpx;
    253. }
    254. .next {
    255. margin-left: 30rpx;
    256. }
    257. }
    258. }
    259. .choose {
    260. background-color: #4d7df9;
    261. color: #fff;
    262. }
    263. @mixin flex {
    264. /* #ifndef APP-NVUE */
    265. display: flex;
    266. /* #endif */
    267. flex-direction: row;
    268. }
    269. ::v-deep.is-input-border[data-v-09fd5285] {
    270. border: none;
    271. }
    272. .popup-content {
    273. @include flex;
    274. align-items: center;
    275. justify-content: center;
    276. background-color: #fff;
    277. }
    278. .content {
    279. position: relative;
    280. overflow: hidden;
    281. transition: height 0.4s ease;
    282. .days {
    283. transition: top 0.3s;
    284. display: flex;
    285. align-items: center;
    286. flex-wrap: wrap;
    287. position: relative;
    288. .item {
    289. position: relative;
    290. display: block;
    291. height: 120rpx;
    292. line-height: 120rpx;
    293. width: calc(100% / 4);
    294. .day {
    295. font-style: normal;
    296. display: inline-block;
    297. vertical-align: middle;
    298. width: 60rpx;
    299. height: 60rpx;
    300. line-height: 60rpx;
    301. // overflow: hidden;
    302. // border-radius: 60rpx;
    303. &.choose {
    304. background-color: #4d7df9;
    305. color: #fff;
    306. }
    307. }
    308. }
    309. }
    310. }
    311. .upper-close-icon {
    312. position: absolute;
    313. right: 10upx;
    314. top: 15px;
    315. z-index: 90;
    316. }
    317. style>
    1. <script setup>
    2. import { ref, watch, getCurrentInstance } from 'vue'
    3. import dateTpl from './date-time/datetimePicker.vue'
    4. import yearTpl from './year/yearTimePickchange.vue'
    5. const operatorData_Base = [
    6. {
    7. value: 'LE',
    8. text: '小于等于',
    9. },
    10. {
    11. value: 'GE',
    12. text: '大于等于',
    13. },
    14. {
    15. value: 'EQ',
    16. text: '等于',
    17. },
    18. {
    19. value: 'BETWEEN',
    20. text: '介于',
    21. }
    22. ];
    23. const props = defineProps({
    24. //v-model
    25. modelValue: {
    26. type: Object,
    27. default: function () {
    28. return {}
    29. },
    30. },
    31. comb: {
    32. type: Object,
    33. default: function () {
    34. return {}
    35. },
    36. },
    37. });
    38. const emits = defineEmits(['update:modelValue'])
    39. const curOperator = ref([]); // 下拉框可选项
    40. const init = () => {
    41. console.log(props, '---props.comb.operatorOptions');
    42. // 日期 和 年 都包含 between
    43. if (props.comb.operatorOptions) {
    44. curOperator.value = props.comb.operatorOptions;
    45. } else {
    46. // 可配置 选择项
    47. curOperator.value = operatorData_Base.slice();
    48. }
    49. // 父传子,数据回显
    50. if (props.modelValue.dateValue) {
    51. props.modelValue.value[0] = props.modelValue.dateValue
    52. }
    53. // modelV.value = Object.assign({}, props.modelValue)
    54. }
    55. init();
    56. const changeValue = (valueArr) => {
    57. let tempObj = Object.assign({}, props.modelValue);
    58. if (props.modelValue.operator === 'BETWEEN') {
    59. tempObj.rangeValue = valueArr;
    60. } else {
    61. tempObj.dateValue = valueArr[0]
    62. }
    63. tempObj.value = valueArr;
    64. emits('update:modelValue', tempObj);
    65. }
    66. script>
    67. <style lang="scss" scoped>
    68. .time-box {
    69. display: flex;
    70. .operator-select {
    71. max-width: 30%;
    72. margin: 0 3%;
    73. }
    74. .time-content-box {
    75. width: 61%;
    76. margin-right: 3%;
    77. }
    78. :deep .uni-select {
    79. background-color: #fff;
    80. }
    81. }
    82. style>
  • 相关阅读:
    【矩阵论】4. 矩阵运算——矩阵拉直
    深入讲解Netty那些事儿之从内核角度看IO模型(上)
    强化学习问题(7)--- Python和Pytorch,Tensorflow的版本对应
    仿真51单片机程序(下载安装+Proteus)
    MySql的索引与算法-B+树索引
    java基于微信小程序的家电维修预约系统 uniapp 小程序
    python引用from的变量报错Unresolved reference ‘xxxx‘
    js原生ajax请求
    Mysql 详解所有的数据类型
    Android如何管理多进程
  • 原文地址:https://blog.csdn.net/irisMoon06/article/details/133852649