推广小程序后台代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

283 lines
7.5 KiB

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