GoodsInfoPicker.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. <!-- 树形层级选择器-->
  2. <!-- 1、支持单选、多选 -->
  3. <template>
  4. <view>
  5. <view class="tree-cover" :class="{'show':showDialog}" @tap="_cancel"></view>
  6. <view class="tree-dialog" :class="{'show':showDialog}">
  7. <view class="tree-bar">
  8. <view class="tree-bar-cancel" :style="{'color':cancelColor}" hover-class="hover-c" @tap="_cancel">取消
  9. </view>
  10. <view class="tree-bar-title" :style="{'color':titleColor}">{{title}}</view>
  11. <view class="tree-bar-confirm" :style="{'color':confirmColor}" hover-class="hover-c" @tap="_confirm">
  12. {{multiple?'确定':''}}
  13. </view>
  14. </view>
  15. <view>
  16. <scroll-view class="single-level-list" :scroll-y="true" style="height: 400rpx;">
  17. <view class="single-level-item" v-for="(item, index) in treeList" :key="index">
  18. <view class="item-label">
  19. <view class="uni-flex-item uni-inline-item" @tap.stop="_onItemSelect(item, index)">
  20. <view class="item-name">{{ item.tradeName }}</view>
  21. <view class="item-check">
  22. <!-- 这里放置单选的勾选标记 -->
  23. <view class="item-check-yes" v-if="item.checkStatus===1"
  24. :class="{'radio':!multiple}" :style="{'border-color':confirmColor}">
  25. <view class="item-check-yes-part"
  26. :style="{'background-color':confirmColor}">
  27. </view>
  28. </view>
  29. <view class="item-check-yes" v-else-if="item.checkStatus===2"
  30. :class="{'radio':!multiple}" :style="{'border-color':confirmColor}">
  31. <view class="item-check-yes-all" :style="{'background-color':confirmColor}">
  32. </view>
  33. </view>
  34. <view class="item-check-no" v-else :class="{'radio':!multiple}"
  35. :style="{'border-color':confirmColor}"></view>
  36. </view>
  37. </view>
  38. </view>
  39. </view>
  40. </scroll-view>
  41. </view>
  42. </view>
  43. </view>
  44. </template>
  45. <script>
  46. export default {
  47. emits: ['select-goods-change'],
  48. name: "ba-tree-picker",
  49. props: {
  50. valueKey: {
  51. type: String,
  52. default: 'id'
  53. },
  54. textKey: {
  55. type: String,
  56. default: 'name'
  57. },
  58. childrenKey: {
  59. type: String,
  60. default: 'children'
  61. },
  62. localdata: {
  63. type: Array,
  64. default: function() {
  65. return []
  66. }
  67. },
  68. localTreeList: { //在已经格式化好的数据
  69. type: Array,
  70. default: function() {
  71. return []
  72. }
  73. },
  74. selectedData: {
  75. type: Array,
  76. default: function() {
  77. return []
  78. }
  79. },
  80. title: {
  81. type: String,
  82. default: ''
  83. },
  84. multiple: { // 是否可以多选
  85. type: Boolean,
  86. default: true
  87. },
  88. selectParent: { //是否可以选父级
  89. type: Boolean,
  90. default: true
  91. },
  92. confirmColor: { // 确定按钮颜色
  93. type: String,
  94. default: '' // #0055ff
  95. },
  96. cancelColor: { // 取消按钮颜色
  97. type: String,
  98. default: '' // #757575
  99. },
  100. titleColor: { // 标题颜色
  101. type: String,
  102. default: '' //
  103. },
  104. switchColor: { // 节点切换图标颜色
  105. type: String,
  106. default: '' // #666
  107. },
  108. border: { // 是否有分割线
  109. type: Boolean,
  110. default: false
  111. },
  112. },
  113. data() {
  114. return {
  115. showDialog: false,
  116. index: '',
  117. treeList: [],
  118. }
  119. },
  120. computed: {},
  121. methods: {
  122. _show(index) {
  123. this.index = index
  124. this.showDialog = true
  125. },
  126. _hide() {
  127. this.showDialog = false
  128. },
  129. _cancel() {
  130. this._hide()
  131. this.$emit("cancel", '');
  132. },
  133. _confirm() { //多选
  134. let selectedList = []; //如果子集全部选中,只返回父级 id
  135. let selectedNames;
  136. let tradeNumber;
  137. let currentLevel = -1;
  138. this.treeList.forEach((item, index) => {
  139. if (currentLevel >= 0 && item.level > currentLevel) {
  140. } else {
  141. if (item.checkStatus === 2) {
  142. currentLevel = item.level;
  143. selectedList.push(item.id);
  144. selectedNames = selectedNames ? selectedNames + ' / ' + item.name : item.name;
  145. tradeNumber = item.tradeNumber
  146. } else {
  147. currentLevel = -1;
  148. }
  149. }
  150. })
  151. this._hide()
  152. this.$emit("select-goods-change", selectedList, selectedNames, this.index, tradeNumber);
  153. },
  154. //格式化原数据(原数据为tree结构)
  155. /*_formatTreeData(list = [], level = 0, parentItem, isShowChild = true) {
  156. let nextIndex = 0;
  157. let parentId = -1;
  158. let initCheckStatus = 0;
  159. if (parentItem) {
  160. nextIndex = this.treeList.findIndex(item => item.id === parentItem.id) + 1;
  161. parentId = parentItem.id;
  162. if (!this.multiple) { //单选
  163. initCheckStatus = 0;
  164. } else
  165. initCheckStatus = parentItem.checkStatus == 2 ? 2 : 0;
  166. }
  167. list.forEach(item => {
  168. let isLastLevel = true;
  169. if (item && item.children) {
  170. let children = item.children;
  171. if (Array.isArray(children) && children.length > 0) {
  172. isLastLevel = false;
  173. }
  174. }
  175. let itemT = {
  176. id: item.id,
  177. name: item.name,
  178. level,
  179. isLastLevel,
  180. isShow: isShowChild,
  181. isShowChild: false,
  182. checkStatus: initCheckStatus,
  183. orCheckStatus: 0,
  184. parentId,
  185. children: item.children,
  186. childCount: item.children ? item.children.length : 0,
  187. childCheckCount: 0,
  188. childCheckPCount: 0
  189. };
  190. if (this.selectedData.indexOf(itemT.id) >= 0) {
  191. itemT.checkStatus = 2;
  192. itemT.orCheckStatus = 2;
  193. itemT.childCheckCount = itemT.children ? itemT.children.length : 0;
  194. this._onItemParentSelect(itemT, nextIndex);
  195. }
  196. this.treeList.splice(nextIndex, 0, itemT);
  197. nextIndex++;
  198. })
  199. // console.log('this.treeList', this.treeList);
  200. },*/
  201. // 节点打开、关闭切换
  202. /*_onItemSwitch(item, index) {
  203. if (item.isLastLevel === true) {
  204. return;
  205. }
  206. item.isShowChild = !item.isShowChild;
  207. if (item.children) {
  208. this._formatTreeData(item.children, item.level + 1, item);
  209. item.children = undefined;
  210. } else {
  211. this._onItemChildSwitch(item, index);
  212. }
  213. },*/
  214. /*_onItemChildSwitch(item, index) {
  215. //console.log('_onItemChildSwitch')
  216. const firstChildIndex = index + 1;
  217. if (firstChildIndex > 0)
  218. for (var i = firstChildIndex; i < this.treeList.length; i++) {
  219. let itemChild = this.treeList[i];
  220. if (itemChild.level > item.level) {
  221. if (item.isShowChild) {
  222. if (itemChild.parentId === item.id) {
  223. itemChild.isShow = item.isShowChild;
  224. if (!itemChild.isShow) {
  225. itemChild.isShowChild = false;
  226. }
  227. }
  228. } else {
  229. itemChild.isShow = item.isShowChild;
  230. itemChild.isShowChild = false;
  231. }
  232. } else {
  233. return;
  234. }
  235. }
  236. },*/
  237. // 节点选中、取消选中
  238. _onItemSelect(item, index) {
  239. //console.log('_onItemSelect')
  240. //console.log(item)
  241. if (!this.multiple) { //单选
  242. item.checkStatus = item.checkStatus == 0 ? 2 : 0;
  243. this.treeList.forEach((v, i) => {
  244. if (i != index) {
  245. this.treeList[i].checkStatus = 0
  246. } else {
  247. this.treeList[i].checkStatus = 2
  248. }
  249. })
  250. let selectedList = [];
  251. let selectedNames;
  252. let tradeNumber;
  253. selectedList.push(item.id);
  254. selectedNames = item.tradeName;
  255. tradeNumber = item.tradeNumber;
  256. this._hide()
  257. this.$emit("select-goods-change", selectedList, selectedNames, this.index,tradeNumber);
  258. return
  259. }
  260. let oldCheckStatus = item.checkStatus;
  261. switch (oldCheckStatus) {
  262. case 0:
  263. item.checkStatus = 2;
  264. item.childCheckCount = item.childCount;
  265. item.childCheckPCount = 0;
  266. break;
  267. case 1:
  268. case 2:
  269. item.checkStatus = 0;
  270. item.childCheckCount = 0;
  271. item.childCheckPCount = 0;
  272. break;
  273. default:
  274. break;
  275. }
  276. //子节点 全部选中
  277. /*this._onItemChildSelect(item, index);
  278. //父节点 选中状态变化
  279. this._onItemParentSelect(item, index, oldCheckStatus);*/
  280. },
  281. /*_onItemChildSelect(item, index) {
  282. //console.log('_onItemChildSelect')
  283. let allChildCount = 0;
  284. if (item.childCount && item.childCount > 0) {
  285. index++;
  286. while (index < this.treeList.length && this.treeList[index].level > item.level) {
  287. let itemChild = this.treeList[index];
  288. itemChild.checkStatus = item.checkStatus;
  289. if (itemChild.checkStatus == 2) {
  290. itemChild.childCheckCount = itemChild.childCount;
  291. itemChild.childCheckPCount = 0;
  292. } else if (itemChild.checkStatus == 0) {
  293. itemChild.childCheckCount = 0;
  294. itemChild.childCheckPCount = 0;
  295. }
  296. // console.log('>>>>index:', index, 'item:', itemChild.name, ' status:', itemChild
  297. // .checkStatus)
  298. index++;
  299. }
  300. }
  301. },
  302. _onItemParentSelect(item, index, oldCheckStatus) {
  303. //console.log('_onItemParentSelect')
  304. const parentIndex = this.treeList.findIndex(itemP => itemP.id == item.parentId);
  305. //console.log('parentIndex:' + parentIndex)
  306. if (parentIndex >= 0) {
  307. let itemParent = this.treeList[parentIndex];
  308. let count = itemParent.childCheckCount;
  309. let oldCheckStatusParent = itemParent.checkStatus;
  310. if (oldCheckStatus == 1) {
  311. itemParent.childCheckPCount -= 1;
  312. } else if (oldCheckStatus == 2) {
  313. itemParent.childCheckCount -= 1;
  314. }
  315. if (item.checkStatus == 1) {
  316. itemParent.childCheckPCount += 1;
  317. } else if (item.checkStatus == 2) {
  318. itemParent.childCheckCount += 1;
  319. }
  320. if (itemParent.childCheckCount <= 0 && itemParent.childCheckPCount <= 0) {
  321. itemParent.childCheckCount = 0;
  322. itemParent.childCheckPCount = 0;
  323. itemParent.checkStatus = 0;
  324. } else if (itemParent.childCheckCount >= itemParent.childCount) {
  325. itemParent.childCheckCount = itemParent.childCount;
  326. itemParent.childCheckPCount = 0;
  327. itemParent.checkStatus = 2;
  328. } else {
  329. itemParent.checkStatus = 1;
  330. }
  331. //console.log('itemParent:', itemParent)
  332. this._onItemParentSelect(itemParent, parentIndex, oldCheckStatusParent);
  333. }
  334. },*/
  335. // 重置数据
  336. _reTreeList() {
  337. this.treeList.forEach((v, i) => {
  338. this.treeList[i].checkStatus = v.orCheckStatus
  339. })
  340. },
  341. _initTree() {
  342. this.treeList = this.localdata
  343. // this._formatTreeData(this.localdata);
  344. }
  345. },
  346. watch: {
  347. localdata() {
  348. this._initTree();
  349. },
  350. localTreeList() {
  351. this.treeList = this.localTreeList;
  352. }
  353. },
  354. mounted() {
  355. this._initTree();
  356. }
  357. }
  358. </script>
  359. <style scoped>
  360. .uni-flex-item {
  361. display: flex; /* 使用 Flex 布局 */
  362. align-items: center; /* 垂直居中 */
  363. }
  364. .tree-cover {
  365. position: fixed;
  366. top: 0rpx;
  367. right: 0rpx;
  368. bottom: 0rpx;
  369. left: 0rpx;
  370. z-index: 100;
  371. background-color: rgba(0, 0, 0, .4);
  372. opacity: 0;
  373. transition: all 0.3s ease;
  374. visibility: hidden;
  375. }
  376. .tree-cover.show {
  377. visibility: visible;
  378. opacity: 1;
  379. }
  380. .tree-dialog {
  381. position: fixed;
  382. top: 0rpx;
  383. right: 0rpx;
  384. bottom: 0rpx;
  385. left: 0rpx;
  386. background-color: #fff;
  387. border-top-left-radius: 10px;
  388. border-top-right-radius: 10px;
  389. /* #ifndef APP-NVUE */
  390. display: flex;
  391. /* #endif */
  392. flex-direction: column;
  393. z-index: 102;
  394. top: 20%;
  395. transition: all 0.3s ease;
  396. transform: translateY(100%);
  397. }
  398. .tree-dialog.show {
  399. transform: translateY(0);
  400. }
  401. .tree-bar {
  402. /* background-color: #fff; */
  403. height: 90rpx;
  404. padding-left: 25rpx;
  405. padding-right: 25rpx;
  406. display: flex;
  407. justify-content: space-between;
  408. align-items: center;
  409. box-sizing: border-box;
  410. border-bottom-width: 1rpx !important;
  411. border-bottom-style: solid;
  412. border-bottom-color: #f5f5f5;
  413. font-size: 32rpx;
  414. color: #757575;
  415. line-height: 1;
  416. }
  417. .tree-bar-confirm {
  418. color: #0055ff;
  419. padding: 15rpx;
  420. }
  421. .tree-bar-title {}
  422. .tree-bar-cancel {
  423. color: #757575;
  424. padding: 15rpx;
  425. }
  426. .tree-view {
  427. flex: 1;
  428. padding: 20rpx;
  429. /* #ifndef APP-NVUE */
  430. display: flex;
  431. /* #endif */
  432. flex-direction: column;
  433. overflow: hidden;
  434. height: 100%;
  435. }
  436. .tree-list {
  437. flex: 1;
  438. height: 100%;
  439. overflow: hidden;
  440. }
  441. .tree-item {
  442. display: flex;
  443. justify-content: space-between;
  444. align-items: center;
  445. line-height: 1;
  446. height: 0;
  447. opacity: 0;
  448. transition: 0.2s;
  449. overflow: hidden;
  450. }
  451. .tree-item.show {
  452. height: 90rpx;
  453. opacity: 1;
  454. }
  455. .tree-item.showchild:before {
  456. transform: rotate(90deg);
  457. }
  458. .tree-item.last:before {
  459. opacity: 0;
  460. }
  461. .switch-on {
  462. width: 0;
  463. height: 0;
  464. border-left: 10rpx solid transparent;
  465. border-right: 10rpx solid transparent;
  466. border-top: 15rpx solid #666;
  467. }
  468. .switch-off {
  469. width: 0;
  470. height: 0;
  471. border-bottom: 10rpx solid transparent;
  472. border-top: 10rpx solid transparent;
  473. border-left: 15rpx solid #666;
  474. }
  475. .item-last-dot {
  476. position: absolute;
  477. width: 10rpx;
  478. height: 10rpx;
  479. border-radius: 100%;
  480. background: #666;
  481. display: none;
  482. }
  483. .item-icon {
  484. width: 26rpx;
  485. height: 26rpx;
  486. margin-right: 8rpx;
  487. padding-right: 20rpx;
  488. padding-left: 20rpx;
  489. }
  490. .item-label {
  491. flex: 1;
  492. display: flex;
  493. align-items: center;
  494. height: auto;
  495. line-height: 1.4;
  496. }
  497. .item-name {
  498. flex: 1;
  499. overflow: hidden;
  500. text-overflow: ellipsis;
  501. white-space: nowrap;
  502. width: 450rpx;
  503. }
  504. .item-check {
  505. margin-right: 8px; /* 适当的左边距 */
  506. order: -1;
  507. /*width: 40px;*/
  508. /*height: 40px;*/
  509. /*display: flex;*/
  510. /*justify-content: center;*/
  511. /*align-items: center;*/
  512. }
  513. .item-check-yes,
  514. .item-check-no {
  515. margin-left: 12px; /* 增加单选按钮与左侧的间距 */
  516. width: 20px;
  517. height: 20px;
  518. border-top-left-radius: 20%;
  519. border-top-right-radius: 20%;
  520. border-bottom-right-radius: 20%;
  521. border-bottom-left-radius: 20%;
  522. border-top-width: 1rpx;
  523. border-left-width: 1rpx;
  524. border-bottom-width: 1rpx;
  525. border-right-width: 1rpx;
  526. border-style: solid;
  527. border-color: #0055ff;
  528. display: flex;
  529. justify-content: center;
  530. align-items: center;
  531. box-sizing: border-box;
  532. }
  533. .item-check-yes-part {
  534. width: 12px;
  535. height: 12px;
  536. border-top-left-radius: 20%;
  537. border-top-right-radius: 20%;
  538. border-bottom-right-radius: 20%;
  539. border-bottom-left-radius: 20%;
  540. background-color: #0055ff;
  541. }
  542. .item-check-yes-all {
  543. margin-left: 12px; /* 增加单选按钮与左侧的间距 */
  544. margin-bottom: 5px;
  545. border: 2px solid #007aff;
  546. border-left: 0;
  547. border-top: 0;
  548. height: 12px;
  549. width: 6px;
  550. transform-origin: center;
  551. /* #ifndef APP-NVUE */
  552. transition: all 0.3s;
  553. /* #endif */
  554. transform: rotate(45deg);
  555. }
  556. .item-check .radio {
  557. border-top-left-radius: 50%;
  558. border-top-right-radius: 50%;
  559. border-bottom-right-radius: 50%;
  560. border-bottom-left-radius: 50%;
  561. }
  562. .item-check .radio .item-check-yes-b {
  563. border-top-left-radius: 50%;
  564. border-top-right-radius: 50%;
  565. border-bottom-right-radius: 50%;
  566. border-bottom-left-radius: 50%;
  567. }
  568. .hover-c {
  569. opacity: 0.6;
  570. }
  571. .itemBorder {
  572. border-bottom: 1px solid #e5e5e5;
  573. }
  574. </style>