国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

vue3+ts+uniapp小程序端自定義日期選擇器基于內(nèi)置組件picker-view + 擴展組件 Popup 實現(xiàn)自定義日期選擇及其他單列選擇

這篇具有很好參考價值的文章主要介紹了vue3+ts+uniapp小程序端自定義日期選擇器基于內(nèi)置組件picker-view + 擴展組件 Popup 實現(xiàn)自定義日期選擇及其他單列選擇。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

vue3+ts 基于內(nèi)置組件picker-view + 擴展組件 Popup 實現(xiàn)自定義日期選擇及單列選擇


自我記錄

1.先上效果圖

vue3+ts+uniapp小程序端自定義日期選擇器基于內(nèi)置組件picker-view + 擴展組件 Popup 實現(xiàn)自定義日期選擇及其他單列選擇,vue3.0,ts,uniApp,uni-app,小程序,vue
vue3+ts+uniapp小程序端自定義日期選擇器基于內(nèi)置組件picker-view + 擴展組件 Popup 實現(xiàn)自定義日期選擇及其他單列選擇,vue3.0,ts,uniApp,uni-app,小程序,vue
vue3+ts+uniapp小程序端自定義日期選擇器基于內(nèi)置組件picker-view + 擴展組件 Popup 實現(xiàn)自定義日期選擇及其他單列選擇,vue3.0,ts,uniApp,uni-app,小程序,vue

vue3+ts+uniapp小程序端自定義日期選擇器基于內(nèi)置組件picker-view + 擴展組件 Popup 實現(xiàn)自定義日期選擇及其他單列選擇,vue3.0,ts,uniApp,uni-app,小程序,vue

vue3+ts+uniapp小程序端自定義日期選擇器基于內(nèi)置組件picker-view + 擴展組件 Popup 實現(xiàn)自定義日期選擇及其他單列選擇,vue3.0,ts,uniApp,uni-app,小程序,vue
vue3+ts+uniapp小程序端自定義日期選擇器基于內(nèi)置組件picker-view + 擴展組件 Popup 實現(xiàn)自定義日期選擇及其他單列選擇,vue3.0,ts,uniApp,uni-app,小程序,vue

直接上代碼

2.代碼展示

2.1 組件

src\components\hbcy-popup.vue

<script setup lang="ts">
import type { Item, PopupType } from '@/types/addInfo'
import { formatDate, parseDate } from '@/utils'
import { onMounted } from 'vue'
import { ref } from 'vue'

const props = defineProps<{
  popupTitle: string
  type: PopupType
  data?: Item[] // 展示數(shù)據(jù)List
  selectData: string | number // 默認顯示 '2023-8-24' || 0
}>()
const emit = defineEmits<{
  (e: 'confirm-popup', params: string | number): void
  (e: 'close-popup'): void
}>()

// 創(chuàng)建選擇區(qū)間 參考uni文檔
const date = new Date()
// 年月日
const TYPEYY_MM_DD = props.type === 'year' || props.type === 'month' || props.type === 'day'
// 月日
const TYPEMM_DD = props.type === 'month' || props.type === 'day'
const TYPEYY = props.type === 'year'
const TYPEMM = props.type === 'month'
const TYPEDD = props.type === 'day'
const TYPESingle = props.type === 'single'
const years = TYPEYY_MM_DD
  ? Array.from({ length: date.getFullYear() - 1989 }, (_, index) => 1990 + index)
  : []
