最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于vue手寫tree插件的那點(diǎn)事兒

 更新時(shí)間:2019年08月20日 08:28:34   作者:煙花散盡13141  
這篇文章主要給大家介紹了基于vue手寫tree插件的那點(diǎn)事兒,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

前言

Tree樹形控件在前端開發(fā)中必不可少,對(duì)于數(shù)據(jù)的展示現(xiàn)在網(wǎng)站大都采取樹形展示。因?yàn)榇髷?shù)據(jù)全部展示出來對(duì)于用戶來說是不友好的。今天我們自己手寫一個(gè)Tree插件。

iview提供的控件

iview已經(jīng)很成熟了,如果說我寫的控件和iview提供的控件誰更好,那肯定是選擇iview , 手寫控件只是為了更好的了解vue父子組件之間的通信的。 請(qǐng)讀者還是不要拿我的控件和iview或者其他第三方的去對(duì)比。下面我們先來看看iview的Tree控件如何使用

<template>
  <Tree :data="data2" show-checkbox></Tree>
</template>

<script>
  export default {
    data () {
      return {
        data2: [
          {
            title: 'parent 1',
            expand: true,
            children: [
              {
                title: 'parent 1-1',
                expand: true,
                children: [
                  {
                    title: 'leaf 1-1-1'
                  },
                  {
                    title: 'leaf 1-1-2'
                  }
                ]
              },
              {
                title: 'parent 1-2',
                expand: true,
                children: [
                  {
                    title: 'leaf 1-2-1'
                  },
                  {
                    title: 'leaf 1-2-1'
                  }
                ]
              }
            ]
          }
        ]
      }
    }
  }
</script>

上述的代碼形成的效果如下

在使用Tree控件時(shí)在Template中還有如下樹形可以使用(根據(jù)自己需求)

然后就是控件的一些事件捕獲

子節(jié)點(diǎn)的一些設(shè)置

對(duì)于iview的Tree總結(jié)就是一句話:到位!。在這里小編也推薦大家使用iview來開發(fā)。這個(gè)框架對(duì)于后端程序員來說是個(gè)福利。因?yàn)槲覀儾恍枰私馓珜I(yè)的前端的只是就能夠滿足80%的需求了。

手寫控件

同樣的我們先來看看他的用法其實(shí)和iview一樣。用我們封裝好的模板就行了。下面是做一個(gè)部門樹。部門下面掛著人員這個(gè)功能。

<zxhtree
    v-if="userChange"
    class="item"
    treekey="deptId"
    treename="deptName"
    treechildren="children"
    :model="deptData"
    :ids="sysUserRole.deptIds"
    :names="sysUserRole.deptNames"
    @keyname="selectedUserObj"
>
</zxhtree>

js就是去填補(bǔ)上述的數(shù)據(jù),比如deptData、sysUserRole這些。至于這些屬性代表什么意思我們先不著急看。先上個(gè)效果圖。

那么我們的zxhtree控件是在哪里注冊(cè)的呢,這里被我們抽離在component.js里。Vue.component('zxhtree', {});
繼續(xù)在zxhtree里看除綁定的節(jié)點(diǎn)是template: '#tree-template'。
tree-template的模板是在component.html中寫好的

<script type="text/x-template" id="tree-template">
  <div>
    <tree-item
        class="item"
        :treekey="treekey"
        v-for="(model, index) in model"
        :treename="treename"
        :treechildren="treechildren"
        :model="model"
        :ids="ids"
        :names="names"
        @keyname="selectedObj"
        @data="synchdata"
    >
    </tree-item>
  </div>
</script>

而在tree-template用到的tree-item控件才是真正的tree控件。這里是為了將樹形包裹起來,所以才包裹了一層模板。
tree-item對(duì)應(yīng)的模板代碼是

<script type="text/x-template" id="item-template">
  <ul class="ztree">
    <li class="level0" @blur="blur" @focus="focus" tabindex="0" hidefocus="true" treenode="">
      <input type="checkbox" :disabled="model.disabled" :ref="model[treename]" :checked="checkStatus" @click="selectedObj"/>
      <span title="" @click="toggle" :class="openStatus" treenode_switch=""></span>
      <a :class="selectClass" treenode_a="" onclick="" target="_blank" style="" :title="model[treename]">
        <span title="" treenode_ico="" class="button ico_open" style=""></span>
        <span @dblclick="toggle" class="node_name">{{model[treename]}}</span>
      </a>
      <tree-item
        class="item"
        v-show="open"
        v-for="(model, index) in model[treechildren]"
        :key="index"
        :model="model"
        :treekey="treekey"
        :treename="treename"
        :vistreekey="vistreekey"
        :vistreename="vistreename"
        :treechildren="treechildren"
        ref="child"
        @keyname="keyname"
      >
      </tree-item>
    </li>
  </ul>
