vue?數(shù)組添加數(shù)據(jù)方式
vue 數(shù)組添加數(shù)據(jù)
數(shù)據(jù)添加分為三種方法
- 1.
unshift()
- 2.
push()
- 3.
splice()
<template> ? ?? ?<div> ?? ??? ?<ul> ?? ??? ? ?<li v-for="(time,index) of listTable" :key="index" @click="copyNew(time,index)"> ?? ??? ? ? ?{{time.id}}{{time.name1}}{{time.name2}} 添加 ?? ??? ? ?</li> ?? ??? ?</ul> ?? ?</div> </template>
<script> export default { ?? ? data(){ ?? ? ? ?return{ ?? ? ? ? ?listTable:[ ?? ? ? ? ? ?{id:'1',name1:'a1',name2:'b1'}, ?? ? ? ? ? ?{id:'2',name1:'a2',name2:'b2'}, ?? ? ? ? ? ?{id:'3',name1:'a3',name2:'b3'}, ?? ? ? ? ?], ?? ? ? ?} ?? ? ?}, ?} ?</script>
1.unshift() //數(shù)組頭部添加一條新數(shù)據(jù)
let newList = { ? ?id:'4' ? ?name1:'a4', ? ?name2:'b4', ?} this.listTable.unshift(newList) ?//unshift()在數(shù)組頭部添加一條數(shù)據(jù)? console.log(this.listTable)
2.push() //數(shù)組末端添加一條新數(shù)據(jù)
this.listTable.push(newList) ?//push()在數(shù)組末端添加一條數(shù)據(jù)? console.log(this.listTable)
3.splice() //數(shù)組操作
?copyNew(time,index){ ? ?console.log(time) ? ?let newList = { ? ? ?id:time.id, ? ? ?name1:time.name1, ? ? ?name2:time.name2, ? ?} ? ?//第一個參數(shù)為需要操作數(shù)據(jù)的下標,第二個參數(shù)為操作添加/刪除(0為添加,1為不操作,2為刪除,3為刪除多條數(shù)據(jù)),第三個參數(shù)可選 ? ?this.listTable.splice(index,0,newList)? ? ?console.log(this.listTable) ?}
4.concat() // 數(shù)組合并
let arrA = [1,2,3] let arrB = [4,5] arrA.concat(arrB) // 輸出 1,2,3,4,5 let arrC = [6,7] arrA.concat(arrB,arrC) // 輸出 1,2,3,4,5,6,7
動態(tài)向數(shù)組中添加對象(關(guān)于v-for,input和push)
核心:深拷貝
第一步:
寫在data(): 設(shè)datas數(shù)組,以及datas中需求的對象
datas: [], data_formInput: { ?? ?keyword: '',//關(guān)鍵字 ?? ?describe: '',//描述 },
第二步:(對象中的屬性,input中的數(shù)據(jù))雙向綁定
<view class="box" v-show="box_show"> ?? ?<view class="box_text">請輸入關(guān)鍵字</view><input type="text" v-model="data_formInput.keyword" /> ?? ?<view class="box_text">請輸入描述</view><input type="text" v-model="data_formInput.describe" /> ?? ?<button type="default" @click='create'>確定</button> </view>
第三步:深拷貝保存數(shù)據(jù)并置空input
create() { //這里要設(shè)一個對象來進行深拷貝才能避免每次push的時候都被最后一次提交的數(shù)據(jù)覆蓋,也可以避免置空的時候數(shù)據(jù)丟失 ?? ?let obj = { ?? ??? ?keyword: this.data_formInput.keyword, ?? ??? ?describe: this.data_formInput.describe ?? ?} ?? ?this.datas.push(obj); ?? ?this.data_formInput.keyword = ''//提交input之后置空 ?? ?this.data_formInput.describe = '' },
第四步:循環(huán)顯示剛剛input提交的數(shù)據(jù)
<button type="default" v-for="(item,index) in datas" :key='index' @click='open(item.describe)'> ??? ? {{item.keyword}} </button>
放一段完整代碼:
挺多的,實現(xiàn)了點擊添加關(guān)鍵字按鈕的時候打開輸入關(guān)鍵字和描述,提交的頁面,點擊提交的時候顯示已保存的關(guān)鍵字數(shù)據(jù)。邏輯很簡單,主要是記錄一下這里的深拷貝。
<template> ?? ?<view class=""> ?? ??? ? ?? ??? ? ?? ??? ?<!-- 這里是輸入關(guān)鍵字和描述 --> ?? ??? ?<view class="box" v-show="box_show"> ?? ??? ??? ?<view class="box_text">請輸入關(guān)鍵字</view><input type="text" v-model="data_formInput.keyword" /> ?? ??? ??? ?<view class="box_text">請輸入描述</view><input type="text" v-model="data_formInput.describe" /> ?? ??? ??? ?<button type="default" @click='create'>確定</button> ?? ??? ?</view> ?? ??? ? ?? ??? ? ?? ??? ? ?? ??? ?<!-- 這里顯示已提交的關(guān)鍵字以及添加關(guān)鍵字按鈕 --> ?? ??? ?<view v-show="button_show"> ?? ??? ??? ?<button type="default" v-for="(item,index) in datas" :key='index' ?? ??? ??? ??? ?@click='open(item.describe)'>{{item.keyword}}</button> ?? ??? ??? ?<u-popup :show="show" @close="close" mode="center" round=20 closeable=true> ?? ??? ??? ??? ?<view> ?? ??? ??? ??? ??? ?{{show_describe}} ?? ??? ??? ??? ?</view> ?? ??? ??? ?</u-popup> ?? ??? ??? ?<button type="default" @click='open_box'>添加關(guān)鍵字</button> ?? ??? ?</view> ?? ??? ? ?? ??? ? ?? ?</view> </template>
<script> ?? ?export default { ?? ??? ?data() { ?? ??? ??? ?return { ?? ??? ??? ??? ?datas: [], ?? ??? ??? ??? ?data_formInput: { ?? ??? ??? ??? ??? ?keyword: '', //關(guān)鍵字 ?? ??? ??? ??? ??? ?describe: '', //描述 ?? ??? ??? ??? ?}, ?? ??? ??? ??? ?show_describe: '', ?? ??? ??? ??? ?show: false, ?? ??? ??? ??? ?box_show: false, ?? ??? ??? ??? ?button_show: true, ?? ??? ??? ?} ?? ??? ?}, ?? ??? ?methods: { ?? ??? ??? ?create() { ?? ??? ??? ??? ?let obj = { ?? ??? ??? ??? ??? ?keyword: this.data_formInput.keyword, ?? ??? ??? ??? ??? ?describe: this.data_formInput.describe ?? ??? ??? ??? ?} ?? ??? ??? ??? ?this.datas.push(obj); ?? ??? ??? ??? ?this.data_formInput.keyword = ''//提交input之后置空 ?? ??? ??? ??? ?this.data_formInput.describe = '' ?? ??? ??? ??? ?this.box_show = false ?? ??? ??? ??? ?this.button_show = true ?? ??? ??? ?}, ?? ??? ??? ?// 打開描述 ?? ??? ??? ?open(t) { ?? ??? ??? ??? ?this.show = true ?? ??? ??? ??? ?this.show_describe = t ?? ??? ??? ?}, ?? ??? ??? ?close() { ?? ??? ??? ??? ?this.show = false ?? ??? ??? ?}, ?? ??? ??? ?open_box() { ?? ??? ??? ??? ?this.box_show = true ?? ??? ??? ??? ?this.button_show = false ?? ??? ??? ?} ?? ??? ?} ?? ?} </script>
<style scoped> ?? ?.box { ?? ??? ?width: 100%; ?? ??? ?height: 500rpx; ?? ??? ?overflow: hidden; ?? ??? ?/* margin-top: 50rpx; */ ?? ??? ?background-image: url(https://pic.imgdb.cn/item/624c0962239250f7c58f97a2.png); ?? ??? ?background-repeat: no-repeat; ?? ??? ?background-size: cover; ?? ?} ?? ?.box_text { ?? ??? ?text-align: center; ?? ??? ?margin-bottom: 20rpx; ?? ?} ?? ?input { ?? ??? ?width: 80%; ?? ??? ?height: 30rpx; ?? ??? ?border: 1rpx solid black; ?? ??? ?margin-top: 50rpx; ?? ??? ?overflow: hidden; ?? ??? ?margin: 10rpx auto; ?? ??? ?padding-left: 20rpx; ?? ??? ?font-size: 25rpx; ?? ?} </style>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue如何實現(xiàn)observer和watcher源碼解析
這篇文章主要為大家詳細介紹了vue如何實現(xiàn)observer和watcher源碼的相關(guān)資料,分析vue的observe實現(xiàn)源碼,聊聊如何一步一步實現(xiàn)$watch,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03如何巧用Vue.extend繼承組件實現(xiàn)el-table雙擊可編輯(不使用v-if、v-else)
這篇文章主要給大家介紹了關(guān)于如何巧用Vue.extend繼承組件實現(xiàn)el-table雙擊可編輯的相關(guān)資料,不使用v-if、v-else,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-06-06vue2.0+ 從插件開發(fā)到npm發(fā)布的示例代碼
這篇文章主要介紹了vue2.0+ 從插件開發(fā)到npm發(fā)布的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04vue路由第二次進入頁面created和mounted不執(zhí)行問題及解決
這篇文章主要介紹了vue路由第二次進入頁面created和mounted不執(zhí)行問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12vue3使用wangeditor封裝和自定義上傳文件官方教程
這篇文章主要為大家介紹了vue3使用wangeditor封裝和自定義上傳文件的官方教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪<BR>2023-06-06vue3+vite assets動態(tài)引入圖片的三種方法及解決打包后圖片路徑錯誤不顯示的問題
這篇文章主要介紹了vue3+vite assets動態(tài)引入圖片的幾種方式,解決打包后圖片路徑錯誤不顯示的問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03vuex存儲數(shù)組(新建,增,刪,更新)并存入localstorage定時刪除功能實現(xiàn)
這篇文章主要介紹了vuex存儲數(shù)組(新建,增,刪,更新),并存入localstorage定時刪除,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04Vue+Electron打包桌面應(yīng)用(超詳細完整教程)
這篇文章主要介紹了Vue+Electron打包桌面應(yīng)用超詳細完整教程,在這大家要記住整個項目的json文件不能有注釋,及時沒報錯也不行,否則運行命令時還是有問題,具體細節(jié)問題參考下本文詳細講解2024-02-02vue2項目使用element-ui的el-tabs組件導致瀏覽器崩潰卡死問題
這篇文章主要介紹了vue2項目使用element-ui的el-tabs組件導致瀏覽器崩潰卡死問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07