const months = TYPEMM_DD ? Array.from({ length: 12 }, (_, index) => index + 1) : []
const days = TYPEDD ? Array.from({ length: 31 }, (_, index) => index + 1) : []
// 處理默認展示的時間
const defaultDate = TYPEYY_MM_DD ? parseDate(props.selectData as string, props.type) : []
// 單列數(shù)據(jù)
const singleList = ref(TYPESingle ? props.data : [])
const singleSelect = ref<number>((props.selectData as number) || 0)
// 確保默認時間
const year = ref<number>(defaultDate[0])
const month = ref<number | undefined>(defaultDate[1])
const day = ref<number | undefined>(defaultDate[2])
// 區(qū)分日期展示
let showValueList: number[] = []
// 展示日期的選中時間
if (TYPEDD) {
  showValueList = [
    years.indexOf(defaultDate[0]),
    months.indexOf(defaultDate[1]!),
    days.indexOf(defaultDate[2]!),
  ]
} else if (TYPEMM) {
  showValueList = [years.indexOf(defaultDate[0]), months.indexOf(defaultDate[1]!)]
} else if (TYPEYY) {
  showValueList = [years.indexOf(defaultDate[0])]
} else if (TYPESingle) {
  showValueList = [singleSelect.value]
}
const valueList = ref<number[]>()
onMounted(() => {
  // 確?;仫@的value 在 頁面渲染之后
  valueList.value = showValueList
})

// 切換事件
const bindChange: UniHelper.PickerViewOnChange = (e) => {
  const val = e.detail.value

  if (TYPEYY_MM_DD) {
    year.value = years[val[0]]
    month.value = months[val[1]]
    day.value = days[val[2]]
  } else {
    // 單列
    singleSelect.value = val[0]
  }
}
// 確定按鈕
const onClickConfirmPopup = (): void => {
  if (TYPEYY_MM_DD) {
    emit('confirm-popup', formatDate(year.value, month.value, day.value))
  } else {
    // 單列
    emit('confirm-popup', singleSelect.value)
  }
  onClosePopup()
}
// 關(guān)閉彈出層
const onClosePopup = (): void => {
  emit('close-popup')
}
const { safeAreaInsets } = uni.getSystemInfoSync()
</script>

<template>
  <view class="selectBox">
    <view class="selectTitle">
      <text class="cancel" @click="onClosePopup">取消</text>
      <text class="title">{{ '選擇' + popupTitle }}</text>
      <text class="cancel ok" @click="onClickConfirmPopup">確定</text>
    </view>
    <block v-if="TYPEYY_MM_DD">
      <picker-view
        :immediate-change="true"
        indicator-class="indicatorClass"
        :value="valueList"
        @change="bindChange"
        class="picker-view"
      >
        <picker-view-column>
          <view class="item" v-for="(item, index) in years" :key="index">{{ item }}</view>
        </picker-view-column>
        <picker-view-column v-if="TYPEMM_DD">
          <view class="item" v-for="(item, index) in months" :key="index">{{ item }}</view>
        </picker-view-column>
        <picker-view-column v-if="TYPEDD">
          <view class="item" v-for="(item, index) in days" :key="index">{{ item }}</view>
        </picker-view-column>
      </picker-view>
    </block>
    <!-- TODO -->
    <block v-else>
      <picker-view
        :immediate-change="true"
        indicator-class="indicatorClass"
        :value="valueList"
        @change="bindChange"
        class="picker-view"
      >
        <picker-view-column>
          <view class="item" v-for="item in singleList" :key="item.key">{{ item.value }}</view>
        </picker-view-column>
      </picker-view>
    </block>
    <!-- 修復(fù)啟用:safeArea="true" 時 圓角不好實現(xiàn)問題,現(xiàn)在是自己做的適配-->
    <view :style="{ height: safeAreaInsets?.bottom + 'px' }" style="width: 100%" />
  </view>
</template>
<style lang="scss" scoped>
::v-deep.indicatorClass {
  height: 100rpx;
}
.picker-view {
  width: 750rpx;
  height: 500rpx;
  margin-top: 20rpx;
}
.item {
  line-height: 100rpx;
  text-align: center;
}
.selectBox {
  width: 100%;
  height: fit-content;
  background-color: #fff;
  border-radius: 20rpx 20rpx 0 0;
  .selectTitle {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 100rpx;
    font-size: 32rpx;
    .title {
      font-size: 32rpx;
    }
    .cancel {
      width: 160rpx;
      text-align: center;
      color: #ff976a;
      font-size: 32rpx;
    }
    .ok {
      font-size: 32rpx;
      color: #07c160;
    }
  }
}
</style>

