欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ElementUI中el-tree節(jié)點(diǎn)的操作的實(shí)現(xiàn)

 更新時(shí)間:2020年02月27日 11:35:26   作者:java_xxxx  
這篇文章主要介紹了ElementUI中el-tree節(jié)點(diǎn)的操作的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

其實(shí)tree的有些方法用起來(lái)是很方便的,
this.$refs.tree.getCheckedKeys();這個(gè)原生態(tài)的方法。官方文檔上說(shuō)的是,返回一個(gè)數(shù)組。有了這個(gè)方法,我們就可以得到選中的每個(gè)節(jié)點(diǎn)的id,拿到了id,那所有的問(wèn)題就迎刃而解了。
廢話不多說(shuō),直接上代碼

html

<div id="app">
  <el-row>
    <el-button @click="checkedKeys">得到節(jié)點(diǎn)id</el-button>
    <el-button @click="addNode">添加節(jié)點(diǎn)</el-button>
    <el-button @click="checkedKeys">修改節(jié)點(diǎn)</el-button>
    <el-button @click="deleteNode">刪除節(jié)點(diǎn)</el-button>
    <br/>
    <br/>

    <el-tree
         ref="tree"
         :data="treeList"
         :props="defaultProps"
         @node-click="handleNodeClick"
         show-checkbox=true
         node-key="id"
         :check-strictly="true"
        >
    </el-tree>
    <el-input>輸入框</el-input>
    <el-dialog title="添加"
         :visible.sync="dialogVisible"
        >
      <el-form ref="form">
        <el-form-item label="節(jié)點(diǎn)父類型">
        <el-select placeholder="請(qǐng)選擇要添加節(jié)點(diǎn)的父節(jié)點(diǎn)" v-model="treeNode.parentId" @change="selectChange" >
          <el-option label="根節(jié)點(diǎn)" :value="0"></el-option>
          <el-option v-for="item in treeListData" :label="item.categoryName" :value="item.nodeId"></el-option>
        </el-select>
      </el-form-item>
          <el-form-item label="節(jié)點(diǎn)名稱">
            <el-input placeholder="請(qǐng)輸入節(jié)點(diǎn)名稱" v-model="treeNode.categoryName" style="width: 220px"></el-input>
          </el-form-item>
    </el-form>

      <span>
        <el-button @click="cancleSaveNode">
          取消
        </el-button>
        <el-button @click="saveNode">
          確定
        </el-button>
      </span>

    </el-dialog>

  </el-row>

</div>

js代碼

