treeReimbursementBuinessUserSelect.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <template>
  2. <el-select :value="valueTitle" :size="size" :disabled="disabled" :clearable="clearable" :placeholder="placeholderText" @clear="clearHandle" filterable :filter-method="selectTree">
  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. :filter-node-method="filterNode"
  14. @check-change="handleCheckChange"
  15. @node-click="handleNodeClick">
  16. </el-tree>
  17. </el-option>
  18. </el-select>
  19. </template>
  20. <script>
  21. export default {
  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. filterNode (value, data) {
  136. if (!value) return true
  137. return data.name.indexOf(value) !== -1
  138. },
  139. selectTree (val) {
  140. this.$refs.selectTree.filter(val)
  141. },
  142. // 初始化值
  143. initHandle () {
  144. if (this.valueId) {
  145. if (this.showCheckbox) {
  146. let ids = this.valueId.split(',')
  147. this.$refs.selectTree.setCheckedKeys(ids)
  148. let titles = []
  149. ids.forEach((id) => {
  150. this.treeList.forEach((d) => {
  151. if (id === d[this.props.value]) {
  152. titles.push(d[this.props.label])
  153. }
  154. })
  155. })
  156. this.valueTitle = titles.join(',')
  157. } else if (this.$refs.selectTree.getNode(this.valueId)) {
  158. this.valueTitle = this.$refs.selectTree.getNode(this.valueId).data[this.props.label] // 初始化显示
  159. this.$refs.selectTree.setCurrentKey(this.valueId) // 设置默认选中
  160. this.defaultExpandedKey = [this.valueId] // 设置默认展开
  161. }
  162. }
  163. this.initScroll()
  164. },
  165. getNode (id) {
  166. return this.$refs.selectTree.getNode(id)
  167. },
  168. // 初始化滚动条
  169. initScroll () {
  170. this.$nextTick(() => {
  171. let scrollWrap = document.querySelectorAll('.el-scrollbar .el-select-dropdown__wrap')[0]
  172. let scrollBar = document.querySelectorAll('.el-scrollbar .el-scrollbar__bar')
  173. if (scrollWrap) { scrollWrap.style.cssText = 'margin: 0px; max-height: none; overflow: hidden;' }
  174. if (scrollBar) {
  175. scrollBar.forEach(ele => {
  176. // eslint-disable-next-line no-return-assign
  177. return ele.style.width = 0
  178. })
  179. }
  180. })
  181. },
  182. // 切换选项
  183. handleNodeClick (node) {
  184. if (this.showCheckbox) {
  185. return
  186. }
  187. if (node['disabled'] || node['typeFlag']) {
  188. // this.$message.warning('节点(' + node[this.props.label] + ')被禁止选择,请重新选择。')
  189. return
  190. }
  191. if (this.isOnlySelectLeaf && node.children.length > 0) {
  192. // this.$message.warning('不能选择根节点(' + node[this.props.label] + ')请重新选择。')
  193. return
  194. }
  195. this.valueTitle = node[this.props.label]
  196. this.valueId = node[this.props.value]
  197. this.$emit('getValue', this.valueId, this.valueTitle, node)
  198. },
  199. handleCheckChange (data, checked, indeterminate) {
  200. let nodes = this.$refs.selectTree.getCheckedNodes()
  201. this.valueTitle = nodes.map((node) => {
  202. return node[this.props.label]
  203. }).join(',')
  204. this.valueId = nodes.map((node) => {
  205. return node[this.props.value]
  206. }).join(',')
  207. this.$emit('getValue', this.valueId, this.valueTitle)
  208. },
  209. // 清除选中
  210. clearHandle () {
  211. this.valueTitle = ''
  212. this.valueId = null
  213. this.defaultExpandedKey = []
  214. this.clearSelected()
  215. this.$emit('getValue', null, null, null)
  216. },
  217. /* 清空选中样式 */
  218. clearSelected () {
  219. let allNode = document.querySelectorAll('#tree-option .el-tree-node')
  220. allNode.forEach((element) => element.classList.remove('is-current'))
  221. }
  222. },
  223. watch: {
  224. value () {
  225. this.valueId = this.value
  226. if (this.value === '' || this.value === null || this.value === undefined) {
  227. this.clearHandle()
  228. } else {
  229. this.initHandle()
  230. }
  231. },
  232. data () {
  233. this.valueData = this.data
  234. }
  235. },
  236. computed: {
  237. optionData () {
  238. if (this.list) {
  239. let cloneData = JSON.parse(JSON.stringify(this.list)) // 对源数据深度克隆
  240. return cloneData.filter(father => { // 循环所有项,并添加children属性
  241. let branchArr = cloneData.filter(child => father.id === child.parentId) // 返回每一项的子级数组
  242. // eslint-disable-next-line no-unused-expressions
  243. branchArr.length > 0 ? father.children = branchArr : '' // 给父级添加一个children属性,并赋值
  244. return father.parentId === '0' // 返回第一层
  245. })
  246. } else {
  247. return this.valueData
  248. }
  249. }
  250. }
  251. }
  252. </script>
  253. <!-- Add "scoped" attribute to limit CSS to this component only -->
  254. <style scoped>
  255. .el-select{
  256. width: 100%;
  257. }
  258. .el-scrollbar .el-scrollbar__view .el-select-dropdown__item{
  259. height: auto;
  260. max-height: 274px;
  261. padding: 0;
  262. overflow: hidden;
  263. overflow-y: auto;
  264. }
  265. .el-select-dropdown__item.selected{
  266. font-weight: normal;
  267. }
  268. ul li >>>.el-tree .el-tree-node__content{
  269. height:auto;
  270. padding: 0 20px;
  271. }
  272. .el-tree-node__label{
  273. font-weight: normal;
  274. }
  275. .el-tree >>>.is-current .el-tree-node__label{
  276. color: #409EFF;
  277. font-weight: 700;
  278. }
  279. .el-tree >>>.is-current .el-tree-node__children .el-tree-node__label{
  280. color:#606266;
  281. font-weight: normal;
  282. }
  283. /* 开发禁用 */
  284. /* .el-tree-node:focus>.el-tree-node__content{
  285. background-color:transparent;
  286. background-color: #f5f7fa;
  287. color: #c0c4cc;
  288. cursor: not-allowed;
  289. }
  290. .el-tree-node__content:hover{
  291. background-color: #f5f7fa;
  292. } */
  293. </style>