main.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <template>
  2. <div class="infinite-transfer" :style="{ height: height + 'px' }">
  3. <div class="infinite-list-main">
  4. <div class="search-input-main">
  5. <el-checkbox style="margin-right: 10px" :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
  6. <el-input v-model="keyword" placeholder="请输入内容" prefix-icon="el-icon-search" clearable size="small" @input="searchEvent">
  7. </el-input>
  8. </div>
  9. <div v-if="infiniteListStatus" class="infinite-list" v-infinite-scroll="onload" :infinite-scroll-disabled="scrollDisabled">
  10. <el-checkbox v-for="(item, index) in dataList" v-model="item.ischecked" :label="item" :key="index" @change="(checked) => checkboxClick(checked, item, index)">{{ infiniteItemName(item) }}</el-checkbox>
  11. <div class="transger-number">{{ infiniteCheckedTotal }}/{{ dataTotal }}</div>
  12. </div>
  13. </div>
  14. <div class="transfer-selected">
  15. <div class="total-main">您已选择记录总数:<span style="color: #00a2d4">{{ selectedList.length }}</span></div>
  16. <div class="infinite-list">
  17. <el-tag v-for="(item, index) in selectedList" :key="index" closable @close="handleClose(item, index)"><div>{{ infiniteItemName(item) }}</div></el-tag>
  18. </div>
  19. </div>
  20. </div>
  21. </template>
  22. <script>
  23. import { debounce } from "main/tools/utils";
  24. export default {
  25. name: 'NtInfiniteTransfer',
  26. props: {
  27. height: {
  28. type: Number,
  29. default: 280
  30. },
  31. options: {
  32. type: Object
  33. },
  34. primary: {
  35. type: String,
  36. required: true
  37. },
  38. dataTotal: {
  39. type: String | Number,
  40. default: function () {
  41. return 0
  42. }
  43. },
  44. fields: {
  45. // eslint-disable-next-line vue/require-prop-type-constructor
  46. type: Array | String,
  47. default: function () {
  48. return []
  49. }
  50. },
  51. dataList: {
  52. type: Array,
  53. required: true
  54. },
  55. selectedList: {
  56. type: Array,
  57. default: function () {
  58. return []
  59. }
  60. }
  61. },
  62. data() {
  63. return {
  64. keyword: '',
  65. checkAll: false,
  66. scrollDisabled: false,
  67. isIndeterminate: false,
  68. infiniteListStatus: true,
  69. infiniteCheckedTotal: 0,
  70. selectedItemArray: [],
  71. debounceInfinite: null
  72. }
  73. },
  74. watch: {
  75. dataList: {
  76. deep: false,
  77. handler(oldList, newList) {
  78. // 是否可滚动
  79. this.scrollDisabled = false
  80. if (this.dataList.length > 0 && this.dataTotal <= this.dataList.length) {
  81. this.scrollDisabled = true
  82. }
  83. this.initDataList();
  84. }
  85. }
  86. },
  87. created() {
  88. this.initData();
  89. this.checkedStatusHandle();
  90. this.debounceInfinite = debounce(this.resetInfiniteStatus)
  91. },
  92. methods: {
  93. initData() {
  94. this.initSelectItemArray();
  95. this.initDataList();
  96. },
  97. initSelectItemArray() {
  98. this.selectedList.forEach(item => {
  99. this.selectedItemArray.push(this.infiniteItemPrimary(item));
  100. })
  101. },
  102. initDataList() {
  103. this.infiniteCheckedTotal = 0;
  104. this.dataList.forEach(item => {
  105. // 设置默认值
  106. if (!item.hasOwnProperty('ischecked')) {
  107. item.ischecked = false
  108. }
  109. // 全选功能
  110. if (this.checkAll) {
  111. item.ischecked = true
  112. }
  113. if (this.selectedItemArray.includes(this.infiniteItemPrimary(item))) {
  114. // 右边列表存在,左边没有
  115. item.ischecked = true
  116. } else {
  117. // 右边不存在,左边为选中
  118. if (item.ischecked) {
  119. this.selectedList.unshift(item)
  120. this.selectedItemArray.unshift(this.infiniteItemPrimary(item))
  121. }
  122. }
  123. // 计数
  124. if (item.ischecked) {
  125. this.infiniteCheckedTotal ++
  126. }
  127. })
  128. },
  129. onload() {
  130. this.$emit('onload', this.keyword)
  131. },
  132. resetInfiniteStatus() {
  133. this.infiniteListStatus = false
  134. this.$emit('remote')
  135. this.checkAll = false
  136. this.$nextTick(() => {
  137. this.infiniteListStatus = true
  138. })
  139. },
  140. searchEvent() {
  141. this.debounceInfinite();
  142. },
  143. infiniteItemPrimary(item) {
  144. return item[this.primary]
  145. },
  146. infiniteItemName(item) {
  147. if (typeof (this.fields) === 'string') {
  148. return item[this.fields]
  149. } else {
  150. if (this.fields.length > 0) {
  151. } else {
  152. return item
  153. }
  154. }
  155. },
  156. handleCheckAllChange(checked) {
  157. this.dataList.forEach(item => {
  158. item.ischecked = checked;
  159. // 右边是否存在记录
  160. if (this.selectedItemArray.includes(this.infiniteItemPrimary(item))) {
  161. if (!checked) {
  162. for (let i = 0; i < this.selectedItemArray.length; i++) {
  163. if (this.infiniteItemPrimary(item) === this.selectedItemArray[i]) {
  164. this.selectedList.splice(i, 1);
  165. this.selectedItemArray.splice(i, 1);
  166. this.infiniteCheckedTotal --;
  167. break;
  168. }
  169. }
  170. }
  171. } else {
  172. if (checked) {
  173. this.selectedList.unshift(item);
  174. this.selectedItemArray.unshift(this.infiniteItemPrimary(item));
  175. this.infiniteCheckedTotal ++;
  176. }
  177. }
  178. })
  179. this.isIndeterminate = false
  180. },
  181. checkboxClick(checked, item, index) {
  182. if (checked) {
  183. this.selectedList.unshift(item);
  184. this.selectedItemArray.unshift(this.infiniteItemPrimary(item));
  185. this.infiniteCheckedTotal ++;
  186. } else {
  187. item.ischecked = false
  188. for (let i = 0; i < this.selectedItemArray.length; i++) {
  189. if (this.infiniteItemPrimary(item) === this.selectedItemArray[i]) {
  190. this.selectedList.splice(i, 1);
  191. this.selectedItemArray.splice(i, 1);
  192. this.infiniteCheckedTotal --;
  193. break;
  194. }
  195. }
  196. }
  197. this.checkedStatusHandle()
  198. },
  199. handleClose(item, index) {
  200. for (let i = 0; i < this.dataList.length; i++) {
  201. if (this.infiniteItemName(item) === this.infiniteItemName(this.dataList[i])) {
  202. this.dataList[i].ischecked = false;
  203. break
  204. }
  205. }
  206. this.selectedList.splice(index, 1);
  207. this.selectedItemArray.splice(index, 1);
  208. this.infiniteCheckedTotal --;
  209. this.checkedStatusHandle()
  210. },
  211. checkedStatusHandle() {
  212. if (this.dataList.length === 0) {
  213. this.checkAll = false
  214. return
  215. }
  216. if (this.dataList.length === this.infiniteCheckedTotal) {
  217. this.checkAll = true
  218. } else {
  219. this.checkAll = false
  220. }
  221. this.isIndeterminate = false
  222. }
  223. }
  224. }
  225. </script>
  226. <style lang="scss" scoped>
  227. .infinite-transfer {
  228. height: 280px;
  229. display: flex;
  230. align-items: center;
  231. justify-content: space-between;
  232. .infinite-list-main, .transfer-selected {
  233. width: 45%;
  234. height: 100%;
  235. padding: 10px;
  236. border-radius: 4px;
  237. border: 1px solid #f2f2f2;
  238. .infinite-list {
  239. height: 220px;
  240. line-height: 25px;
  241. overflow: hidden;
  242. margin-top: 10px;
  243. overflow-y: auto;
  244. .el-checkbox {
  245. display: block;
  246. }
  247. .el-tag {
  248. display: flex;
  249. align-items: center;
  250. justify-content: space-between;
  251. margin-bottom: 5px;
  252. i {
  253. display: block;
  254. }
  255. }
  256. }
  257. }
  258. .infinite-list-main {
  259. .search-input-main {
  260. display: flex;
  261. align-items: center;
  262. justify-content: flex-start;
  263. }
  264. .transger-number {
  265. position: absolute;
  266. bottom: 5px;
  267. right: 55%;
  268. margin-right: 10px;
  269. }
  270. }
  271. .transfer-selected {
  272. .total-main {
  273. height: 32px;
  274. line-height: 32px;
  275. }
  276. }
  277. }
  278. </style>