treeUserSelect.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <template>
  2. <el-select ref="selectCom" :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. name: 'el-tree-select',
  22. props: {
  23. /* 配置项 */
  24. props: {
  25. type: Object,
  26. default: () => {
  27. return {
  28. value: 'id', // ID字段名
  29. label: 'label', // 显示名称
  30. children: 'children' // 子级字段名
  31. }
  32. }
  33. },
  34. /* 选项列表数据(树形结构的对象数组) */
  35. data: {
  36. type: Array,
  37. default: () => { return [] }
  38. },
  39. /* 选项列表数据(树形结构的对象数组) */
  40. list: {
  41. type: Array,
  42. default: () => { return null }
  43. },
  44. /* 初始值 */
  45. value: {
  46. type: String,
  47. default: () => { return null }
  48. },
  49. /* 初始值 */
  50. url: {
  51. type: String,
  52. default: () => { return null }
  53. },
  54. disabled: {
  55. type: Boolean,
  56. dafault: () => { return false }
  57. },
  58. showCheckbox: {
  59. type: Boolean,
  60. dafault: () => { return false }
  61. },
  62. /* 初始值 */
  63. label: {
  64. type: String,
  65. default: () => { return null }
  66. },
  67. /* 可清空选项 */
  68. clearable: {
  69. type: Boolean,
  70. default: () => { return true }
  71. },
  72. /* 自动收起 */
  73. accordion: {
  74. type: Boolean,
  75. default: () => { return false }
  76. },
  77. size: {
  78. type: String,
  79. default: () => { return 'small' }
  80. },
  81. placeholder: {
  82. type: String,
  83. default: () => { return '请选择' }
  84. },
  85. isOnlySelectLeaf: {
  86. type: Boolean,
  87. default: () => {
  88. return false
  89. }
  90. }
  91. },
  92. data () {
  93. return {
  94. valueId: this.value, // 初始值
  95. valueTitle: this.label,
  96. defaultExpandedKey: [],
  97. placeholderText: this.placeholder,
  98. treeList: [],
  99. valueData: this.data
  100. }
  101. },
  102. created () {
  103. if (this.url !== null) {
  104. this.placeholderText = '加载数据中...'
  105. let interval = setInterval(() => {
  106. this.placeholderText = this.placeholderText + '.'
  107. }, 500)
  108. this.$http({
  109. url: this.url,
  110. method: 'get'
  111. }).then(({data}) => {
  112. this.valueData = data
  113. this.setTreeList(this.valueData)
  114. this.$nextTick(() => {
  115. this.initHandle()
  116. this.placeholderText = this.placeholder
  117. clearInterval(interval)
  118. })
  119. })
  120. } else {
  121. this.valueData = this.data
  122. this.setTreeList(this.valueData)
  123. }
  124. },
  125. methods: {
  126. setTreeList (datas) { // 遍历树 获取id数组
  127. for (var i in datas) {
  128. this.treeList.push(datas[i])
  129. if (datas[i].children) {
  130. this.setTreeList(datas[i].children)
  131. }
  132. }
  133. },
  134. // 初始化值
  135. initHandle () {
  136. if (this.valueId) {
  137. if (this.showCheckbox) {
  138. let ids = this.valueId.split(',')
  139. this.$refs.selectTree.setCheckedKeys(ids)
  140. let titles = []
  141. ids.forEach((id) => {
  142. this.treeList.forEach((d) => {
  143. if (id === d[this.props.value]) {
  144. titles.push(d[this.props.label])
  145. }
  146. })
  147. })
  148. this.valueTitle = titles.join(',')
  149. } else if (this.$refs.selectTree.getNode(this.valueId)) {
  150. this.valueTitle = this.$refs.selectTree.getNode(this.valueId).data[this.props.label] // 初始化显示
  151. this.$refs.selectTree.setCurrentKey(this.valueId) // 设置默认选中
  152. this.defaultExpandedKey = [this.valueId] // 设置默认展开
  153. }
  154. }
  155. this.initScroll()
  156. },
  157. getNode (id) {
  158. return this.$refs.selectTree.getNode(id)
  159. },
  160. // 初始化滚动条
  161. initScroll () {
  162. this.$nextTick(() => {
  163. let scrollWrap = document.querySelectorAll('.el-scrollbar .el-select-dropdown__wrap')[0]
  164. let scrollBar = document.querySelectorAll('.el-scrollbar .el-scrollbar__bar')
  165. if (scrollWrap) { scrollWrap.style.cssText = 'margin: 0px; max-height: none; overflow: hidden;' }
  166. if (scrollBar) {
  167. scrollBar.forEach(ele => {
  168. // eslint-disable-next-line no-return-assign
  169. return ele.style.width = 0
  170. })
  171. }
  172. })
  173. },
  174. // 切换选项
  175. handleNodeClick (node) {
  176. if (this.showCheckbox) {
  177. return
  178. }
  179. if (node['disabled'] || node['typeFlag']) {
  180. // this.$message.warning('节点(' + node[this.props.label] + ')被禁止选择,请重新选择。')
  181. return
  182. }
  183. if (this.isOnlySelectLeaf && node.children.length > 0) {
  184. // this.$message.warning('不能选择根节点(' + node[this.props.label] + ')请重新选择。')
  185. return
  186. }
  187. this.valueTitle = node[this.props.label]
  188. this.valueId = node[this.props.value]
  189. this.$emit('getValue', this.valueId, this.valueTitle, node)
  190. this.$refs.selectCom.blur()
  191. },
  192. handleCheckChange (data, checked, indeterminate) {
  193. let nodes = this.$refs.selectTree.getCheckedNodes()
  194. this.valueTitle = nodes.map((node) => {
  195. return node[this.props.label]
  196. }).join(',')
  197. this.valueId = nodes.map((node) => {
  198. return node[this.props.value]
  199. }).join(',')
  200. this.$emit('getValue', this.valueId, this.valueTitle)
  201. this.$refs.selectCom.blur()
  202. },
  203. // 清除选中
  204. clearHandle () {
  205. this.valueTitle = ''
  206. this.valueId = null
  207. this.defaultExpandedKey = []
  208. this.clearSelected()
  209. this.$emit('getValue', null, null, null)
  210. this.$refs.selectCom.blur()
  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>