<script type="text/javascript">


  var _treeNode={
    nodeId:0,
    categoryName:"",
    parentId:0
  }

  var app = new Vue({
    el:'#app',
    data:{
      treeNode:_treeNode,
      treeList:[],
      treeListData:[], // 無(wú)層級(jí)結(jié)構(gòu)節(jié)點(diǎn)數(shù)據(jù)
      defaultProps:{
        children: 'childList',
        label: 'name'
        /* label: 'categoryName'*/
      },
      dialogVisible:false, // 對(duì)話框,默認(rèn)不打開(kāi)
      api:{
        treeDataList:"/category/treeList.do",  // 得到節(jié)點(diǎn)數(shù)據(jù),無(wú)層級(jí)結(jié)構(gòu) GET
        saveTreeNode:"/category/saveTreeNode.do",  // 得到節(jié)點(diǎn)數(shù)據(jù),無(wú)層級(jí)結(jié)構(gòu) GET
        deleteTreeNode:"/category/deleTreeNode.do",
      }
    },
    methods: {
      cateListFunction: function () {
        $.ajax({
          type: "get",
          url: "/category/cateList.do",
          success: function (result) {
            app.cateList = result;
            app.treeList=result;
          }, error: function (result) {
          }
        });
      },
      // 點(diǎn)擊節(jié)點(diǎn)名稱觸發(fā)的事件
      handleNodeClick: function (data) {
        alert(data.id);
        console.log(data);
      },
      // 獲得選中的節(jié)點(diǎn)的key
      checkedKeys:function (data) {
        alert(JSON.stringify(this.$refs.tree.getCheckedKeys()));
      },
      // 添加節(jié)點(diǎn)查詢所有節(jié)點(diǎn)的方法
      addNode:function () {
        // 查詢所有節(jié)點(diǎn)數(shù)據(jù)無(wú)層級(jí)結(jié)構(gòu)
        app.getTreeNode();
        app.dialogVisible=true;
      },
      // 保存節(jié)點(diǎn)
      saveNode:function () {
       //alert(app.treeNode.categoryName+app.treeNode.parentId);
       axios.post(app.api.saveTreeNode,app.treeNode).then(function (resule) {
         app.dialogVisible=false;
         app.treeNode.parentId=0;
         app.treeNode.categoryName="";
         app.cateListFunction();
       });

      },
      cancleSaveNode:function () {
        app.dialogVisible=false;
        app.treeNode.parentId=0;
        app.treeNode.categoryName="";

      },
      // 下拉框選中事件
      selectChange:function (val) {
        // select控件的option綁定的值為節(jié)點(diǎn)的id,我們將值綁定在要添加的元素的父id
        //alert("得到節(jié)點(diǎn)類型"+val);
      },
      // 批量刪除節(jié)點(diǎn)(若有子節(jié)點(diǎn)則不予刪除)
      deleteNode:function () {
        app.getTreeNode();
        var str=[];
        str =this.$refs.tree.getCheckedKeys();
         if(str.length<=0){ // 因?yàn)関ue返回的是選中的key的數(shù)組
                    // ,如果沒(méi)有選擇任何元素返回 "[]",這是兩個(gè)元素,所以我們判斷長(zhǎng)度為2時(shí),為空
           app.$message.error("請(qǐng)至少選擇一個(gè)節(jié)點(diǎn)");
           return ;
         }
        for(var i in str){
          for(var j in app.treeListData){
            if(app.treeListData[j].parentId == str[i]){
              app.$message.error("父節(jié)點(diǎn)不可被刪除");
              return;
            }
          }
        }
        axios.post(this.api.deleteTreeNode,str).then(function (result) {
          app.$message({message: '刪除成功', type: 'success'});
          app.getTreeNode();
          app.cateListFunction();
        });

      },getTreeNode:function () {
        // 查詢所有節(jié)點(diǎn)數(shù)據(jù)無(wú)層級(jí)結(jié)構(gòu)
        axios.get(this.api.treeDataList)
          .then(
            function(result){
              // vue給我們封裝的得,我們所得到的數(shù)據(jù)在返回的對(duì)象的data屬性里
              app.treeListData=result.data;
            });
      }

    },
      created: function () {
        this.getTreeNode();
        this.cateListFunction();
      }
  });
</script>

controller

/**
   * 保存節(jié)點(diǎn)
   */
  @RequestMapping("/saveTreeNode.do")
  @ResponseBody
  public void saveTreeNode(@RequestBody TbCategory category){
    System.out.println(category.getCategoryName() +"------" +category.getParentId());
    categoryService.insert(category);
  }


  @RequestMapping("/deleTreeNode.do")
  @ResponseBody
  public void deleTreeNode(@RequestBody String str){

    System.out.println(str);
    String [] strIds = str.substring(1,str.length()-1).split(",");
    for(int i =0;i<strIds.length;i++){
      categoryService.delete(Long.valueOf(strIds[i])); // 1,2
    }
  }

以上代碼真的沒(méi)有什么好解釋的,大家直接按照我的代碼,就不會(huì)有問(wèn)題了

截圖:

到此這篇關(guān)于ElementUI中el-tree節(jié)點(diǎn)的操作的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)ElementUI el-tree節(jié)點(diǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論