詳解Vue2 無限級分類(添加,刪除,修改)
更新時間:2017年03月07日 16:06:48 作者:木子煒培先生
本篇文章主要介紹了詳解Vue2 無限級分類(添加,刪除,修改) ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
本人對vue不是很懂,搜索了很多關(guān)于Vue2 無限級分類介紹,下面我來記錄一下,有需要了解Vue2 無限級分類的朋友可參考。希望此文章對各位有所幫助。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Vue 樹</title> <script src="https://unpkg.com/vue/dist/vue.js"></script> <style type="text/css"> a{color: #333; text-decoration: none;} ul{padding-left: 15px;} </style> </head> <body> <div id="app"> {{items}} <treelist v-for="(val, index) in items" :item="val" @remove="delItem(index)"></treelist> </div> <template id="treelist-template"> <ul> <div style="padding:5px 0;"> <a v-if="isFolder" @click="toggle()">[{{open ? '-' : '+'}}]</a> <a v-else style="color:#FFF;">[+]</a> <input type="number" style="width:80px;" v-model="item.sort"> <input type="text" size="30" v-model="item.name" placeholder="崗位名稱"> <button type="button" @click="addChild()">添加</button> <button type="button" @click="$emit('remove')" v-if="!isFolder">刪除</button> </div> <ul v-show="open" v-if="isFolder"> <treelist v-for="(val, index) in item.children" :item="val" @remove="delItem(index)"></treelist> </ul> </ul> </template> <script> window.onload = function(){ //treelist組件 Vue.component('treelist', { template: '#treelist-template', props: { item: Object }, data: function() { return { open: false } }, computed: { isFolder: function() { return this.item.children && this.item.children.length } }, methods: { toggle: function() { if (this.isFolder) { this.open = !this.open } }, addChild: function() { /*添加內(nèi)容但不同步到服務(wù)器*/ if (!this.isFolder) { Vue.set(this.item, 'children', []) } this.open = true this.item.children.push({ sort: 0, name: '', status: 1, parent_id: this.item['id'] }) }, delItem: function(index){ this.item['children'].splice(index, 1) } } }) new Vue({ el: '#app', data:{ mydata: {}, items: [ {"id":"10","parent_id":"0","sort":"0","name":"其它","status":"0"}, {"id":"12","parent_id":"0","sort":"0","name":"測試","status":"0"}, {"id":"1","parent_id":"0","sort":"0","name":"水果","status":"0", "children":[ {"id":"4","parent_id":"1","sort":"0","name":"香蕉","status":"0"} ] }, {"id":"2","parent_id":"0","sort":"0","name":"飲料","status":"0", "children":[ {"id":"5","parent_id":"2","sort":"0","name":"可樂","status":"0"}, {"id":"6","parent_id":"2","sort":"0","name":"酒水","status":"0", "children":[ {"id":"7","parent_id":"6","sort":"0","name":"啤酒","status":"0"} ] } ] }, {"id":"3","parent_id":"0","sort":"0","name":"美食","status":"0", "children":[ {"id":"8","parent_id":"3","sort":"0","name":"紅燒魚","status":"0"} ] } ] }, methods: { add:function(){ this.mydata['id'] = 100;//從服務(wù)器返回的ID號 this.mydata['status'] = 0; this.mydata['parent_id'] = 0; this.items.push(this.mydata); this.mydata = {}; }, delItem: function(index){ this.items.splice(index, 1) } } }); } </script> </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何使用vue-json-viewer插件展示JSON格式數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于如何使用vue-json-viewer插件展示JSON格式數(shù)據(jù)的相關(guān)資料,Vue-json-viewer是一個Vue組件,用于在Vue應(yīng)用中顯示JSON數(shù)據(jù)的可視化工具,需要的朋友可以參考下2023-11-11vue click.stop阻止點擊事件繼續(xù)傳播的方法
今天小編就為大家分享一篇vue click.stop阻止點擊事件繼續(xù)傳播的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09使用vue-antDesign menu頁面方式(添加面包屑跳轉(zhuǎn))
這篇文章主要介紹了使用vue-antDesign menu頁面方式(添加面包屑跳轉(zhuǎn)),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01在Vue3中實現(xiàn)子組件向父組件傳遞數(shù)據(jù)的代碼示例
Vue3作為目前最熱門的前端框架之一,以其輕量化、易用性及性能優(yōu)勢吸引了大量開發(fā)者,在開發(fā)過程中,不可避免地需要在組件之間傳遞數(shù)據(jù),本文將詳細講解在Vue3中如何實現(xiàn)子組件向父組件傳遞數(shù)據(jù),并通過具體示例代碼使概念更加清晰2024-07-07vue實現(xiàn)el-select默認選擇第一個或者第二個
這篇文章主要介紹了vue實現(xiàn)el-select默認選擇第一個或者第二個,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09vue中如何使用echarts動態(tài)渲染數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于vue中如何使用echarts動態(tài)渲染數(shù)據(jù)的相關(guān)資料,echarts是一款基于JavaScript的開源可視化圖表庫,它通過簡單的配置即可實現(xiàn)各種各樣的可視化效果,需要的朋友可以參考下2023-11-11