paging-transfer.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <template>
  2. <div class="transfer">
  3. <div class="transfer-panel">
  4. <p class="transfer-panel-header">
  5. <span>{{ titleTexts && titleTexts[0] }}</span>
  6. <span>{{ leftSelection.length }}/{{ leftTableData.length }}</span>
  7. </p>
  8. <div v-if="showQuery">
  9. <el-form :inline="true" :model="leftQueryCondition" class="query-form">
  10. <slot name="leftCondition" v-bind:scope="leftQueryCondition"></slot>
  11. <el-form-item>
  12. <el-button type="primary" @click="onLeftQuerySubmit()">{{ queryTexts[0] }}</el-button>
  13. </el-form-item>
  14. </el-form>
  15. </div>
  16. <el-table
  17. ref="leftTable"
  18. size="small"
  19. :max-height="maxHeight"
  20. :height="minHeight"
  21. :data="leftTableData"
  22. :row-key="tableRowKey"
  23. :row-style="handleRowStyle"
  24. @row-click="handleLeftRowClick"
  25. @selection-change="handleLeftSelectionChange"
  26. border
  27. stripe
  28. >
  29. <el-table-column width="40px" type="selection" :selectable="handleSelectable"></el-table-column>
  30. <el-table-column v-for="col in leftColumns" :prop="col.id" :key="col.id" :label="col.label" :width="col.width">
  31. <template slot-scope="scope">
  32. <slot v-bind:scope="{ row: scope.row, col: col }">
  33. <span>{{ scope.row[col.id] }}</span>
  34. </slot>
  35. </template>
  36. </el-table-column>
  37. </el-table>
  38. <el-pagination
  39. v-if="showPagination"
  40. @size-change="handleSizeChange"
  41. @current-change="handleCurrentChange"
  42. :current-page="pageIndex"
  43. :page-sizes="[10, 20, 50, 100]"
  44. :page-size="pageSize"
  45. :pager-count="5"
  46. :total="totalSize"
  47. layout="total, sizes, prev, pager, next"
  48. >
  49. </el-pagination>
  50. </div>
  51. <div class="transfer-buttons">
  52. <el-button type="primary" :class="buttonClasses" :disabled="disabledLeftButton" @click.native="addToRight">
  53. <span v-if="buttonTexts[0] !== undefined" class="button-text">{{ buttonTexts[0] }}</span>
  54. <i class="el-icon-arrow-right"></i>
  55. </el-button>
  56. <el-button
  57. type="primary"
  58. :class="buttonClasses"
  59. :disabled="rightSelection.length === 0"
  60. @click.native="addToLeft"
  61. >
  62. <i class="el-icon-arrow-left"></i>
  63. <span v-if="buttonTexts[1] !== undefined" class="button-text">{{ buttonTexts[1] }}</span>
  64. </el-button>
  65. </div>
  66. <div class="transfer-panel">
  67. <p class="transfer-panel-header">
  68. <span>{{ titleTexts && titleTexts[1] }}</span>
  69. <span>{{ rightSelection.length }}/{{ rightTableData.length }}</span>
  70. </p>
  71. <div v-if="showQuery">
  72. <el-form :inline="true" :model="rightQueryCondition" class="query-form">
  73. <slot name="rightCondition" v-bind:scope="rightQueryCondition"></slot>
  74. <el-form-item>
  75. <el-button type="primary" @click="onRightQuerySubmit()">{{ queryTexts[1] }}</el-button>
  76. </el-form-item>
  77. </el-form>
  78. </div>
  79. <el-table
  80. ref="rightTable"
  81. size="small"
  82. :max-height="maxHeight"
  83. :height="minHeight"
  84. :data="calcRightTableData"
  85. :row-key="tableRowKey"
  86. @row-click="handleRightRowClick"
  87. @selection-change="handleRightSelectionChange"
  88. border
  89. stripe
  90. >
  91. <el-table-column width="40px" type="selection"></el-table-column>
  92. <el-table-column
  93. v-for="col in rightColumns || leftColumns"
  94. :prop="col.id"
  95. :key="col.id"
  96. :label="col.label"
  97. :width="col.width"
  98. >
  99. <template slot-scope="scope">
  100. <slot v-bind:scope="{ row: scope.row, col: col }">
  101. <span>{{ scope.row[col.id] }}</span>
  102. </slot>
  103. </template>
  104. </el-table-column>
  105. </el-table>
  106. <el-pagination v-if="showPagination" :total="rightTableData.length" layout="total"> </el-pagination>
  107. </div>
  108. </div>
  109. </template>
  110. <script>
  111. import { EventBus } from '@/utils/eventBus'
  112. export default {
  113. name: 'pagingTransfer',
  114. props: {
  115. value: {
  116. type: Array,
  117. default() {
  118. return []
  119. }
  120. },
  121. // 显示条件查询
  122. showQuery: {
  123. type: Boolean,
  124. default: false
  125. },
  126. // 显示分页
  127. showPagination: {
  128. type: Boolean,
  129. default: false
  130. },
  131. // 左侧分页回调
  132. paginationCallBack: {
  133. type: Function,
  134. default: function () {
  135. return new Promise((resolve, reject) => {
  136. try {
  137. resolve({ total: 0, data: null })
  138. } catch {
  139. reject()
  140. }
  141. })
  142. }
  143. },
  144. // 标题文本
  145. titleTexts: {
  146. type: Array,
  147. default() {
  148. return ['待选项', '已选项']
  149. }
  150. },
  151. // 按钮文本
  152. buttonTexts: {
  153. type: Array,
  154. default() {
  155. return []
  156. }
  157. },
  158. // 查询按钮文本
  159. queryTexts: {
  160. type: Array,
  161. default() {
  162. return ['查询', '筛选']
  163. }
  164. },
  165. // 左侧参数
  166. leftColumns: {
  167. type: Array,
  168. default() {
  169. return []
  170. }
  171. },
  172. // 右侧参数
  173. rightColumns: {
  174. type: Array,
  175. default() {
  176. return undefined
  177. }
  178. },
  179. // 表格最小高度
  180. minHeight: {
  181. type: String,
  182. default: '300px'
  183. },
  184. // 表格最大高度
  185. maxHeight: {
  186. type: String,
  187. default: '500px'
  188. },
  189. // 表格行数据的Key
  190. tableRowKey: {
  191. type: Function,
  192. default(row) {
  193. return row && row && row.id
  194. }
  195. },
  196. handlePaginationCallBackKey: {
  197. type: String,
  198. default: 'handlePaginationCallBack'
  199. }
  200. },
  201. data() {
  202. return {
  203. leftTableData: [],
  204. rightTableData: this.value || [],
  205. pageIndex: 1,
  206. pageSize: 20,
  207. totalSize: 0,
  208. leftSelection: [],
  209. rightSelection: [],
  210. leftQueryCondition: {},
  211. rightQueryCondition: {},
  212. rightConditionTemp: undefined
  213. }
  214. },
  215. created() {
  216. this.handlePaginationCallBack()
  217. EventBus.$on(this.handlePaginationCallBackKey, this.handlePaginationCallBack)
  218. },
  219. beforeDestroy() {
  220. EventBus.$off(this.handlePaginationCallBackKey, this.handlePaginationCallBack)
  221. },
  222. computed: {
  223. hasButtonTexts() {
  224. return this.buttonTexts.length === 2
  225. },
  226. buttonClasses() {
  227. return ['transfer-button', { 'is-with-texts': this.hasButtonTexts }]
  228. },
  229. disabledLeftButton() {
  230. return !this.leftSelection.some(
  231. leftRow => !this.rightTableData.some(rightRow => this.checkObjectIsEqual(leftRow, rightRow))
  232. )
  233. },
  234. calcRightTableData() {
  235. if (this.showQuery && this.rightConditionTemp) {
  236. const conditionKeys = Object.keys(this.rightConditionTemp)
  237. return this.rightTableData.filter(data => {
  238. return conditionKeys.some(key => {
  239. const rowCellData = data[key]
  240. const condVal = this.rightConditionTemp[key].trim()
  241. if (rowCellData) {
  242. return String(rowCellData).indexOf(condVal) > -1
  243. }
  244. return true
  245. })
  246. })
  247. }
  248. return this.rightTableData
  249. }
  250. },
  251. methods: {
  252. handleLeftSelectionChange(selection) {
  253. this.leftSelection = selection
  254. },
  255. handleRightSelectionChange(selection) {
  256. this.rightSelection = selection
  257. },
  258. handleLeftRowClick(row) {
  259. if (!this.rightTableData.some(rightRow => this.checkObjectIsEqual(rightRow, row))) {
  260. this.$refs.leftTable.toggleRowSelection(row)
  261. }
  262. },
  263. handleRightRowClick(row) {
  264. this.$refs.rightTable.toggleRowSelection(row)
  265. },
  266. handleSizeChange(val) {
  267. this.pageSize = val
  268. this.handlePaginationCallBack()
  269. },
  270. handleCurrentChange(val) {
  271. this.pageIndex = val
  272. this.handlePaginationCallBack()
  273. },
  274. handlePaginationCallBack() {
  275. // this.showPagination &&
  276. if (this.paginationCallBack) {
  277. const condition = {
  278. pageIndex: this.pageIndex,
  279. pageSize: this.pageSize,
  280. ...this.leftQueryCondition
  281. }
  282. this.paginationCallBack.call(null, condition).then(result => {
  283. console.log(result)
  284. if (result && Array.isArray(result.data)) {
  285. this.leftTableData = result.data
  286. this.totalSize = result.total
  287. }
  288. this.$nextTick(() => {
  289. this.leftTableData.forEach(leftRow => {
  290. const isHave = this.rightTableData.some(rightRow => this.checkObjectIsEqual(rightRow, leftRow))
  291. this.$refs.leftTable.toggleRowSelection(leftRow, isHave)
  292. })
  293. })
  294. })
  295. }
  296. },
  297. handleRowStyle({ row }) {
  298. if (this.rightTableData.some(rightRow => this.checkObjectIsEqual(rightRow, row))) {
  299. return {
  300. color: 'blue'
  301. }
  302. }
  303. return {}
  304. },
  305. handleSelectable(row) {
  306. return !this.rightTableData.some(rightRow => this.checkObjectIsEqual(rightRow, row))
  307. },
  308. addToRight() {
  309. for (const item of this.leftSelection) {
  310. const isHave = this.rightTableData.some(rightRow => this.checkObjectIsEqual(rightRow, item))
  311. if (!isHave) {
  312. this.rightTableData.push(item)
  313. }
  314. }
  315. this.$emit('input', this.rightTableData)
  316. },
  317. addToLeft() {
  318. this.rightSelection.forEach(item => {
  319. const index = this.rightTableData.findIndex(rightRow => this.checkObjectIsEqual(rightRow, item))
  320. if (index !== -1) {
  321. this.rightTableData.splice(index, 1)
  322. const leftRow = this.leftTableData.find(leftRow => this.checkObjectIsEqual(leftRow, item))
  323. if (leftRow) {
  324. this.$refs.leftTable.toggleRowSelection(leftRow, false)
  325. }
  326. }
  327. })
  328. this.$emit('input', this.rightTableData)
  329. },
  330. onLeftQuerySubmit() {
  331. this.handlePaginationCallBack()
  332. },
  333. onRightQuerySubmit() {
  334. this.rightConditionTemp = JSON.parse(JSON.stringify(this.rightQueryCondition))
  335. },
  336. checkObjectIsEqual(rowObj1, rowObj2) {
  337. return this.tableRowKey(rowObj1) === this.tableRowKey(rowObj2)
  338. },
  339. clear() {
  340. this.rightTableData = []
  341. this.$refs.leftTable.clearSelection()
  342. for (const key in this.leftQueryCondition) {
  343. this.leftQueryCondition[key] = undefined
  344. }
  345. for (const key in this.rightQueryCondition) {
  346. this.rightQueryCondition[key] = undefined
  347. }
  348. this.pageIndex = 1
  349. this.handlePaginationCallBack()
  350. }
  351. }
  352. }
  353. </script>
  354. <style scoped>
  355. .transfer {
  356. font-size: 14px;
  357. display: flex;
  358. justify-content: center;
  359. align-items: center;
  360. }
  361. .el-icon-arrow-right,
  362. .el-icon-arrow-left {
  363. font-size: 40px;
  364. cursor: pointer;
  365. }
  366. .transfer-panel {
  367. border: 1px solid #ebeef5;
  368. border-radius: 4px;
  369. overflow: hidden;
  370. background: #fff;
  371. display: inline-block;
  372. width: calc((100% - 100px) / 2);
  373. max-height: 100%;
  374. box-sizing: border-box;
  375. position: relative;
  376. }
  377. .transfer-panel .transfer-panel-header {
  378. height: 40px;
  379. line-height: 40px;
  380. background: #f5f7fa;
  381. margin: 0;
  382. padding-left: 15px;
  383. border-bottom: 1px solid #ebeef5;
  384. -webkit-box-sizing: border-box;
  385. box-sizing: border-box;
  386. color: #000;
  387. }
  388. .transfer-panel-header span:last-child {
  389. position: absolute;
  390. right: 15px;
  391. }
  392. .transfer-buttons {
  393. display: inline-block;
  394. vertical-align: middle;
  395. width: 100px;
  396. }
  397. .transfer-button {
  398. display: block;
  399. margin: 0 auto;
  400. padding: 10px;
  401. border-radius: 4px;
  402. color: #fff;
  403. background-color: #409eff;
  404. font-size: 0;
  405. }
  406. .transfer-button .button-text {
  407. margin-left: 2px;
  408. margin-right: 5px;
  409. }
  410. .transfer-button.is-with-texts {
  411. border-radius: 4px;
  412. }
  413. .transfer-button.is-disabled,
  414. .transfer-button.is-disabled:hover {
  415. border: 1px solid #dcdfe6;
  416. background-color: #f5f7fa;
  417. color: #c0c4cc;
  418. }
  419. .transfer-button:first-child {
  420. margin-bottom: 10px;
  421. }
  422. .transfer-button:nth-child(2) {
  423. margin: 0 auto;
  424. }
  425. .transfer-button i,
  426. .transfer-button span {
  427. font-size: 14px;
  428. }
  429. .query-form {
  430. margin: 5px;
  431. }
  432. </style>
  433. <style>
  434. .query-form .el-form-item {
  435. margin-bottom: 0;
  436. }
  437. </style>