linkageAssembly.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <template>
  2. <view class="select_popup_container">
  3. <view class="popup_title">
  4. <navigator v-if="mer_id" :url="`pages/product/addGoods/freightTemplate?mer_id=${mer_id}`" hover-class="none" class="manage_btn">管理</navigator>
  5. <view class="popup_title_msn">{{ selectProductTitle }}</view>
  6. <view class="close" @click="close"><text class="iconfont">&#xe761;</text></view>
  7. </view>
  8. <!-- 已选列表 -->
  9. <view class="selected_list" v-if="selectProductItem.multiple && multipleList.length">
  10. <view class="selected_list_item" v-for="(item, index) in multipleList" :key="index">
  11. <view>{{ item.label }}</view>
  12. <view @click="deleteSelectedList(item, index)"><view class="iconfont">&#xe6a0;</view></view>
  13. </view>
  14. </view>
  15. <!-- 标签头部 -->
  16. <view class="tap">
  17. <scroll-view scroll-x="true" class="popup_tap">
  18. <view class="popup_tap_content">
  19. <view class="popup_tap_item" v-for="(item, index) in tap" :key="index" @click="handleSelectTapId(item, index)" :class="{ selectTapEd: selectTapId == item.value }">
  20. <view>{{ item.label }}</view>
  21. </view>
  22. </view>
  23. </scroll-view>
  24. </view>
  25. <!-- 内容选择 -->
  26. <view class="content">
  27. <scroll-view scroll-y="true" class="popup_sroll" @scrolltolower="scrolltolower">
  28. <view class="content_list">
  29. <view v-for="(item, index) in assemblyList" :key="index" class="content_list_item" :class="{ selectSingleCase: item.selectEd }">
  30. <view @click="selectItem(item, assemblyList)">{{ item.label }}</view>
  31. <view v-if="selectProductItem.multiple && !item.children"><text class="iconfont" @click="selectPushMultiple(item, assemblyList)">&#xe70e;</text></view>
  32. </view>
  33. </view>
  34. </scroll-view>
  35. </view>
  36. </view>
  37. </template>
  38. <script>
  39. // +----------------------------------------------------------------------
  40. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  41. // +----------------------------------------------------------------------
  42. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  43. // +----------------------------------------------------------------------
  44. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  45. // +----------------------------------------------------------------------
  46. // | Author: CRMEB Team <admin@crmeb.com>
  47. // +----------------------------------------------------------------------
  48. import { serialize } from '../../../libs/uniApi.js';
  49. export default {
  50. props: {
  51. selectProductTitle: {
  52. type: String,
  53. default: ''
  54. },
  55. // 从外部传入的分类数据
  56. classifiedData: {
  57. type: Array,
  58. default: () => {
  59. return [];
  60. }
  61. },
  62. // 表单数据
  63. form: {
  64. type: Object,
  65. default: () => {
  66. return {};
  67. }
  68. },
  69. // 外部选项数据,携带需要进项判断的参数
  70. selectProductItem: {
  71. type: Object,
  72. default: () => {
  73. return {
  74. // multiple true || false 是否多选
  75. // model 外部表单的key值
  76. // showTap 是否展示tap
  77. // allreadySelect 曾经的选项,用于再次打开后回显
  78. // singleColumn 单列单选
  79. };
  80. }
  81. },
  82. mer_id: {
  83. type: Number,
  84. default: 0
  85. }
  86. },
  87. data() {
  88. return {
  89. assemblyList: [],
  90. tap: [], // 标签数据
  91. selectTapId: '',
  92. tapIndex: '', // 选中d的tap的下标
  93. multipleList: [] // 多选的数组, 可用于展示
  94. };
  95. },
  96. watch: {
  97. tap: {
  98. handler(val) {
  99. // console.log(val);
  100. },
  101. deep: true
  102. },
  103. multipleList: {
  104. handler(val) {
  105. this.$emit('multipleList', val, this.selectProductItem.model);
  106. }
  107. },
  108. // 监听外部传入的数据变化,重新渲染数据
  109. classifiedData: {
  110. handler(val) {
  111. this.initData();
  112. },
  113. deep: true
  114. }
  115. },
  116. created() {
  117. this.initData();
  118. this.tap.push({ value: '', label: '请选择', samelevel: this.assemblyList });
  119. },
  120. methods: {
  121. initData() {
  122. this.addClassifiedAttr(this.classifiedData, 'children', 'selectEd', false);
  123. // 代表有选中项
  124. if (this.selectProductItem.singleColumn && (this.selectProductItem.allreadySelect && this.selectProductItem.allreadySelect.length) ) {
  125. this.assemblyList = serialize(this.classifiedData);
  126. this.assemblyList.forEach(item => {
  127. if (this.selectProductItem.allreadySelect.map(val => val.value).includes(item.value)) {
  128. item.selectEd = true;
  129. }
  130. });
  131. return;
  132. }
  133. // 多选有数据
  134. if (this.selectProductItem.allreadySelect && this.selectProductItem.allreadySelect.length && !this.selectProductItem.multiple) {
  135. this.tap = this.selectProductItem.allreadySelect;
  136. this.selectTapId = this.tap[0].value;
  137. this.handleSelectTapId(this.tap[0], 0);
  138. return;
  139. }
  140. // 品牌数据没有 value, label 字段
  141. this.classifiedData.forEach(item => {
  142. if (!item.value) {
  143. this.$set(item, 'value', item.brand_id);
  144. }
  145. if (!item.label) {
  146. this.$set(item, 'label', item.brand_name);
  147. }
  148. });
  149. this.assemblyList = serialize(this.classifiedData);
  150. },
  151. selectItem(item, samelevel) {
  152. console.log(item);
  153. if (this.selectProductItem.multiple) {
  154. // 注意,只能选择最后一级
  155. if (!item.children || !item.children.length) {
  156. item.selectEd = !item.selectEd;
  157. }
  158. } else {
  159. this.assemblyList.forEach(val => {
  160. val.selectEd = false;
  161. });
  162. item.selectEd = true;
  163. }
  164. if (this.tap.filter(val => val.value === item.value).length) {
  165. return;
  166. }
  167. this.$set(item, 'samelevel', samelevel);
  168. if (this.selectTapId !== '') {
  169. if (!item.children || !item.children.length) {
  170. this.tap[this.tap.length - 1].label = item.label;
  171. this.tap[this.tap.length - 1].value = item.value;
  172. this.$emit('getLinkageData', this.tap, this.selectProductItem.model);
  173. return;
  174. }
  175. this.tap.splice(this.tapIndex, 1, item);
  176. this.tap.splice(this.tapIndex + 1, this.tap.length);
  177. this.selectTapId = '';
  178. this.assemblyList = item.children;
  179. this.tap.push({ value: '', label: '请选择', samelevel: this.assemblyList });
  180. return;
  181. }
  182. if (item.children && item.children.length && !this.selectTapId) {
  183. this.tap.splice(this.tap.length - 1, 0, item);
  184. this.assemblyList = item.children;
  185. } else {
  186. this.tap[this.tap.length - 1].label = item.label;
  187. this.tap[this.tap.length - 1].value = item.value;
  188. if (this.selectProductItem.multiple) {
  189. return;
  190. }
  191. this.$emit('getLinkageData', this.tap, this.selectProductItem.model);
  192. }
  193. },
  194. // 多选时添加事件
  195. selectPushMultiple(item, samelevel) {
  196. item.selectEd = true;
  197. this.multipleList.push(item);
  198. this.multipleList = this.unique(this.multipleList);
  199. },
  200. // 数组去重
  201. unique(arr) {
  202. var obj = {};
  203. return arr.filter(ele => {
  204. if (!obj[ele]) {
  205. obj[ele] = true;
  206. return true;
  207. }
  208. });
  209. },
  210. // 删除已选项
  211. deleteSelectedList(item, index) {
  212. this.multipleList.splice(index, 1);
  213. this.assemblyList.forEach(val => {
  214. if (item.value == val.value) {
  215. val.selectEd = false;
  216. }
  217. });
  218. },
  219. // 将数组用 "/" 连接
  220. connectionString(arr, lab = '/') {
  221. return arr.map(item => item.label).join('/');
  222. },
  223. handleSelectTapId(item, index) {
  224. if (!item.value) {
  225. this.assemblyList = this.tap[this.tap.length - 2].children;
  226. this.selectTapId = '';
  227. return;
  228. }
  229. if (index == this.tap.length - 1) {
  230. this.selectTapId = item.value;
  231. this.assemblyList = this.tap[this.tap.length - 2].children;
  232. return;
  233. }
  234. this.selectTapId = item.value;
  235. this.tapIndex = index;
  236. this.assemblyList = item.samelevel;
  237. },
  238. // 递归添加属性
  239. addClassifiedAttr(arr, childKey, addkey, value) {
  240. arr.forEach(item => {
  241. this.$set(item, addkey, value);
  242. if (item[childKey] && item[childKey].length) {
  243. this.addClassifiedAttr(item[childKey], childKey, addkey, value);
  244. }
  245. });
  246. },
  247. close() {
  248. this.$emit('close');
  249. },
  250. // 滑动组件滑倒底部时触发事件
  251. scrolltolower() {
  252. this.$emit('scrolltolower', this.selectProductItem.model);
  253. }
  254. }
  255. };
  256. </script>
  257. <style lang="scss" scoped>
  258. .select_popup_container {
  259. background: #fff;
  260. border-radius: 16rpx 16rpx 0 0;
  261. }
  262. .popup_title {
  263. display: flex;
  264. justify-content: flex-end;
  265. padding: 36rpx 30rpx;
  266. position: relative;
  267. .manage_btn{
  268. font-weight: normal;
  269. color: #e93323;
  270. font-size: 24rpx;
  271. position: absolute;
  272. left: 30rpx;
  273. top: 40rpx;
  274. }
  275. &_msn {
  276. position: absolute;
  277. top: 50%;
  278. left: 50%;
  279. transform: translate(-50%, -50%);
  280. color: #282828;
  281. font-size: 32rpx;
  282. font-weight: bold;
  283. }
  284. .close {
  285. position: relative;
  286. z-index: 10;
  287. font-size: 28rpx;
  288. color: #8a8a8a;
  289. }
  290. }
  291. .selected_list {
  292. display: flex;
  293. flex-wrap: wrap;
  294. padding: 50rpx 30rpx;
  295. &_item {
  296. display: flex;
  297. align-items: center;
  298. color: #e93323;
  299. margin-right: 31rpx;
  300. margin-bottom: 40rpx;
  301. padding: 3rpx 11rpx;
  302. background: #fff6f5;
  303. .iconfont {
  304. display: inline-block;
  305. margin-left: 14rpx;
  306. font-size: 24rpx;
  307. }
  308. }
  309. }
  310. .popup_tap {
  311. display: flex;
  312. padding: 0 30rpx;
  313. color: #333333;
  314. font-size: 28rpx;
  315. border-bottom: 1px solid #eeeeee;
  316. margin-bottom: 40rpx;
  317. .popup_tap_content {
  318. display: flex;
  319. }
  320. &_item {
  321. margin-right: 60rpx;
  322. padding-bottom: 21rpx;
  323. white-space: nowrap;
  324. }
  325. .selectTapEd {
  326. color: #e93323;
  327. border-bottom: 3rpx solid #e93323;
  328. }
  329. }
  330. .content {
  331. .popup_sroll {
  332. max-height: 742rpx;
  333. min-height: 300rpx;
  334. }
  335. &_list {
  336. &_item {
  337. color: #333333;
  338. margin-bottom: 50rpx;
  339. margin-left: 40rpx;
  340. margin-right: 40rpx;
  341. display: flex;
  342. align-items: center;
  343. justify-content: space-between;
  344. > view {
  345. flex: 0.6;
  346. }
  347. > view:nth-child(2) {
  348. flex: 0.4;
  349. display: flex;
  350. justify-content: flex-end;
  351. }
  352. .iconfont {
  353. color: #e93323;
  354. font-size: 36rpx !important;
  355. }
  356. }
  357. }
  358. }
  359. .selectSingleCase {
  360. color: #e93323;
  361. }
  362. .selectTapEd {
  363. color: #e93323;
  364. border-bottom: 3rpx solid #e93323;
  365. }
  366. </style>