2.2 公共方法處理日期

src\utils\index.ts

// 將 yyyy-mm-dd 的字符串 2023-08-24 => [2023,8,24] || [2023,8] || [2023]
export function parseDate(dateString: string, type: string): [number, number?, number?] {
  const date = dateString ? new Date(dateString) : new Date()
  const year = date.getFullYear()
  const month = type === 'day' || type === 'month' ? date.getMonth() + 1 : undefined
  const day = type === 'day' ? date.getDate() : undefined
  return [year, month, day]
}

// 將數(shù)字格式的年、月、日轉(zhuǎn)換成格式為 yyyy-mm-dd 的字符串 || yyyy-mm || yyyy
export function formatDate(year: number, month?: number, day?: number): string {
  const formattedMonth = month !== undefined ? (month < 10 ? `0${month}` : `${month}`) : ''
  const formattedDay = day !== undefined ? (day < 10 ? `0${day}` : `${day}`) : ''
  return `${year}${formattedMonth ? `-${formattedMonth}` : ''}${
    formattedDay ? `-${formattedDay}` : ''
  }`
}
// 獲取當前年月日并返回yyyy-mm-dd格式
export function getCurrentDate(): string {
  const currentDate = new Date()
  const year = currentDate.getFullYear()
  const month = (currentDate.getMonth() + 1).toString().padStart(2, '0')
  const day = currentDate.getDate().toString().padStart(2, '0')
  return `${year}-${month}-${day}`
}

2.3 使用組件(全局自動導(dǎo)入的情況)

