7.從0做一個vue鍵盤組件

文章目錄

  • 1. 從0做一個鍵盤組件
    • 1.1. 最終效果
    • 1.2. 分析
    • 1.3. 實現
    • 1.4. 如何引用

1. 從0做一個鍵盤組件

首先是why的問題:為什么需要做鍵盤組件?

我們目前可知的場景:

  1. 在新增賬單的時候,需要用到鍵盤
  2. 在比如從賬單列表頁,進行行項目編輯某筆賬單的時候,也需要用到鍵盤

如果都是每個vue頁面自己寫鍵盤的話,就比較尷尬了,回頭需要優化的時候,就要多處維護。

所以,我們需要定義一個鍵盤組件。

1.1. 最終效果

或者是編輯的時候:

1.2. 分析

  1. 主要是顯示:item_name賬單類目名稱、bill_money賬單金額、賬單日期選擇、提交賬單、刪除賬單
  2. 如果是新增賬單,就不顯示”刪除賬單“按鈕;如果是編輯賬單,展示”刪除賬單“按鈕。

1.3. 實現

整體的鍵盤樣式都比較簡單,可以直接通過布局來繪制。

直接上代碼:

<template><view class="keyboardbox"><view style="display: flex; font-size: 50rpx; justify-content: space-between; padding: 20rpx 20rpx;"><view style="font-size: 40rpx;">{{item_name}}</view><view color = '#bbb' >{{bill_money}}</view></view><u-inputplaceholder="備注: 點擊填寫備注":border="true"v-model="bill_desc"clearable></u-input><view class="numkeyboard"><view class="num-area"><view class="row" v-for="(item,index) in numKeybordList" :key="index"><view class="item"v-for="(ite,idx) in item" hover-class="active" :hover-start-time="0":hover-stay-time="5" :key="idx" @tap="input(ite)">{{ite}}</view></view></view><view class="btn-area"><view :class="['item','dateChoose']" hover-class="active" :hover-start-time="0" :hover-stay-time="5"@tap="dateVal"><view class="uni-input">{{choosedDateShow}}</view></view><view :class="['item','del']" hover-class="active" :hover-start-time="0" :hover-stay-time="5"@tap="deleteVal"><u-icon name="arrow-leftward"></u-icon></view><view class="confirem item" hover-class="active" :hover-start-time="0" :hover-stay-time="5"@tap="submit">完成</view><view v-if="add_or_update=='編輯賬單'" class="deletebill item" hover-class="active" :hover-start-time="0" :hover-stay-time="5"@tap="deleteBill">刪除賬單</view></view></view></view><u-picker mode="time":default-time="date_picker_date" v-model="date_picker_show" :params="date_picker_params"@confirm="date_pick_ok"></u-picker><!-- <u-modal v-model="showDeleteBillModal" :content="content" negative-top=500></u-modal> -->
</template><script setup>import {ref, defineProps, defineEmits, watch, defineModel, computed} from 'vue';import {onLoad,onUnload,onReachBottom,onShareAppMessage,onShareTimeline} from "@dcloudio/uni-app"onLoad((e)=>{});const add_or_update = defineModel("add_or_update", { type: String, default: '' });const bill_id = defineModel("bill_id", { type: String, default: '' });const item_name = defineModel("item_name", { type: String, default: '' });const bill_money = defineModel("bill_money", { type: String, default: '' });const bill_desc = defineModel("bill_desc", { type: String, default: '' });const date_picker_date = defineModel("date_picker_date", {type: String, default: ''})console.log(add_or_update.value);console.log(date_picker_date.value);console.log(bill_id.value);const numKeybordList = ref([  // 鍵盤數值[1, 2, 3],[4, 5, 6],[7, 8, 9],[0, '.']]);// 默認不顯示時間選擇組件。在點擊了“今天”之后,可以進行選擇其他日期const date_picker_show = ref(false);// 選擇時間時候的時間選擇組件,僅展示年月日const date_picker_params = ref({year: true,month: true,day: true,hour: false,minute: false,second: false});// 監聽contentId// watch(()=>props.item_name,(newValue, oldValue)=>{// 	console.log(newValue);//    item_name.value = newValue// },{ deep: true, immediate:true})/*** 按鍵* @param {Object}*/// const clickInfo = () => {const input = (val) => {// input(val) {let money = bill_money.value;if (money.length >= 10) {uni.showToast({title: '金額過大',icon: 'error'})return;}let arr = money.split('.');if (money == '0.00' && val != null) {money = val.toString();} else {let arr = money.split('.');if (val == '.' && arr.length > 1) {} else if (arr.length <= 1 || (arr.length > 1 && arr[1].length <= 1)) {if (money == '0' && val != '.') {money = val.toString();} else if (money != '0' || val != '0')money += val;}}bill_money.value = money;};/*** 刪除*/const deleteVal = () => {// deleteVal() {let money = bill_money.value; console.log(money.length);if (money != '0.00' && money.length > 0)money = money.substring(0, money.length - 1)if (money.length <= 0)money = '0.00';bill_money.value = money;};const date_pick_ok = (callback_data) => {// date_pick_ok(callback_data) {date_picker_date.value = callback_data.year + '-' + callback_data.month + '-'  + callback_data.day;console.log('選擇了:' + date_picker_date.value);};const dateVal = () => {date_picker_show.value = true;};const emit = defineEmits(['submit', 'deleteBill']);const submit = () => {emit('submit',{bill_money: bill_money.value,bill_desc: bill_desc.value,date_picker_date: date_picker_date.value},(res)=>{//回調函數的方法體.處理自己的業務.console.log("獲取到父組件執行結果的回調");console.log(res);});};const deleteBill = () => {uni.showModal({title: '提示',content: '確定要刪除此賬單嗎?',success: function (res) {if (res.confirm) {console.log('用戶點擊確定');emit('deleteBill',{bill_id: bill_id.value},(res)=>{//回調函數的方法體.處理自己的業務.console.log("獲取到父組件執行結果的回調");console.log(res);});} else if (res.cancel) {console.log('用戶點擊取消');}}});};const getTodayDateTime = () => {var date = new Date();var Y = date.getFullYear() + '-';var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1):date.getMonth()+1) + '-';var D = (date.getDate()< 10 ? '0'+date.getDate():date.getDate())+ ' ';var h = (date.getHours() < 10 ? '0'+date.getHours():date.getHours())+ ':';var m = (date.getMinutes() < 10 ? '0'+date.getMinutes():date.getMinutes()) + ':';var s = date.getSeconds() < 10 ? '0'+date.getSeconds():date.getSeconds();return Y+M+D+h+m+s;};// 一個計算屬性 refconst choosedDateShow = computed(() => {if (date_picker_date.value == getTodayDateTime().substring(0, 10)) {return '今天';}else {return date_picker_date.value;}})</script><style lang="scss">
.keyboardbox {width: 100%;position: absolute;left: 0;bottom: 0;background-color: #FFFFFF;.numkeyboard {height: 432rpx;display: flex;background-color: #ebedf0;.btn-area {width: 180rpx;height: 100%;display: flex;flex-direction: column;.item {width: 100%;display: flex;justify-content: center;align-items: center;flex-grow: 1;}.del {background-color: #ebedf0;color: #333;&.active {background-color: #f1f3f5;}}.confirem {background-color: #4fae70;color: #FFFFFF;&.active {background-color: #4fae70;}}.deletebill {background-color: #e94d26;color: #333;}.dateChoose {background-color: #f9db56;color: #2d2d2d;&.active {background-color: #f9db56;}}}.num-area {flex-grow: 1;display: flex;flex-wrap: wrap;.row {width: 100%;height: 25%;display: flex;margin-top: 1px;.item {flex-grow: 1;height: 100%;display: flex;justify-content: center;align-items: center;background-color: #FFFFFF;border-right: 1px solid #ebedf0;width: 33.33%;&.active {background-color: #ebedf0;}&.z {flex-grow: 2;width: 66.66%;}&.disabled {background: #FFFFFF;color: #B9B9B9;}}}}}}
</style>

1.4. 如何引用

我們選擇在父組件里面,通過一個u-popup來包裹此鍵盤組件,并在顯示之前,把要傳遞進去顯示的數據用v-model進行綁定:

  • 如果是添加賬單的場景:父組件傳值給子組件;子組件要調用父組件的submit方法;子組件要知道父組件submit方法的執行結果。
<u-popup v-model="popup_show" mode="bottom" border-radius="14" height="600rpx"><keyboard-info :item_name="item_name" :bill_money="bill_money" :bill_desc="bill_desc":date_picker_date="date_picker_date":add_or_update="'添加賬單'"@submit="submit"></keyboard-info></u-popup>
// 上面的item_name、bill_money、bill_desc、date_picker_date要自行處理賦值。
selectItem(index) {this.bill_money = '0.00';// 點擊彈出的金額默認還原為0this.bill_desc = '';// 理由同上this.select_item = index;this.popup_show = true;  // 展示u-popup,也就能展示出來鍵盤子組件了。if (this.current_type == 0) {this.item_name = this.expenditure_list[index-1].textthis.item_img_path = this.expenditure_list[index-1].iconthis.item_id = this.expenditure_list[index-1].id}else {this.item_name = this.income_list[index-1].textthis.item_img_path = this.income_list[index-1].iconthis.item_id = thisincome_list[index-1].id}
},
// submit方法,主要是用來給鍵盤組件在點擊鍵盤的”提交“按鈕時,將鍵盤組件錄入的數據回傳到父組件的submit方法中
// 這里要再說一下callback參數,其實是鍵盤組件為了獲取到父組件執行了submit方法之后的返回值,以便在成功執行操作之后,將鍵盤隱藏或引導頁面跳轉
submit(data, callback) {console.log('收到鍵盤子組件觸發submit方法');console.log(data);if (this.direction == '支出') {data.bill_money = -data.bill_money;}// console.log('bill_add.vue的 submit() 方法被調用');let bill_detail = {bill_id: this.direction + this.getTodayDateTime(),direction: this.direction,year: parseInt(this.date_picker_date.substring(0, 4)),month: parseInt(this.date_picker_date.substring(5,7)),day: parseInt(this.date_picker_date.substring(8,10)),week: this.getWeek(this.date_picker_date),item_name: this.item_name,item_img_path: this.item_img_path,item_id: parseInt(this.item_id),add_time: this.getTodayDateTime(),update_time: this.getTodayDateTime(),// 下面3個,是從鍵盤組件回調來的值bill_desc: data.bill_desc,bill_date: data.date_picker_date,bill_money: parseFloat(data.bill_money)}console.log(bill_detail);callback(true);
},
  • 如果是編輯賬單的場景

    <u-popup v-model="popup_show" mode="bottom" border-radius="14" height="600rpx"><keyboard-info :item_name="item_name" :bill_money="bill_money" :bill_desc="bill_desc":date_picker_date="date_picker_date":add_or_update="'編輯賬單'":bill_id="bill_id"@submit="submit"@deleteBill="deleteBill"></keyboard-info></u-popup>
    

    跟之前的分析是類似的。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/13655.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/13655.shtml
英文地址,請注明出處:http://en.pswp.cn/web/13655.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

保護共享資源的方法(互斥鎖)

我最近開了幾個專欄&#xff0c;誠信互三&#xff01; > |||《算法專欄》&#xff1a;&#xff1a;刷題教程來自網站《代碼隨想錄》。||| > |||《C專欄》&#xff1a;&#xff1a;記錄我學習C的經歷&#xff0c;看完你一定會有收獲。||| > |||《Linux專欄》&#xff1…

MagicAnimate: Temporally Consistent Human Image Animation using Diffusion Model

show lab NUS&bytedancehttps://github.com/magic-research/magic-animate 問題引入 輸入參考圖片 I r e f I_{ref} Iref?和動作序列 p 1 : N [ p 1 , ? , p N ] p^{1:N}[p_1,\cdots,p_N] p1:N[p1?,?,pN?]&#xff0c;其中 N N N表示的是幀數&#xff0c;輸出的是 …

探索iOS中的KVC

目錄 前言 1.iOS中的KVC&#xff08;鍵值編碼&#xff09; 1. 什么是KVC&#xff1f; 2. 使用KVC 1.設置屬性值 2.獲取屬性值 3. KVC的高級用法 1.訪問私有屬性 2.訪問集合屬性 4. KVC的安全性 5. KVC原理 1. 查找順序 2. 設置值 6.參考文章 前言 這篇文章主要是…

UbuntuLinux系統下安裝wrk和使用

前言 wrk是一個用c語言寫的壓力測試工具&#xff0c;非常有用&#xff0c;但是ubuntu的軟件倉庫沒有收錄wrk&#xff0c;需要我們自己進行編譯和安裝&#xff0c;最近在學習一些性能測試、性能優化方面的知識&#xff0c;需要使用到這個強有力的工具&#xff0c;故此記錄安裝和…

Windows安全應急--在應急響應中需要知道的信息

在網絡安全事件發生后&#xff0c;一般是要去客戶現場排查問題的&#xff0c; 那么要想解決問題&#xff0c;信息的完整性決定了這次任務的成敗。。 1. 你需要知道的&#xff1a; 先讓客戶梳理一遍事情的起因經過結果 詢問客戶需要解決的問題 了解客戶的網絡環境&#xff08…

【ARM 嵌入式 C 入門及漸進 6.2 -- ARMv8 C 內嵌匯編讀系統寄存器的函數實現】

請閱讀【嵌入式開發學習必備專欄】 文章目錄 ARMv8 C 內嵌匯編讀系統寄存器 ARMv8 C 內嵌匯編讀系統寄存器 要在ARMv8架構中通過C代碼和內嵌匯編來讀取系統寄存器s3_0_c15_c5_5的值&#xff0c;并將其返回&#xff0c;可以按照以下方式實現system_read_reg函數&#xff1a; #…

buuctf的RSA(二)

1.RSA 知道 flag.enc 和 pub.key&#xff0c;典型的加密、解密 將pub,key 改為pub.txt 打開后發現公鑰 在RSA公私鑰分解 Exponent、Modulus&#xff0c;Rsa公私鑰指數、系數(模數)分解--查錯網 進行解密 得到e65537 n8693448229604811919066606200349480058890565…

innerText和innerHTML的區別

innerHTML和innerText都是元素的屬性&#xff0c;通過修改這個元素的屬性可以達到修改元素內容的目的。但是二者之間略有不同。具體來說&#xff0c;它們的區別如下&#xff1a; innerHTML可以獲取或設置元素內部的HTML內容&#xff0c;包括HTML標簽&#xff0c;而innerText則…

LeetCode 79.單詞搜索

原題鏈接&#xff1a;. - 力扣&#xff08;LeetCode&#xff09; 給定一個 m x n 二維字符網格 board 和一個字符串單詞 word 。如果 word 存在于網格中&#xff0c;返回 true &#xff1b;否則&#xff0c;返回 false 。 單詞必須按照字母順序&#xff0c;通過相鄰的單元格內…

若依前后端分離版本-前后端交互整理

ruoyi-ui與后端交互 方法一&#xff1a;表單 使用 headers: {Content-Type:application/x-www-form-urlencoded}, ruoyi-ui的vue中 //ruoyi-ui的vue中定義 formData: {a: 111,b: 111,c: 1,}, //vue中方法調用 outBound() { empty(this.formData).…

6款網頁表白代碼6(附帶源碼)

6款網頁表白代碼6 前言效果圖及部分源碼1.愛心倒計時2.一起看星星3.愛心4.愛心&#xff08;有鼠標移動特效&#xff09;5.愛心&#xff08;高級效果&#xff09;6.愛心&#xff08;3D效果&#xff09; 領取源碼下期更新預報 前言 大部分人都有喜歡的人&#xff0c;學會這些表白…

藍橋杯物聯網競賽_STM32L071KBU6_關于sizo of函數產生的BUG

首先現象是我在用LORA發送信息的時候&#xff0c;左邊顯示長度是8而右邊接收到的數據長度卻是4 我以為是OLED顯示屏壞了&#xff0c;又或者是我想搞創新用了const char* 類型強制轉換數據的原因&#xff0c;結果發現都不是 void Function_SendMsg( unsigned char* data){unsi…

微軟Edge

微軟Edge瀏覽器概述 功能介紹 微軟Edge是一款基于Chromium開源項目的網頁瀏覽器&#xff0c;旨在提供更快的網頁加載速度、更高的安全性和更好的用戶體驗。它支持多種操作系統&#xff0c;包括Windows、macOS、Android和iOS&#xff0c;能夠滿足不同用戶的需求。Edge瀏覽器擁…

趕緊收藏!2024 年最常見 20道 Redis面試題(三)

上一篇地址&#xff1a;趕緊收藏&#xff01;2024 年最常見 20道 Redis面試題&#xff08;二&#xff09;-CSDN博客 五、Redis的持久化機制是什么&#xff1f; Redis 是一個高性能的鍵值存儲系統&#xff0c;支持多種類型的數據結構&#xff0c;如字符串、哈希、列表、集合、…

python數據類型之字符串

目錄 1.字符串概念和注意事項 2.字符串內置函數 3.字符串的索引、切片和遍歷 4.字符串運算符 5.字符串常用方法 性質判斷 開頭結尾判斷 是否存在某個子串 大小寫等格式轉化 子串替換 刪除兩端空白字符 格式化字符串 分割與合并 6.字符串模板 7.exec 函數 8.字符…

【Linux】-Zookeeper安裝部署[17]

簡介 apache ZooKeeper是一個分布式的&#xff0c;開放源碼的分布式應用程序協調服務&#xff0c;是Hadoop和Hbase的重要組件。它是一個為分布式應用提供一致性服務的軟件&#xff0c;提供的功能包括&#xff1a;配置維護、域名服務、分布式同步、組服務等。 除了為Hadoop和H…

2024最新 Jenkins + Docker 實戰教程(四) - 編寫自己的Springboot項目實現自動化部署

&#x1f604; 19年之后由于某些原因斷更了三年&#xff0c;23年重新揚帆起航&#xff0c;推出更多優質博文&#xff0c;希望大家多多支持&#xff5e; &#x1f337; 古之立大事者&#xff0c;不惟有超世之才&#xff0c;亦必有堅忍不拔之志 &#x1f390; 個人CSND主頁——Mi…

VMware Num Lock 總自動切換的問題解決

VMware Num Lock 總自動切換的問題解決 0. 問題描述1. 解決方法 0. 問題描述 使用 VMware 虛擬機時&#xff0c;鼠標在 VMware 和主機之間切換時&#xff0c;總是顯示 “Num Lock 開” 和 “Num Lock 關” 的提示框。 1. 解決方法 在 VMware 系統中&#xff0c;按 fn num 統…

0407放大電路的頻率響應

放大電路的頻率響應 單時間常數RC電路的頻率響應中頻響應高頻響應低頻響應全頻域響應 放大電路頻率響應概述1. 直接耦合放大電路頻域響應阻容耦合放大電路頻域響應 4.7.1 單時間常數RC電路的頻率響應 4.7.2 放大電路頻率響應概述 4.7.3 單級共射極放大電路的頻率響應 4.7.4 單級…

TOSHIBA UTLH21 屬于Unifi NV系列

TOSHIBA UTLH21 是東芝推出的一款工業控制器&#xff0c;屬于Unifi NV系列。 這款控制器代表了東芝在工業自動化領域的一次重要進步&#xff0c;它在功能和性能上都超越了現有的V系列控制器。以下是UTLH21的一些主要特點&#xff1a; 高速邏輯與控制能力&#xff1a;UTLH21具…