vue 拖拽

来源:https://www.cnblogs.com/nelsonlei/archive/2022/05/30/16327043.html
-Advertisement-
Play Games

1、左右容器拖拽 2、容器內位置拖拽切換 3、判斷性拖拽(若不適配,滑鼠顯示禁止拖拽圖標) 4、點擊大標題,變輸入框修改,最後提交表單 <template> <div> <div class="left_list"> <div class="_flex"> <transition-group tag ...


1、左右容器拖拽

2、容器內位置拖拽切換

3、判斷性拖拽(若不適配,滑鼠顯示禁止拖拽圖標)

4、點擊大標題,變輸入框修改,最後提交表單

 

<template>
  <div>
    <div class="left_list">
      <div class="_flex">
        <transition-group tag="div">
          <div
            class="item list_wrap"
            :class="'item' + index"
            v-for="(item, index) in dataList"
            :key="index"
            v-if="item.key!='box'"
            @drop='onDrop($event, item.key)'
            @dragover.prevent="dragOver($event, item,index)"
            @dragenter.prevent
          >
            <h2 v-on:dblclick="editTitle(index,true)" v-show="!item.showTitleEdit">{{ item.title }}</h2>
            <Input v-model="item.title" :autofocus="true" :ref="'editInput'+index" v-show="item.showTitleEdit"
                   @on-blur="editTitle(index,false)"/>
            <p v-if="item.children&&item.children.length>0" v-for="(row,_index) in item.children" :key="_index"
               draggable="true"
               @dragstart="handleDragStart($event, item,index,_index)"
               @dragover.prevent="handleDragOver($event, item,index,_index)"
               @dragenter="handleDragEnter($event, item,index,_index)"
               @dragend="handleDragEnd($event, item,index,_index)">
              {{ row.lable }}
              <Icon @click="deleteItem(index,_index)" type="md-close-circle"/>
            </p>

          </div>
        </transition-group>
      </div>
      <div class="_flex">
        <transition-group tag="div">
          <div
            class="item list_wrap"
            :class="'item' + index"
            v-for="(item, index) in dataList"
            :key="index"
            v-if="item.key=='box'"
            @drop='onDrop($event, item.key)'
            @dragover.prevent="dragOver($event, item,index)"
            @dragenter.prevent
          >
            <h2 v-on:dblclick="editTitle(index,true)" v-show="!item.showTitleEdit">{{ item.title }}</h2>
            <Input v-model="item.title" :autofocus="true" :ref="'editInput'+index" v-show="item.showTitleEdit"
                   @on-blur="editTitle(index,false)"/>
            <p v-if="item.children&&item.children.length>0" v-for="(row,_index) in item.children" :key="_index"
               draggable="true"
               @dragstart="handleDragStart($event, item,index,_index)"
               @dragenter="handleDragEnter($event, item,index,_index)"
               @dragend="handleDragEnd($event, item,index,_index)">
              {{ row.lable }}
              <Icon @click="deleteItem(index,_index)" type="md-close-circle"/>
            </p>
          </div>
        </transition-group>
      </div>
    </div>
    <div class="right_list">
      <h1>檢測限</h1>
      <div class="_list" v-for="(item,index) in rightList" :key="index">
        <h2>{{ item.title }}</h2>
        <p v-if="item.children&&item.children.length>0" v-for="(row,_index) in item.children" :key="_index"
           draggable
           @dragstart='startDrag($event, item,index,_index)'
           @dragend="dragEnd($event, item,index,_index)">{{ row.lable }}</p>
      </div>
    </div>
    <Button @click="getD">按鈕</Button>
  </div>
</template>