</script>

可以很明顯的看到這里我們使用了遞歸進(jìn)行展示樹形結(jié)構(gòu)。因?yàn)闃湫谓Y(jié)構(gòu)你無法確定層級(jí)。所以在里面又使用了針對(duì)子節(jié)點(diǎn)的展示tree-item.

屬性 含義 示例
treekey 內(nèi)部樹形展示 deptId
vistreekey 樹形展示key deptId
ids 默認(rèn)顯示的數(shù)據(jù)
names 默認(rèn)顯示的數(shù)據(jù)
treename 內(nèi)部真是展示數(shù)據(jù) deptName
vistreename 樹形展示數(shù)據(jù) deptName
treechildren 當(dāng)前樹的子節(jié)點(diǎn)數(shù)據(jù)
model 當(dāng)前樹的數(shù)據(jù)
(M)keyname 用于接受返回的數(shù)據(jù)

手寫控件擴(kuò)展

控件接受數(shù)據(jù)處理邏輯

//接收到數(shù)據(jù)在外面套一層
if(this.model[this.treekey]==undefined){
  this.treekey=this.vistreekey;
}
if(this.model[this.treename]==undefined){
  this.treename=this.vistreename;
}
if (this.model.disabled == true) {
  this.model.disabled = 'disabled';
}
console.log('組件注冊(cè)了嗎');
if ((','+this.ids+',').indexOf(','+this.model[this.treekey]+',') == -1) {
  this.checkStatus = false;
  this.model.checkStatus=this.checkStatus;
} else {
  this.checkStatus=true;
  this.model.checkStatus=this.checkStatus;
  this.treekeys[this.model[this.treekey]]= this.checkStatus;
  this.treenames[this.model[this.treename]]= this.checkStatus;
  this.opt.key=this.treekeys;
  this.opt['name']=this.treenames;
}
if(this.ids!=''){
  var idarr = this.ids;
  for(var index in idarr){
    this.treekeys[idarr[index]]=true;
  }
  if (this.names.indexOf(",") == -1&&this.names!='') {
    this.treenames[this.names]=true;
  }else{
    var namearr = this.names.split(",");
    for(var index in namearr){
      this.treenames[namearr[index]]=true;
    }
  }
}

渲染默認(rèn)數(shù)據(jù)

var newOpt ={'key':{},'name':{}};
  newOpt.key = Object.assign(this.opt.key, opt.key);
  newOpt.name = Object.assign(this.opt.name, opt.name);
  var flag=false;
  for(var index in this.model[this.treechildren]){
    if(newOpt.key[this.model[this.treechildren][index][this.treekey]]!=true){
      flag=true;
    }
  }
  if(!flag){
    newOpt.key[this.model[this.treekey]]=true;
    newOpt.name[this.model[this.treename]]=true;
    this.checkStatus=true;
    this.model.checkStatus=true;
  }
  for(var key in newOpt){
    this.filterRealCheck(newOpt[key]);
  }
  this.opt=newOpt;
  this.$emit('keyname', newOpt);

選擇節(jié)點(diǎn)數(shù)據(jù)處理

if(selected instanceof MouseEvent){
  this.checkStatus=!this.checkStatus;
}else{
  this.checkStatus=selected;
}

this.model.checkStatus=this.checkStatus;
if (this.model.expected != true) {
  this.treekeys[this.model[this.treekey]]= this.checkStatus;
  this.treenames[this.model[this.treename]]= this.checkStatus;
  this.opt.key=this.treekeys;
  this.opt['name']=this.treenames;
}
for(var index in this.$refs.child){
  this.$refs.child[index].selectedObj(this.checkStatus);
}

this.$emit('keyname', this.opt);

手寫控件總結(jié)

