tui-sel-cascade.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <template>
  2. <view class="abow-sel-cascade">
  3. <view @tap.stop="closemask">
  4. <slot :selData="seldata">
  5. <text>{{seldata&&seldata.fulltext?seldata.fulltext:placeholder}}</text>
  6. </slot>
  7. </view>
  8. <tui-bottom-popup class="page-popup" :title="title" :dotColor="dotColor" :show="isopen"
  9. backgroundColor="#ffffff" @close="closemask">
  10. <tui-cascade-selection :isshow="1" :size="26" request :receiveData="receiveData" :textSize="24"
  11. :itemList="itemList" :reset="resetid" @complete="complete" @change="change"></tui-cascade-selection>
  12. <view class="submit-frame">
  13. <uni-goods-nav :fill="true" :options="options" :button-group="customButtonGroup" @click="onClick"
  14. @buttonClick="buttonClick" />
  15. </view>
  16. </tui-bottom-popup>
  17. </view>
  18. </template>
  19. <script>
  20. let App = getApp()
  21. import request from '@/utils/request'
  22. export default {
  23. // mixins: [mixinsPage],
  24. components: {},
  25. data() {
  26. return {
  27. isopen: false,
  28. receiveData: [],
  29. itemList: [],
  30. seldata: {},
  31. options: [{
  32. // icon: 'close',
  33. text: '清空'
  34. }],
  35. resetid: 0,
  36. customButtonGroup: [],
  37. }
  38. },
  39. props: {
  40. // 手动设置根目录数据
  41. rootData: {
  42. type: Array,
  43. default: null,
  44. },
  45. //改变值则重置数据
  46. reset: {
  47. type: [Number, String],
  48. default: 0
  49. },
  50. // 占位符
  51. placeholder: {
  52. type: String,
  53. default: "无"
  54. },
  55. fulltext: {
  56. type: String,
  57. default: ""
  58. },
  59. disabled: {
  60. type: Boolean,
  61. default: false
  62. },
  63. value: {
  64. type: [Number, String],
  65. default: null
  66. },
  67. title: {
  68. type: String,
  69. default: "选择"
  70. },
  71. apiurl: {
  72. type: String,
  73. default: ""
  74. },
  75. leaf: {
  76. type: String,
  77. default: "leaf"
  78. },
  79. allSel: {
  80. type: String,
  81. default: "" // 选择全部
  82. },
  83. dotColor: {
  84. type: String,
  85. default: '#26ae7e'
  86. },
  87. postKey: {
  88. type: Array,
  89. default: () => {
  90. return [{
  91. key: 'induPid',
  92. type: 'par',
  93. value: 'induId'
  94. }, {
  95. key: 'induType',
  96. type: '',
  97. value: 1
  98. }]
  99. }
  100. },
  101. keyId: {
  102. type: String,
  103. default: "induId"
  104. },
  105. keyLabel: {
  106. type: String,
  107. default: "induName"
  108. },
  109. keyList: {
  110. type: String,
  111. default: ""
  112. },
  113. selLeaf: {
  114. type: [Boolean], // 是否要求选择叶子节点
  115. default: true
  116. },
  117. lvlLeaf: {
  118. type: [Number], // 数值则限制最大选择深度
  119. default: 0
  120. }
  121. },
  122. watch: {
  123. // value: {
  124. // handler(nval, oval) {},
  125. // immediate: true,
  126. // deep: true
  127. // }
  128. reset(val) {
  129. this.resetid = val
  130. this.clearData()
  131. }
  132. },
  133. created() {
  134. this.appData = App.globalData ? App.globalData : getApp().globalData;
  135. this.resetid = this.reset
  136. if (this.fulltext) {
  137. this.$set(this.seldata, "fulltext", this.fulltext)
  138. }
  139. this.customButtonGroup = [{
  140. text: '取消',
  141. backgroundColor: 'linear-gradient(90deg, #f4f4f4, #f4f4f4)',
  142. color: '#333'
  143. }]
  144. if (!this.selLeaf) {
  145. this.customButtonGroup.push({
  146. text: '确定',
  147. backgroundColor: 'linear-gradient(90deg, #26ae7e, #26ae7e)',
  148. color: '#fff'
  149. })
  150. }
  151. console.log(3333)
  152. this.loadData('rush');
  153. },
  154. methods: {
  155. // 弹窗选择
  156. closemask() {
  157. if (this.disabled) {
  158. return false
  159. }
  160. this.isopen = !this.isopen
  161. },
  162. complete(e) {
  163. if (this.selLeaf) {
  164. this.returndata()
  165. } else {
  166. // this.$api.toast("请选择数据")
  167. }
  168. },
  169. onClick(e) {
  170. // console.log('清空', e)
  171. this.resetid = this.resetid * -1
  172. this.clearData()
  173. this.closemask()
  174. },
  175. clearData() {
  176. const seldata = {
  177. id: 0,
  178. fullvalue: '',
  179. fulltext: ''
  180. }
  181. this.seldata = seldata
  182. this.$emit("change", seldata)
  183. this.$emit("input", '')
  184. },
  185. buttonClick(e) {
  186. if (e.index == 1) {
  187. // console.log('确认', e)
  188. this.returndata()
  189. } else {
  190. // console.log('取消', e)
  191. this.closemask()
  192. }
  193. },
  194. change(e) {
  195. console.log(5555)
  196. this.seldata = Object.assign({}, e)
  197. if (!e.leaf) {
  198. if (!this.selLeaf) {
  199. this.seldata = Object.assign({}, e)
  200. }
  201. if (this.selLeaf && this.lvlLeaf > 0 && this.lvlLeaf <= e.result.length) {
  202. // this.seldata = Object.assign({}, e)
  203. console.log(666)
  204. this.returndata()
  205. return false
  206. }
  207. if (this.lvlLeaf <= 0 || this.lvlLeaf > e.result.length) {
  208. this.loadData(e);
  209. }
  210. } else {
  211. // this.seldata = Object.assign({}, e)
  212. this.receiveData = this.receiveData == null ? [] : null;
  213. }
  214. },
  215. returndata() {
  216. // console.log(this.seldata)
  217. this.$emit("change", this.seldata)
  218. this.$emit("input", this.seldata[this.keyId])
  219. this.closemask()
  220. },
  221. //行业列表
  222. loadData(e = null, index = 0) {
  223. setTimeout(() => {
  224. if (e == 'rush' && this.rootData && this.rootData) {
  225. this.itemList = [].concat(this.rootData).map(item => {
  226. console.log(item,'itemitem')
  227. this.$set(item, "text", item[this.keyLabel])
  228. this.$set(item, "value", item[this.keyId])
  229. this.$set(item, "leaf", !!item[this.leaf])
  230. return item
  231. })
  232. console.log(this.rootData)
  233. return false
  234. }
  235. },300)
  236. if (!this.apiurl) {
  237. return false
  238. }
  239. const data = {}
  240. // { key: "", type: "par", value: "value|text|fullvalue|fulltext" }
  241. // this.postKey.forEach(item => {
  242. // if (item.type == "par" && e) {
  243. // this.$set(data, item.key, e[item.value])
  244. // } else if (!item.type) {
  245. // this.$set(data, item.key, item.value)
  246. // }
  247. // })
  248. // // console.log(e, data)
  249. request({
  250. 'url': this.apiurl,
  251. 'method': 'get',
  252. params: data
  253. }).then(res => {
  254. if (res.code == 200) {
  255. let list = res.data
  256. if (this.keyList) {
  257. list = res.items[this.keyList]
  258. }
  259. if (e) {
  260. console.log(list,'666')
  261. this.receiveData = [].concat(list).map(item => {
  262. this.$set(item, "text", item[this.keyLabel])
  263. this.$set(item, "value", item[this.keyId])
  264. this.$set(item, "leaf", !!item[this.leaf])
  265. return item
  266. })
  267. } else {
  268. console.log(list,'888')
  269. this.itemList = [].concat(list).map(item => {
  270. this.$set(item, "text", item[this.keyLabel])
  271. this.$set(item, "value", item[this.keyId])
  272. this.$set(item, "leaf", !!item[this.leaf])
  273. return item
  274. })
  275. if (this.allSel) {
  276. const al = {
  277. value: 0,
  278. text: this.allSel,
  279. leaf: true
  280. }
  281. this.itemList.unshift(al)
  282. }
  283. // console.log(this.itemList,this.receiveData,'this.receiveData')
  284. }
  285. }
  286. }).catch(res => {
  287. })
  288. }
  289. }
  290. }
  291. </script>
  292. <style lang="scss" scoped>
  293. .abow-sel-cascade {
  294. .tui-header {
  295. width: 100%;
  296. display: flex;
  297. align-items: center;
  298. justify-content: center;
  299. padding: 20rpx;
  300. box-sizing: border-box;
  301. background-color: #fff;
  302. border-top-left-radius: 20rpx;
  303. border-top-right-radius: 20rpx;
  304. overflow: hidden;
  305. position: relative;
  306. }
  307. .tui-icon-close {
  308. position: absolute;
  309. right: 30rpx;
  310. top: 50%;
  311. transform: translateY(-50%);
  312. }
  313. .submit-frame {
  314. padding: 30rpx 30rpx;
  315. background-color: #FFFFFF;
  316. position: relative;
  317. &:before {
  318. content: ' ';
  319. position: absolute;
  320. left: 0;
  321. top: 0px;
  322. right: 0;
  323. height: 1rpx;
  324. z-index: 1;
  325. // border-bottom: 1rpx solid #e5e5e5;
  326. background-color: #e5e5e5;
  327. transform-origin: 0 100%;
  328. transform: scaleY(0.5);
  329. }
  330. .btn {
  331. flex: 1;
  332. margin-right: 30rpx;
  333. &:last-child {
  334. margin-right: 0;
  335. }
  336. }
  337. }
  338. }
  339. </style>