JTreeSelect.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <template>
  2. <a-tree-select
  3. allowClear
  4. labelInValue
  5. :getPopupContainer="(node) => node.parentNode"
  6. style="width: 100%"
  7. :disabled="disabled"
  8. :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
  9. :placeholder="placeholder"
  10. :loadData="asyncLoadTreeData"
  11. :value="treeValue"
  12. :treeData="treeData"
  13. :multiple="multiple"
  14. @change="onChange"
  15. @search="onSearch"
  16. >
  17. </a-tree-select>
  18. </template>
  19. <script>
  20. /*
  21. * 异步树加载组件 通过传入表名 显示字段 存储字段 加载一个树控件
  22. * <j-tree-select dict="aa_tree_test,aad,id" pid-field="pid" ></j-tree-select>
  23. * */
  24. import { getAction } from '@/api/manage'
  25. export default {
  26. name: 'JTreeSelect',
  27. props: {
  28. value: {
  29. type: String,
  30. required: false,
  31. },
  32. placeholder: {
  33. type: String,
  34. default: '请选择',
  35. required: false,
  36. },
  37. dict: {
  38. type: String,
  39. default: '',
  40. required: false,
  41. },
  42. pidField: {
  43. type: String,
  44. default: 'pid',
  45. required: false,
  46. },
  47. pidValue: {
  48. type: String,
  49. default: '',
  50. required: false,
  51. },
  52. disabled: {
  53. type: Boolean,
  54. default: false,
  55. required: false,
  56. },
  57. hasChildField: {
  58. type: String,
  59. default: '',
  60. required: false,
  61. },
  62. condition: {
  63. type: String,
  64. default: '',
  65. required: false,
  66. },
  67. // 是否支持多选
  68. multiple: {
  69. type: Boolean,
  70. default: false,
  71. },
  72. loadTriggleChange: {
  73. type: Boolean,
  74. default: false,
  75. required: false,
  76. },
  77. },
  78. data() {
  79. return {
  80. treeValue: null,
  81. treeData: [],
  82. url: '/sys/dict/loadTreeData',
  83. view: '/sys/dict/loadDictItem/',
  84. tableName: '',
  85. text: '',
  86. code: '',
  87. }
  88. },
  89. watch: {
  90. value() {
  91. this.loadItemByCode()
  92. },
  93. dict() {
  94. this.initDictInfo()
  95. this.loadRoot()
  96. },
  97. },
  98. created() {
  99. this.validateProp().then(() => {
  100. this.initDictInfo()
  101. this.loadRoot()
  102. this.loadItemByCode()
  103. })
  104. },
  105. methods: {
  106. loadItemByCode() {
  107. if (!this.value || this.value == '0') {
  108. this.treeValue = null
  109. } else {
  110. getAction(`${this.view}${this.dict}`, { key: this.value }).then((res) => {
  111. if (res.success) {
  112. let values = this.value.split(',')
  113. this.treeValue = res.result.map((item, index) => ({
  114. key: values[index],
  115. value: values[index],
  116. label: item,
  117. }))
  118. this.onLoadTriggleChange(res.result[0])
  119. }
  120. })
  121. }
  122. },
  123. onLoadTriggleChange(text) {
  124. //只有单选才会触发
  125. if (!this.multiple && this.loadTriggleChange) {
  126. this.$emit('change', this.value, text)
  127. }
  128. },
  129. initDictInfo() {
  130. let arr = this.dict.split(',')
  131. this.tableName = arr[0]
  132. this.text = arr[1]
  133. this.code = arr[2]
  134. },
  135. asyncLoadTreeData(treeNode) {
  136. return new Promise((resolve) => {
  137. if (treeNode.$vnode.children) {
  138. resolve()
  139. return
  140. }
  141. let pid = treeNode.$vnode.key
  142. let param = {
  143. pid: pid,
  144. tableName: this.tableName,
  145. text: this.text,
  146. code: this.code,
  147. pidField: this.pidField,
  148. hasChildField: this.hasChildField,
  149. condition: this.condition,
  150. }
  151. getAction(this.url, param).then((res) => {
  152. if (res.success) {
  153. for (let i of res.result) {
  154. i.value = i.key
  155. if (i.leaf == false) {
  156. i.isLeaf = false
  157. } else if (i.leaf == true) {
  158. i.isLeaf = true
  159. }
  160. }
  161. this.addChildren(pid, res.result, this.treeData)
  162. this.treeData = [...this.treeData]
  163. }
  164. resolve()
  165. })
  166. })
  167. },
  168. addChildren(pid, children, treeArray) {
  169. if (treeArray && treeArray.length > 0) {
  170. for (let item of treeArray) {
  171. if (item.key == pid) {
  172. if (!children || children.length == 0) {
  173. item.isLeaf = true
  174. } else {
  175. item.children = children
  176. }
  177. break
  178. } else {
  179. this.addChildren(pid, children, item.children)
  180. }
  181. }
  182. }
  183. },
  184. loadRoot() {
  185. let param = {
  186. pid: this.pidValue,
  187. tableName: this.tableName,
  188. text: this.text,
  189. code: this.code,
  190. pidField: this.pidField,
  191. hasChildField: this.hasChildField,
  192. condition: this.condition,
  193. }
  194. getAction(this.url, param).then((res) => {
  195. if (res.success && res.result) {
  196. for (let i of res.result) {
  197. i.value = i.key
  198. if (i.leaf == false) {
  199. i.isLeaf = false
  200. } else if (i.leaf == true) {
  201. i.isLeaf = true
  202. }
  203. }
  204. this.treeData = [...res.result]
  205. } else {
  206. console.log('数根节点查询结果-else', res)
  207. }
  208. })
  209. },
  210. onChange(value) {
  211. if (!value) {
  212. this.$emit('change', '')
  213. this.treeValue = null
  214. } else if (value instanceof Array) {
  215. this.$emit('change', value.map((item) => item.value).join(','))
  216. this.treeValue = value
  217. } else {
  218. this.$emit('change', value.value, value.label)
  219. this.treeValue = value
  220. }
  221. },
  222. onSearch(value) {
  223. console.log(value)
  224. },
  225. getCurrTreeData() {
  226. return this.treeData
  227. },
  228. validateProp() {
  229. let mycondition = this.condition
  230. return new Promise((resolve, reject) => {
  231. if (!mycondition) {
  232. resolve()
  233. } else {
  234. try {
  235. let test = JSON.parse(mycondition)
  236. console.log('aaaaasdsdd', typeof test)
  237. if (typeof test == 'object' && test) {
  238. resolve()
  239. } else {
  240. this.$message.error('组件JTreeSelect-condition传值有误,需要一个json字符串!')
  241. reject()
  242. }
  243. } catch (e) {
  244. this.$message.error('组件JTreeSelect-condition传值有误,需要一个json字符串!')
  245. reject()
  246. }
  247. }
  248. })
  249. },
  250. },
  251. //2.2新增 在组件内定义 指定父组件调用时候的传值属性和事件类型 这个牛逼
  252. model: {
  253. prop: 'value',
  254. event: 'change',
  255. },
  256. }
  257. </script>