treeReimbursementBuinessUserSelect.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <template>
  2. <el-select :value="valueTitle" :size="size" :disabled="disabled" :clearable="clearable" :placeholder="placeholderText" @clear="clearHandle">
  3. <el-option :value="valueTitle" :label="valueTitle" class="options">
  4. <el-tree id="tree-option"
  5. ref="selectTree"
  6. :accordion="accordion"
  7. :data="optionData"
  8. :show-checkbox="showCheckbox"
  9. :props="props"
  10. highlight-current
  11. :node-key="props.value"
  12. :default-expanded-keys="defaultExpandedKey"
  13. @check-change="handleCheckChange"
  14. @node-click="handleNodeClick">
  15. </el-tree>
  16. </el-option>
  17. </el-select>
  18. </template>
  19. <script>
  20. export default {
  21. inject: ['say'],
  22. name: 'el-tree-select',
  23. props: {
  24. /* 配置项 */
  25. props: {
  26. type: Object,
  27. default: () => {
  28. return {
  29. value: 'id', // ID字段名
  30. label: 'label', // 显示名称
  31. children: 'children' // 子级字段名
  32. }
  33. }
  34. },
  35. /* 选项列表数据(树形结构的对象数组) */
  36. data: {
  37. type: Array,
  38. default: () => { return [] }
  39. },
  40. /* 选项列表数据(树形结构的对象数组) */
  41. list: {
  42. type: Array,
  43. default: () => { return null }
  44. },
  45. /* 初始值 */
  46. value: {
  47. type: String,
  48. default: () => { return null }
  49. },
  50. /* 初始值 */
  51. url: {
  52. type: String,
  53. default: () => { return null }
  54. },
  55. disabled: {
  56. type: Boolean,
  57. dafault: () => { return false }
  58. },
  59. showCheckbox: {
  60. type: Boolean,
  61. dafault: () => { return false }
  62. },
  63. /* 初始值 */
  64. label: {
  65. type: String,
  66. default: () => { return null }
  67. },
  68. /* 可清空选项 */
  69. clearable: {
  70. type: Boolean,
  71. default: () => { return true }
  72. },
  73. /* 自动收起 */
  74. accordion: {
  75. type: Boolean,
  76. default: () => { return false }
  77. },
  78. size: {
  79. type: String,
  80. default: () => { return 'small' }
  81. },
  82. placeholder: {
  83. type: String,
  84. default: () => { return '请选择' }
  85. },
  86. isOnlySelectLeaf: {
  87. type: Boolean,
  88. default: () => {
  89. return false
  90. }
  91. }
  92. },
  93. data () {
  94. return {
  95. valueId: this.value, // 初始值
  96. valueTitle: this.label,
  97. defaultExpandedKey: [],
  98. placeholderText: this.placeholder,
  99. treeList: [],
  100. valueData: this.data
  101. }
  102. },
  103. created () {
  104. if (this.url !== null) {
  105. this.placeholderText = '加载数据中...'
  106. let interval = setInterval(() => {
  107. this.placeholderText = this.placeholderText + '.'
  108. }, 500)
  109. this.$http({
  110. url: this.url,
  111. method: 'get'
  112. }).then(({data}) => {
  113. this.valueData = data
  114. this.setTreeList(this.valueData)
  115. this.$nextTick(() => {
  116. this.initHandle()
  117. this.placeholderText = this.placeholder
  118. clearInterval(interval)
  119. })
  120. })
  121. } else {
  122. this.valueData = this.data
  123. this.setTreeList(this.valueData)
  124. }
  125. },
  126. methods: {
  127. setTreeList (datas) { // 遍历树 获取id数组
  128. for (var i in datas) {
  129. this.treeList.push(datas[i])
  130. if (datas[i].children) {
  131. this.setTreeList(datas[i].children)
  132. }
  133. }
  134. },
  135. // 初始化值
  136. initHandle () {
  137. if (this.valueId) {
  138. if (this.showCheckbox) {
  139. let ids = this.valueId.split(',')
  140. this.$refs.selectTree.setCheckedKeys(ids)
  141. let titles = []
  142. ids.forEach((id) => {
  143. this.treeList.forEach((d) => {
  144. if (id === d[this.props.value]) {
  145. titles.push(d[this.props.label])
  146. }
  147. })
  148. })
  149. this.valueTitle = titles.join(',')
  150. } else if (this.$refs.selectTree.getNode(this.valueId)) {
  151. this.valueTitle = this.$refs.selectTree.getNode(this.valueId).data[this.props.label] // 初始化显示
  152. this.$refs.selectTree.setCurrentKey(this.valueId) // 设置默认选中
  153. this.defaultExpandedKey = [this.valueId] // 设置默认展开
  154. }
  155. }
  156. this.initScroll()
  157. },
  158. getNode (id) {
  159. return this.$refs.selectTree.getNode(id)
  160. },
  161. // 初始化滚动条
  162. initScroll () {
  163. this.$nextTick(() => {
  164. let scrollWrap = document.querySelectorAll('.el-scrollbar .el-select-dropdown__wrap')[0]
  165. let scrollBar = document.querySelectorAll('.el-scrollbar .el-scrollbar__bar')
  166. if (scrollWrap) { scrollWrap.style.cssText = 'margin: 0px; max-height: none; overflow: hidden;' }
  167. if (scrollBar) {
  168. scrollBar.forEach(ele => {
  169. // eslint-disable-next-line no-return-assign
  170. return ele.style.width = 0
  171. })
  172. }
  173. })
  174. },
  175. // 切换选项
  176. handleNodeClick (node) {
  177. if (this.showCheckbox) {
  178. return
  179. }
  180. if (node['disabled'] || node['typeFlag']) {
  181. // this.$message.warning('节点(' + node[this.props.label] + ')被禁止选择,请重新选择。')
  182. return
  183. }
  184. if (this.isOnlySelectLeaf && node.children.length > 0) {
  185. // this.$message.warning('不能选择根节点(' + node[this.props.label] + ')请重新选择。')
  186. return
  187. }
  188. this.valueTitle = node[this.props.label]
  189. this.valueId = node[this.props.value]
  190. this.$emit('getValue', this.valueId, this.valueTitle, node)
  191. // 调用主页面的方法
  192. this.say(this.valueId)
  193. },
  194. handleCheckChange (data, checked, indeterminate) {
  195. let nodes = this.$refs.selectTree.getCheckedNodes()
  196. this.valueTitle = nodes.map((node) => {
  197. return node[this.props.label]
  198. }).join(',')
  199. this.valueId = nodes.map((node) => {
  200. return node[this.props.value]
  201. }).join(',')
  202. this.$emit('getValue', this.valueId, this.valueTitle)
  203. },
  204. // 清除选中
  205. clearHandle () {
  206. this.valueTitle = ''
  207. this.valueId = null
  208. this.defaultExpandedKey = []
  209. this.clearSelected()
  210. this.$emit('getValue', null, null, null)
  211. },
  212. /* 清空选中样式 */
  213. clearSelected () {
  214. let allNode = document.querySelectorAll('#tree-option .el-tree-node')
  215. allNode.forEach((element) => element.classList.remove('is-current'))
  216. }
  217. },
  218. watch: {
  219. value () {
  220. this.valueId = this.value
  221. if (this.value === '' || this.value === null || this.value === undefined) {
  222. this.clearHandle()
  223. } else {
  224. this.initHandle()
  225. }
  226. },
  227. data () {
  228. this.valueData = this.data
  229. }
  230. },
  231. computed: {
  232. optionData () {
  233. if (this.list) {
  234. let cloneData = JSON.parse(JSON.stringify(this.list)) // 对源数据深度克隆
  235. return cloneData.filter(father => { // 循环所有项,并添加children属性
  236. let branchArr = cloneData.filter(child => father.id === child.parentId) // 返回每一项的子级数组
  237. // eslint-disable-next-line no-unused-expressions
  238. branchArr.length > 0 ? father.children = branchArr : '' // 给父级添加一个children属性,并赋值
  239. return father.parentId === '0' // 返回第一层
  240. })
  241. } else {
  242. return this.valueData
  243. }
  244. }
  245. }
  246. }
  247. </script>
  248. <!-- Add "scoped" attribute to limit CSS to this component only -->
  249. <style scoped>
  250. .el-select{
  251. width: 100%;
  252. }
  253. .el-scrollbar .el-scrollbar__view .el-select-dropdown__item{
  254. height: auto;
  255. max-height: 274px;
  256. padding: 0;
  257. overflow: hidden;
  258. overflow-y: auto;
  259. }
  260. .el-select-dropdown__item.selected{
  261. font-weight: normal;
  262. }
  263. ul li >>>.el-tree .el-tree-node__content{
  264. height:auto;
  265. padding: 0 20px;
  266. }
  267. .el-tree-node__label{
  268. font-weight: normal;
  269. }
  270. .el-tree >>>.is-current .el-tree-node__label{
  271. color: #409EFF;
  272. font-weight: 700;
  273. }
  274. .el-tree >>>.is-current .el-tree-node__children .el-tree-node__label{
  275. color:#606266;
  276. font-weight: normal;
  277. }
  278. /* 开发禁用 */
  279. /* .el-tree-node:focus>.el-tree-node__content{
  280. background-color:transparent;
  281. background-color: #f5f7fa;
  282. color: #c0c4cc;
  283. cursor: not-allowed;
  284. }
  285. .el-tree-node__content:hover{
  286. background-color: #f5f7fa;
  287. } */
  288. </style>