当前位置:   article > 正文

【element-ui】前端使用el-table自定义表头数据左右移动_elementui表格数据左移右移

elementui表格数据左移右移

一、模拟假数据

  1. <template>
  2. <div class="about">
  3. <h1>
  4. <button>导入</button>
  5. <el-checkbox-group v-model="form.checkedCities" @change="handleCheckedCitiesChange">
  6. <el-checkbox v-for="city in cities" :label="city.value" :value="city.value" :key="city.value">{{city.name}}</el-checkbox>
  7. </el-checkbox-group>
  8. </h1>
  9. <br>
  10. <div class="box2">
  11. <el-table :data="tableData" @selection-change="handleSelectionChange">
  12. <el-table-column type="selection" width="55">
  13. </el-table-column>
  14. <el-table-column prop="date" label="日期">
  15. </el-table-column>
  16. <el-table-column prop="name" label="姓名">
  17. </el-table-column>
  18. <el-table-column prop="address" label="地址">
  19. </el-table-column>
  20. </el-table>
  21. <div style="padding-top:100px;" class="center-box">
  22. <p>
  23. <el-button @click="handlerRight">向右移动</el-button>
  24. </p>
  25. <p>
  26. <el-button @click="handlerLeft">向左移动</el-button>
  27. </p>
  28. </div>
  29. <el-table :data="rightData" @selection-change="handleSelectionChange1">
  30. <el-table-column type="selection" width="55">
  31. </el-table-column>
  32. <el-table-column prop="date" label="日期">
  33. </el-table-column>
  34. <el-table-column prop="name" label="姓名">
  35. </el-table-column>
  36. <el-table-column prop="address" label="地址">
  37. </el-table-column>
  38. </el-table>
  39. </div>
  40. <el-button type="success" style="margin-top:20px" @click="handelerTop">提交表头数据</el-button>
  41. </div>
  42. </template>
  43. <script>
  44. export default {
  45. data() {
  46. return {
  47. form: {
  48. checkedCities: [2, 3]
  49. },
  50. tableData: [],
  51. rightData: [],
  52. // checkedCities: [],
  53. cities: [{ name: '北京', value: 2 }, { name: '上海', value: 5 }, { name: '广州', value: 6 }, { name: '深圳', value: 8 }],
  54. selectLeft: [],
  55. selectRight: [],
  56. };
  57. },
  58. created() {
  59. let data = [{
  60. id: 21,
  61. date: '2016-05-02',
  62. name: '008',
  63. address: '上海市普陀区金沙江路 1518 弄'
  64. }, {
  65. id: 22,
  66. date: '2016-05-04',
  67. name: '009',
  68. address: '上海市普陀区金沙江路 1517 弄'
  69. }, {
  70. id: 30,
  71. date: '2016-05-01',
  72. name: '100',
  73. address: '上海市普陀区金沙江路 1519 弄'
  74. }, {
  75. id: 25,
  76. date: '2016-05-03',
  77. name: '101',
  78. address: '上海市普陀区金沙江路 1516 弄'
  79. }]
  80. this.tableData = data
  81. let rightdata = [{
  82. id: 0,
  83. date: '2016-05-02',
  84. name: '001',
  85. address: '上海市普陀区金沙江路 1518 弄'
  86. }, {
  87. id: 1,
  88. date: '2016-05-04',
  89. name: '002',
  90. address: '上海市普陀区金沙江路 1517 弄'
  91. }, {
  92. id: 2,
  93. date: '2016-05-01',
  94. name: '003',
  95. address: '上海市普陀区金沙江路 1519 弄'
  96. }, {
  97. id: 3,
  98. date: '2016-05-03',
  99. name: '004',
  100. address: '上海市普陀区金沙江路 1516 弄'
  101. }]
  102. this.rightData = rightdata
  103. },
  104. methods: {
  105. handleCheckedCitiesChange(value) {
  106. console.log(value);
  107. },
  108. handleSelectionChange(val) {
  109. this.selectLeft = val
  110. },
  111. handleSelectionChange1(val) {
  112. this.selectRight = val
  113. },
  114. handlerRight() {
  115. if (this.tableData.length === 0) {
  116. this.$message.warning('数据为空,无法右移')
  117. return
  118. }
  119. if (this.selectLeft.length === 0) {
  120. this.$message.error('请先选择左侧数据')
  121. return
  122. }
  123. this.selectLeft.forEach((item) => {
  124. this.rightData.push(item)
  125. this.tableData.forEach((item1, index) => {
  126. if (item1.id == item.id) {
  127. this.tableData.splice(index, 1)
  128. }
  129. })
  130. })
  131. let tempObj = {}
  132. let arr = this.tableData.reduce((it, next) => {
  133. tempObj[next.name] ? '' : tempObj[next.name] = true && it.push(next)
  134. return it
  135. }, [])
  136. this.tableData = arr
  137. this.selectRight = []
  138. },
  139. handlerLeft() {
  140. if (this.rightData.length === 0) {
  141. this.$message.warning('数据为空,无法左移')
  142. return
  143. }
  144. if (this.selectRight.length === 0) {
  145. this.$message.error('请先选择右侧数据')
  146. return
  147. }
  148. this.selectRight.forEach((item) => {
  149. this.tableData.push(item)
  150. this.rightData.forEach((item1, index) => {
  151. if (item1.id == item.id) {
  152. this.rightData.splice(index, 1)
  153. }
  154. })
  155. })
  156. let tempObj = {}
  157. let arr = this.rightData.reduce((it, next) => {
  158. tempObj[next.name] ? '' : tempObj[next.name] = true && it.push(next)
  159. return it
  160. }, [])
  161. this.rightData = arr
  162. this.selectRight = []
  163. },
  164. handelerTop() {
  165. let objData = []
  166. this.rightData.map((item, index) => {
  167. objData.push({ id: index, date: item.date, name: item.name, address: item.address })
  168. })
  169. console.log(objData);
  170. }
  171. }
  172. };
  173. </script>
  174. <style lang="less" scoped>
  175. .box2 {
  176. display: flex;
  177. padding: 0 30px;
  178. .center-box {
  179. cursor: pointer;
  180. }
  181. }
  182. </style>

 二、展示 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/383559?site
推荐阅读
相关标签
  

闽ICP备14008679号