当前位置: 首页 > news >正文

el-table拖动排序

.<template>
  <div>
    <div style="margin-bottom: 5px" v-show="tableData.length">
      <el-button size="mini" type="danger" @click="handleAllDelete">删除选中</el-button>
    </div>
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      height="220"
      size="mini"
      :border="false"
      row-key="id"
      empty-text="暂无素材!"
      @selection-change="handleSelectionChange"
      >
      <el-table-column type="selection"></el-table-column>
      <el-table-column label="文件名"  show-overflow-tooltip>
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column label="类型" >
        <template slot-scope="scope">{{ returnType(scope.row.type) }}</template>
      </el-table-column>
      <el-table-column prop="createBy" label="创建者" >
        <template slot-scope="scope">
          {{scope.row.createBy}}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 删除提示弹窗 -->
    <el-dialog
      title="温馨提示"
      :visible.sync="deleteVisible"
      width="20%"
      append-to-body
      >
      <span>是否删除素材{{filename}}?</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleFormat">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Sortable from 'sortablejs'
export default {
  name: 'MaterialTable',
  props: {
    tableData: {
      type: Array,
      default () {
        return []
      }
    },
    control_type: {
      type: [Number, String]
    },
  },
  data () {
    return {
      deleteVisible: false,
      selectData: [],
      filename: '',
    }
  },
  watch: {
    tableData: {
      handler: function (newValue) {
        console.log('MaterialTable:', newValue)
      },
      deep: true
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.initSortable()
    })
  },
  methods: {
    // 初始化拖拽
    initSortable () {
      const el = this.$refs.multipleTable.$el.querySelectorAll('.el-table__body-wrapper > table > tbody')[0]
      this.sortable = Sortable.create(el, {
        ghostClass: 'chosen',
        animation: 150,
        onEnd: evt => {
          const oldIndex = evt.oldIndex
          const newIndex = evt.newIndex
          const temp = this.tableData[oldIndex]
          if (oldIndex < newIndex) { // 向下移动调整顺序
            for (let i = oldIndex; i < newIndex; i++) {
              this.tableData[i] = this.tableData[i + 1]
            }
          } else if (oldIndex > newIndex) { // 向上移动时调整顺序
            for (let i = oldIndex; i > newIndex; i--) {
              this.tableData[i] = this.tableData[i - 1]
            }
          }
          this.tableData[newIndex] = temp
        },
      })
    },
    returnType (type) {
      return type === 2 ? '图片' : '视频'
    },
    // 选中的素材
    handleSelectionChange (selection) {
      this.selectData = selection
    },
    handleDelete (index, row) {
      // 删除
      this.deleteVisible = true
      this.filename = row.name
      this.selectData = [row]
    },
    // 删除选中
    handleAllDelete () {
      if (!this.selectData.length) {
        this.$message.error('暂未选中素材!')
        return false
      }
      this.deleteVisible = true
    },
    handleFormat () {
      const tableData = JSON.parse(JSON.stringify(this.tableData))
      const newTableData = []
      tableData.forEach(value => {
        this.selectData.forEach(item => {
          if (value.id !== item.id) {
            newTableData.push(value)
          }
        })
      })
      this.$emit('delete:table', newTableData)
      this.deleteVisible = false
      this.filename = ''
      this.selectRowId = ''
      console.log('tableData:', tableData, newTableData)
    }
  }
}
</script>

<style scoped lang="scss">
    /deep/ .el-table__row {
      cursor: move!important;
    }
   /deep/.chosen {
       border: solid 1px #3089dc !important;
       cursor: move!important;
   }
</style>

相关文章:

  • vue项目进行前端埋点,记录页面菜单停留时间
  • input实现在移动端软键盘中显示“搜索”案件的注意事项
  • export, import 的用法详解
  • vue中ref的作用及用法
  • Vue中数据缓存localStroage
  • keep-alive实现数据缓存
  • keep-alive的使用
  • Vue项目中文件路径的引用问题
  • e.preventDefault()与e.stopPropagation()的区别
  • es6数组方法find()、findIndex()与filter()的总结
  • js数组对象(1个数组) 判断 | 两个属性值相同时,另外某个属性值相加
  • js数组对象去重(4种方法)
  • js数组对象 判断两个属性值相同时另外某个属性值相加后,再进行 去重 (项目实战提取!!!)
  • Vue2.0 $set()的正确使用详解
  • Vue的三个点es6知识,扩展运算符
  • JavaScript 如何正确处理 Unicode 编码问题!
  • Android开发 - 掌握ConstraintLayout(四)创建基本约束
  • CentOS学习笔记 - 12. Nginx搭建Centos7.5远程repo
  • Date型的使用
  • ES6 学习笔记(一)let,const和解构赋值
  • java8 Stream Pipelines 浅析
  • Javascript 原型链
  • Linux编程学习笔记 | Linux多线程学习[2] - 线程的同步
  • Swift 中的尾递归和蹦床
  • 编写高质量JavaScript代码之并发
  • 初识MongoDB分片
  • 简单数学运算程序(不定期更新)
  • 利用jquery编写加法运算验证码
  • 每个JavaScript开发人员应阅读的书【1】 - JavaScript: The Good Parts
  • 小而合理的前端理论:rscss和rsjs
  • Salesforce和SAP Netweaver里数据库表的元数据设计
  • ​HTTP与HTTPS:网络通信的安全卫士
  • ​水经微图Web1.5.0版即将上线
  • #QT(TCP网络编程-服务端)
  • #基础#使用Jupyter进行Notebook的转换 .ipynb文件导出为.md文件
  • (+3)1.3敏捷宣言与敏捷过程的特点
  • (17)Hive ——MR任务的map与reduce个数由什么决定?
  • (33)STM32——485实验笔记
  • (day6) 319. 灯泡开关
  • (阿里巴巴 dubbo,有数据库,可执行 )dubbo zookeeper spring demo
  • (笔试题)合法字符串
  • (第9篇)大数据的的超级应用——数据挖掘-推荐系统
  • (机器学习-深度学习快速入门)第三章机器学习-第二节:机器学习模型之线性回归
  • (六)库存超卖案例实战——使用mysql分布式锁解决“超卖”问题
  • (十二)devops持续集成开发——jenkins的全局工具配置之sonar qube环境安装及配置
  • (转)h264中avc和flv数据的解析
  • .equal()和==的区别 怎样判断字符串为空问题: Illegal invoke-super to void nio.file.AccessDeniedException
  • .NET MVC 验证码
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地定义和使用弱事件
  • .NET基础篇——反射的奥妙
  • .net开发时的诡异问题,button的onclick事件无效
  • .NET是什么
  • .NET学习全景图
  • /etc/sudoers (root权限管理)
  • @FeignClient注解,fallback和fallbackFactory