因?yàn)楣P者是側(cè)重后端,所以前端知識(shí)不是很好,這個(gè)組件寫的也是很亂。這個(gè)組件是之前臨時(shí)寫的。里面沒有進(jìn)行系統(tǒng)的梳理,上述的邏輯也是很亂。讀者需要的可以選擇下列加入戰(zhàn)隊(duì)(#addMe)聯(lián)系我

好了,以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • Vue異步組件處理路由組件加載狀態(tài)的解決方案

    Vue異步組件處理路由組件加載狀態(tài)的解決方案

    這篇文章主要介紹了Vue異步組件處理路由組件加載狀態(tài)的解決方案,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-09-09
  • vue如何利用store實(shí)現(xiàn)兩個(gè)平行組件間的傳值

    vue如何利用store實(shí)現(xiàn)兩個(gè)平行組件間的傳值

    這篇文章主要介紹了vue如何利用store實(shí)現(xiàn)兩個(gè)平行組件間的傳值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vuex中實(shí)現(xiàn)數(shù)據(jù)狀態(tài)查詢與更改

    Vuex中實(shí)現(xiàn)數(shù)據(jù)狀態(tài)查詢與更改

    今天小編就為大家分享一篇Vuex中實(shí)現(xiàn)數(shù)據(jù)狀態(tài)查詢與更改,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Vue3學(xué)習(xí)之語法糖、箭頭函數(shù)、函數(shù)聲明詳解

    Vue3學(xué)習(xí)之語法糖、箭頭函數(shù)、函數(shù)聲明詳解

    在Vue3中箭頭函數(shù)被廣泛支持,尤其是在組合式API的上下文中,這篇文章主要給大家介紹了關(guān)于Vue3學(xué)習(xí)之語法糖、箭頭函數(shù)、函數(shù)聲明的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-08-08
  • vue中插值表達(dá)式使用的示例詳解

    vue中插值表達(dá)式使用的示例詳解

    Vue的插值表達(dá)式是一種特殊的語法,用于在模板中動(dòng)態(tài)地將數(shù)據(jù)綁定到視圖中,一般使用雙大括號(hào) ("{{ }}")將表達(dá)式包裹起來,下面我們就來根據(jù)三個(gè)案例來深入了解下插值表達(dá)式的使用吧
    2023-11-11
  • 前端在el-dialog中嵌套多個(gè)el-dialog代碼實(shí)現(xiàn)

    前端在el-dialog中嵌套多個(gè)el-dialog代碼實(shí)現(xiàn)

    最近使用vue+elementUI做項(xiàng)目,使用過程中很多地方會(huì)用到dialog這個(gè)組件,有好幾個(gè)地方用到了dialog的嵌套,下面這篇文章主要給大家介紹了關(guān)于前端在el-dialog中嵌套多個(gè)el-dialog代碼實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • vue加載完成后的回調(diào)函數(shù)方法

    vue加載完成后的回調(diào)函數(shù)方法

    今天小編就為大家分享一篇vue加載完成后的回調(diào)函數(shù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • Vue在 Nuxt.js 中重定向 404 頁面的方法

    Vue在 Nuxt.js 中重定向 404 頁面的方法

    這篇文章主要介紹了Vue在 Nuxt.js 中重定向 404 頁面的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-04-04
  • vue實(shí)現(xiàn)簡(jiǎn)單轉(zhuǎn)盤抽獎(jiǎng)功能

    vue實(shí)現(xiàn)簡(jiǎn)單轉(zhuǎn)盤抽獎(jiǎng)功能

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)簡(jiǎn)單轉(zhuǎn)盤抽獎(jiǎng)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 淺談vue 單文件探索

    淺談vue 單文件探索

    這篇文章主要介紹了淺談vue 單文件探索,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-09-09

最新評(píng)論

南丹县| 通山县| 根河市| 昔阳县| 彭阳县| 陵川县| 余江县| 全州县| 柳河县| 诸暨市| 无极县| 湘阴县| 务川| 汉中市| 宁远县| 平安县| 安宁市| 崇礼县| 鸡东县| 万宁市| 浦县| 太仆寺旗| 涞源县| 鄂伦春自治旗| 壤塘县| 白银市| 庆云县| 邵武市| 宁津县| 霍邱县| 内丘县| 合水县| 丰原市| 广灵县| 阿巴嘎旗| 墨江| 建瓯市| 河曲县| 财经| 临颍县| 平湖市|