新手学习 Vue 官网的树形列表例子求助 - V2EX
chuanqirenwu

新手学习 Vue 官网的树形列表例子求助

  •  
  •   chuanqirenwu Apr 8, 2018 2593 views
    This topic created in 2962 days ago, the information mentioned may be changed or developed.

    大家好:

    最近在学习 Vue,模仿官网的树形视图列表例子练习,想为其增加一个删除列表项的功能,但是不知道该如何实现。我的代码如下:

    <script type="text/x-template" id="item-template"> <li> <div @dblclick="changeType" :class="{bold: isFolder}"> {{ model.name }} <span @click="toggle" v-if="isFolder" >[ {{ open ? '-' : '+' }} ] </span> // 想点击这个按钮后删除对应的列表项 <span @click="deleteItem" v-if="!isFolder" > [x] </span> </div> <ul v-show="open" v-if="isFolder"> <item v-for="(m, index) in model.children" :model="m" :key="index" /> <li @click="add">+</li> </ul> </li> </script> Vue.component('item', { template: '#item-template', props: { model: Object }, data: function () { return {open: false} }, computed: { isFolder: function () { return this.model.children } }, methods: { toggle: function () { this.open = !this.open }, add: function () { this.model.children.push({name: 'new stuff'}) }, changeType: function () { if (!this.isFolder) { Vue.set(this.model, 'children', []); this.add() } }, deleteItem: function () { // 不知道该如何删除被点击的按钮对应的列表项 } } }); 

    谢谢大家的指点!

    2 replies    2018-04-08 18:30:29 +08:00
    overflowHidden
        1
    overflowHidden  
       Apr 8, 2018
    删除事件可以传一个删除项的 index 进去,model.children 去掉相应项( splice), 重新渲染就 ok 了。建议把删除事件绑定到列表项上。
    chuanqirenwu
        2
    chuanqirenwu  
    OP
       Apr 8, 2018 via Android
    @overflowHidden 绑定到列表项点击整个列表都会删除,这不是想要的结果吧?

    我看看 index 能否获取。

    谢谢你的指点!
    About     Help     Advertise     Blog     API     FAQ     Solana     5692 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 32ms UTC 07:39 PVG 15:39 LAX 00:39 JFK 03:39
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86