linkageAssembly.vue 9.0 KB

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