6579cc1eff33ca3cfc7f4547a53467299650bb34.svn-base 6.0 KB

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