<script>
export default {
  name: 'test2',
  data () {
    return {
      dataList: [
        {
          title: '電源狀態',
          key: 'power',
          showTitleEdit: false,
          children: [{lable: '電壓(V)'}, {lable: '總電量'}, {lable: '電流(A)'}, {lable: '電壓故障次數'}, {lable: '漏電流(mA)'}, {lable: '電流故障次數'}]
        },
        {
          title: '環境',
          key: 'surroundings',
          showTitleEdit: false,
          children: [{lable: '溫度1'}, {lable: '溫度2'}, {lable: '濕度'}, {lable: '雷擊次數'}]
        },
        {
          title: 'IO輸出',
          key: 'IOOutput',
          showTitleEdit: false,
          children: [{lable: '主繼電器'}, {lable: '報警'}, {lable: 'LED'}, {lable: '風扇'}, {lable: '頻閃燈'}]
        },
        {
          title: '箱體佈防狀態',
          key: 'box',
          showTitleEdit: false,
          children: [{lable: '空開檢測'}, {lable: '壓敏監測'}, {lable: '門磁監測'}, {lable: '傾斜監測'}, {lable: '水浸監測'}, {lable: '雷擊監測'}]
        }
      ],
      rightList: [
        {
          title: 'IO輸入',
          key: 'surroundings',
          children: [{lable: '電壓11111'}, {lable: '總電量1111'}, {lable: '電流111(A)'}, {lable: '電壓故障次數1111'}, {lable: '漏電流111(mA)'}, {lable: '電流故障次數111'}]
        },
        {
          title: '模擬量',
          key: 'box',
          children: [{lable: '溫度133333'}, {lable: '溫度233333'}, {lable: '濕度33333'}, {lable: '雷擊次數33333'}]
        },
        {
          title: '電壓值',
          key: 'power',
          children: [{lable: '主繼電器2222'}, {lable: '報警2222'}, {lable: 'LED222'}, {lable: '風扇2222'}, {lable: '頻閃燈222'}]
        }
      ],
      ending: null,
      dragging: null,
      newDrag: true,
      parentDragging: null
    }
  },
  methods: {
    // 父拖動
    startDrag (event, item, index, _index) {
      event.dataTransfer.dropEffect = 'move'
      event.dataTransfer.effectAllowed = 'move'
      this.newDrag = true
      this.parentDragging = Object.assign({}, item, {itemIndex: index, rowIndex: _index})
      this.dragging = null
      this.ending = null
    },
    dragOver(event, item, index) {
      // event.dataTransfer.dropEffect = 'move'
      if (this.newDrag) {
        //判斷父類移入
        event.dataTransfer.dropEffect = this.parentDragging.key == item.key ? 'move' : 'none'
      }
    },
    dragEnd (event, item, index, _index) {
      this.newDrag = true
    },
    onDrop (event, list) {
      if (list == this.parentDragging.key) {
        for (let item of this.dataList) {
          if (item.key == this.parentDragging.key) {
            item.children.push(this.rightList[this.parentDragging.itemIndex].children[this.parentDragging.itemIndex])
            break
          }
        }
      } else {
        if (this.newDrag) {
          // 不是左邊拖動
          this.$Message.error('當前key不是同一個')
        }
      }
    },

    // 子拖動
    handleDragStart (e, item, index, _index) {
      this.newDrag = false
      this.parentDragging = null
      this.dragging = Object.assign({}, item, {index: index, _index: _index})
    },
    handleDragEnd (e, item) {
      if (this.ending.key != this.dragging.key) {
        return
      }
      let sourceIndex = this.dragging._index
      let targetIndex = this.ending._index
      let parentInex = this.dragging.index
      this.dataList[parentInex].children[sourceIndex] = this.dataList[parentInex].children.splice(targetIndex, 1, this.dataList[parentInex].children[sourceIndex])[0]
    },
    handleDragOver (e, item, index, _index) {
      // e.dataTransfer.dropEffect = 'move'
      //判斷子類移入
      if (this.dragging) {
        if (this.ending.key != this.dragging.key) {
          e.dataTransfer.dropEffect = 'none'
        } else {
          e.dataTransfer.dropEffect = 'move'
        }
      }
    },
    handleDragEnter (e, item, index, _index) {
      // 為需要移動的元素設置dragstart事件
      e.dataTransfer.effectAllowed = 'move'
      this.ending = Object.assign({}, item, {index: index, _index: _index})
    },

    editTitle (index, bool) {
      this.dataList[index].showTitleEdit = bool
      this.$nextTick(() => {
        this.$refs['editInput' + index][0].focus()
      })
    },
    deleteItem (index, rowIndex) {
      this.dataList[index].children.splice(rowIndex, 1)
    },
    getD () {
      console.log(this.dataList)
    }
  }
}
</script>