全局自動導(dǎo)入看(https://blog.csdn.net/zhgweb/article/details/132499886?spm=1001.2014.3001.5502 第11標題
沒有配置全局自動導(dǎo)入的需要自己手動引入!
src\pages\test\index.vue文章來源地址http://www.zghlxwxcb.cn/news/detail-685845.html

<script setup lang="ts">
import type { Ref } from 'vue'
import { ref } from 'vue'
import { getCurrentDate } from '@/utils'
type Item = {
  key: number | string
  value: string
}

// 日期相關(guān)
const isShowPopop = ref(false)
// 彈出層實例
const refSelectDialog: Ref<UniHelper.UniPopup | null> = ref(null)
const dateTime = ref(getCurrentDate()) // 默認顯示當前時間
// 單列相關(guān)
let list = [
  { key: 1, value: '最高' },
  { key: 2, value: '最低' },
  { key: 3, value: '自定義' },
]
const singleList = ref<Item[]>(list)
const singleSelect = ref(0)
const isShowSingPopop = ref(false)
const selectItem = ref<Item>(singleList.value[singleSelect.value]) // 默認選中

// 打開日期彈窗 or 單列
const onClickPopup = (type?: string) => {
  refSelectDialog.value!.open()
  if (type === 'single') {
    isShowSingPopop.value = true
  } else {
    isShowPopop.value = true
  }
}
// 關(guān)閉彈窗
const onClosePopup = () => {
  refSelectDialog.value!.close()
  isShowPopop.value = false
  isShowSingPopop.value = false
}
// 確定日期彈窗
const onConfirmPopup = (val: string | number, type: string) => {
  if (type === 'single') {
    singleSelect.value = val as number
    selectItem.value = singleList.value[singleSelect.value]
    console.log(selectItem.value, 'singleSelect')
  } else {
    dateTime.value = val as string
    console.log(dateTime.value, 'dateTime.value')
  }
}
</script>

<template>
  <view class="test-page">
    <!-- 使用組件 -->
    <uni-popup
      ref="refSelectDialog"
      type="bottom"
      :maskClick="false"
      :isMaskClick="false"
      :safeArea="false"
      :close="onClosePopup"
    >
      <hbcy-popup
        v-if="isShowPopop"
        popup-title="日期"
        type="day"
        :select-data="dateTime"
        @confirm-popup="onConfirmPopup($event, 'dateTime')"
        @close-popup="onClosePopup"
      />
      <hbcy-popup
        v-if="isShowSingPopop"
        popup-title="社?;鶖?shù)"
        type="single"
        :data="singleList"
        :select-data="singleSelect"
        @confirm-popup="onConfirmPopup($event, 'single')"
        @close-popup="onClosePopup"
      />
    </uni-popup>
  </view>
</template>
<style lang="scss" scoped>
.test-page {
  .item-date {
    width: 300rpx;
    height: 60rpx;
    line-height: 60rpx;
    text-align: center;
    border: 1rpx solid #999;
    font-size: 28rpx;

    &-placeholder {
      color: #999;
    }

    &-txt {
      color: #333;
    }
  }
}
</style>

3.注意事項

3.1refSelectDialog

// 彈出層實例
const refSelectDialog: Ref<UniHelper.UniPopup | null> = ref(null)
  • ts類型有一些問題,找了好久不知道該給什么類型!!! 新手TS,有大佬的話請指出,感謝!

3.1 backgroundColor="#fff" 圓角問題 (已優(yōu)化)

<uni-popup backgroundColor="#fff" />  // 可以不加了
  • 因為默認是開啟適配的,需要加上背景色,否則就是透明的底部區(qū)域
  • 示例如下:
  • vue3+ts+uniapp小程序端自定義日期選擇器基于內(nèi)置組件picker-view + 擴展組件 Popup 實現(xiàn)自定義日期選擇及其他單列選擇,vue3.0,ts,uniApp,uni-app,小程序,vue
  • 源碼查看
  • vue3+ts+uniapp小程序端自定義日期選擇器基于內(nèi)置組件picker-view + 擴展組件 Popup 實現(xiàn)自定義日期選擇及其他單列選擇,vue3.0,ts,uniApp,uni-app,小程序,vue
    vue3+ts+uniapp小程序端自定義日期選擇器基于內(nèi)置組件picker-view + 擴展組件 Popup 實現(xiàn)自定義日期選擇及其他單列選擇,vue3.0,ts,uniApp,uni-app,小程序,vue
    vue3+ts+uniapp小程序端自定義日期選擇器基于內(nèi)置組件picker-view + 擴展組件 Popup 實現(xiàn)自定義日期選擇及其他單列選擇,vue3.0,ts,uniApp,uni-app,小程序,vue
    整理不易,如有轉(zhuǎn)載請備注原文地址!

到了這里,關(guān)于vue3+ts+uniapp小程序端自定義日期選擇器基于內(nèi)置組件picker-view + 擴展組件 Popup 實現(xiàn)自定義日期選擇及其他單列選擇的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • uniapp 實現(xiàn)富文本編輯器【小程序端】

    uniapp 實現(xiàn)富文本編輯器【小程序端】

    css資源文件戳該鏈接:富文本css文件鏈接 編輯菜單我搞成吸頂效果了,方便手機編輯不用再滑到頭部點編輯菜單:css實現(xiàn)元素吸頂效果 ————————————————————————————————————————————

    2024年02月16日
    瀏覽(29)
  • uniApp -- 學習筆記(vue3+ts)

    uniApp -- 學習筆記(vue3+ts)

    uniApp官網(wǎng)介紹 (一) 個人理解是官網(wǎng)返回一個 SelectorQuery 對象實例。 并且可以在這個實例上使用 select 等方法選擇節(jié)點,并使用 boundingClientRect 等方法選擇需要查詢的信息。但是關(guān)于這個需要到查詢信息,只有打印出來 , 在onReady 調(diào)用 let selectorQuery: UniNamespace.SelectorQuery =

    2024年02月09日
    瀏覽(20)
  • uniapp,小程序端返回上一頁并傳遞參數(shù)

    使用場景: 從A頁面跳到B頁面,在數(shù)據(jù)操作后要返回A頁面(使用uni.navigateBack()返回), 要求: 攜帶參數(shù)返回A頁面,不使用鏈接帶參數(shù)返回,不用使用緩存:uni.setStorageSync()儲存數(shù)據(jù)等情況下怎么傳遞參數(shù) 可使用解決方案: 方法一:使用getCurrentPages() 函數(shù)獲取上一頁面棧的

    2024年02月08日
    瀏覽(23)
  • uniapp實現(xiàn)微信小程序端動態(tài)生成海報

    uniapp實現(xiàn)微信小程序端動態(tài)生成海報

    背景: 基于uniapp實現(xiàn)微信小程序中商品詳情的海報生成與保存 效果圖: 思路: 首先把海報上需要的內(nèi)容準備好,比如用戶頭像,商品信息,二維碼等。需要注意的是,因為二維碼是動態(tài)生成的,所以需要后端傳給我們,前端需要把路徑和參數(shù)傳給后端,后端請求微信服務(wù)接

    2024年02月12日
    瀏覽(24)
  • vue3 ts 定義全局變量

    在 Vue3 中使用 TypeScript 定義全局變量可以這樣做: 創(chuàng)建一個文件,如 global.d.ts ,并在其中聲明全局變量。 在 main.ts 或其他入口文件中引入該文件。 在需要使用全局變量的地方直接使用即可。 注意,這種方式只能用于定義全局變量,不能用于定義全局函數(shù)或類。

    2024年02月17日
    瀏覽(16)
  • uniapp條件判斷app,H5,微信小程序端

    1.在頁面上判斷不同的端 2.JS里面判斷不同的端 3.CSS里面判斷不同的端

    2024年02月05日
    瀏覽(31)
  • 中國省市區(qū)地區(qū)選擇組件(ElementPlus + Vue3 + TS )

    中國省市區(qū)地區(qū)選擇組件(ElementPlus + Vue3 + TS )

    1.引用 2.用法 provinceAndCityData :省市數(shù)據(jù)(不帶“全部”選項) regionData :省市區(qū)數(shù)據(jù)(不帶“全部”選項) provinceAndCityDataPlus :省市區(qū)數(shù)據(jù)(帶“全部”選項) regionDataPlus :省市區(qū)數(shù)據(jù)(帶“全部”選項) CodeToText :例如:CodeToText[‘110000’]輸出北京市 TextToCode :例如:

    2023年04月27日
    瀏覽(33)
  • uniapp 小程序 使用vue3+ts運行項目

    uniapp 小程序 使用vue3+ts運行項目

    uniapp官網(wǎng)教程:uni-app官網(wǎng) 創(chuàng)建方式有2種 第一種:通過HBuilderX可視化界面創(chuàng)建 第二種:通過vue-cli命令行 注意:若不能直接使用命令下載,可去gitee下載模板使用 下載完模板后進行解壓,利用vscode打開文件,會發(fā)現(xiàn)報這樣的錯誤??主要原因是沒有下載依賴 解決以上錯誤:使

    2024年02月13日
    瀏覽(96)
  • uniapp 在app和小程序端使用webview進行數(shù)據(jù)交互

    結(jié)論:app端支持比較好可以做到實時傳遞,微信小程序支持比較差,小程序向url傳參只能通過url,url向app傳參需要特定時機(后退、組件銷毀、分享、復(fù)制鏈接)觸發(fā)才能收到消息。 以下是代碼 app端(需要使用nvue) 微信小程序端(正常vue格式) 3、html端

    2024年02月16日
    瀏覽(32)
  • uniapp 判斷微信小程序端、App端、h5端

    區(qū)分標識 寫法:以 #ifdef 或 #ifndef 加 %PLATFORM% 開頭,以 #endif 結(jié)尾。 #ifdef:if defined 僅在某平臺存在 #ifndef:if not defined 除了某平臺均存在 %PLATFORM%:平臺名稱 此方法支持文件有 .vue (模板里使用 ) .js (使用// 注釋) .css (使用 /* 注釋 */) pages.json (使用// 注釋) 各預(yù)編譯

    2024年02月03日
    瀏覽(33)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包