<style scoped lang="less">
.right_list, .left_list {
  width: 49%;
  padding: 10px;
  display: inline-block;
  vertical-align: top;

  h2 {
    font-size: 1.5em;
    margin: 20px 20px 10px;
  }

  p {
    padding: 10px;
    display: inline-block;
    width: 48%;
    text-align: center;
    cursor: pointer;
  }
}

.left_list {
  background: #bcf5de;

  ._flex {
    margin: 10px;
    display: inline-block;
    width: 45%;
    vertical-align: top;
  }

  .list_wrap {
    margin-bottom: 10px;
    border: 1px solid red;
  }
}

.right_list {
  background: #fdbfab;
}
</style>
View Code

 

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 原文地址:Android 實現開機自啟APP - Stars-One的雜貨小窩 公司有個項目,需要實現自啟動的功能,本來想著是設置桌面啟動器的方式去實現,但是設備是華為平板(EMUI系統),不允許設置第三方桌面 且監聽開機廣播也無效,本來以為沒法實現了,沒想到公司的另一款APP確實支持,於是便是研究 ...
  • 日常出行中,路徑規劃是很重要的部分。用戶想要去往某個地點,獲取到該地點的所有路徑,再根據預估出行時間自行選擇合適的路線,極大方便出行。平時生活中也存在大量使用場景,在出行類App中,根據乘客的目的地可以為用戶規劃合適出行路線和預計到達時間;便捷生活類App中,用戶可以搜索指定範圍內的周邊服務,查看去 ...
  • 一,引入中國地圖 <div id="chinaMap"></div> import china from 'echarts/map/js/china.js' export default { data() { return { myChart: {}, echarts:echarts, }; }, ...
  • Bootstrap Bootstrap是Twitter推出的一個用於前端開發的開源工具包。它由Twitter的設計師Mark Otto和Jacob Thornton合作開發,是一個CSS/HTML框架。目前,Bootstrap最新版本為4.4。 註意,Bootstrap有三個大版本分別是 v2、v3 ...
  • jQuery的操作標簽 文本操作 $("選擇符").html() // 讀取指定元素的內容,如果$()函數獲取了有多個元素,則提取第一個元素 $("選擇符").html(內容) // 修改內容,如果$()函數獲取了多個元素, 則批量修改內容 $("選擇符").text() // 效果同上,但是獲取的 ...
  • #snail-player-native 一個純原生代碼編寫的h5視頻播放器, 功能完善,基本滿足使用,僅供學習,禁止商用 演示 演示加速 Install 1. git clone https://github.com/snail-boy/snail-player-native.git 2. 拷貝l ...
  • uni-simple-router 專為uniapp打造的路由器,和uniapp深度集成 通配小程式、App和H5端 H5能完全使用vue-router開發 模塊化、查詢、通配符、路由參數 使 uni-app實現嵌套路由(僅H5端完全使用vue-router) uniapp用到了很多vue的api, ...
  • 一、傳統的props 通過在父組件中給子組件傳值,然後在子組件中通過props接受數據 1 //父組件 2 <ValidateInput 3 type="text" 4 v-model="emailVal" 5 :rules='emailRules' 6 placeholder='請輸入郵箱地址' ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...