1b51777be61e4c7bafa68f36659ca4e8726df4bf.svn-base 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <template>
  2. <a-select
  3. v-if="async"
  4. showSearch
  5. labelInValue
  6. :disabled="disabled"
  7. :getPopupContainer="getParentContainer"
  8. @search="loadData"
  9. :placeholder="placeholder"
  10. v-model="selectedAsyncValue"
  11. style="width: 100%"
  12. :filterOption="false"
  13. @change="handleAsyncChange"
  14. allowClear
  15. :notFoundContent="loading ? undefined : null"
  16. >
  17. <a-spin v-if="loading" slot="notFoundContent" size="small"/>
  18. <a-select-option v-for="d in options" :key="d.value" :value="d.value">{{ d.text }}</a-select-option>
  19. </a-select>
  20. <a-select
  21. v-else
  22. :getPopupContainer="getParentContainer"
  23. showSearch
  24. :disabled="disabled"
  25. :placeholder="placeholder"
  26. optionFilterProp="children"
  27. style="width: 100%"
  28. @change="handleChange"
  29. :filterOption="filterOption"
  30. v-model="selectedValue"
  31. allowClear
  32. :notFoundContent="loading ? undefined : null">
  33. <a-spin v-if="loading" slot="notFoundContent" size="small"/>
  34. <a-select-option v-for="d in options" :key="d.value" :value="d.value">{{ d.text }}</a-select-option>
  35. </a-select>
  36. </template>
  37. <script>
  38. import { ajaxGetDictItems,getDictItemsFromCache } from '@/api/api'
  39. import debounce from 'lodash/debounce';
  40. import { getAction } from '../../api/manage'
  41. export default {
  42. name: 'JSearchSelectTag',
  43. props:{
  44. disabled: Boolean,
  45. value: [String, Number],
  46. dict: String,
  47. dictOptions: Array,
  48. async: Boolean,
  49. placeholder:{
  50. type:String,
  51. default:"请选择",
  52. required:false
  53. },
  54. popContainer:{
  55. type:String,
  56. default:'',
  57. required:false
  58. },
  59. pageSize:{
  60. type: Number,
  61. default: 10,
  62. required: false
  63. },
  64. getPopupContainer: {
  65. type:Function,
  66. default: null
  67. },
  68. },
  69. data(){
  70. this.loadData = debounce(this.loadData, 800);//消抖
  71. this.lastLoad = 0;
  72. return {
  73. loading:false,
  74. selectedValue:[],
  75. selectedAsyncValue:[],
  76. options: [],
  77. }
  78. },
  79. created(){
  80. this.initDictData();
  81. },
  82. watch:{
  83. "value":{
  84. immediate:true,
  85. handler(val){
  86. if(!val){
  87. if(val==0){
  88. this.initSelectValue()
  89. }else{
  90. this.selectedValue=[]
  91. this.selectedAsyncValue=[]
  92. }
  93. }else{
  94. this.initSelectValue()
  95. }
  96. }
  97. },
  98. "dict":{
  99. handler(){
  100. this.initDictData()
  101. }
  102. },
  103. 'dictOptions':{
  104. deep: true,
  105. handler(val){
  106. if(val && val.length>0){
  107. this.options = [...val]
  108. }
  109. }
  110. }
  111. },
  112. methods:{
  113. initSelectValue(){
  114. if(this.async){
  115. if(!this.selectedAsyncValue || !this.selectedAsyncValue.key || this.selectedAsyncValue.key!=this.value){
  116. console.log("这才请求后台")
  117. getAction(`/sys/dict/loadDictItem/${this.dict}`,{key:this.value}).then(res=>{
  118. if(res.success){
  119. let obj = {
  120. key:this.value,
  121. label:res.result
  122. }
  123. this.selectedAsyncValue = {...obj}
  124. }
  125. })
  126. }
  127. }else{
  128. this.selectedValue = this.value.toString()
  129. }
  130. },
  131. loadData(value){
  132. console.log("数据加载",value)
  133. this.lastLoad +=1
  134. const currentLoad = this.lastLoad
  135. this.options = []
  136. this.loading=true
  137. // 字典code格式:table,text,code
  138. getAction(`/sys/dict/loadDict/${this.dict}`,{keyword:value, pageSize: this.pageSize}).then(res=>{
  139. this.loading=false
  140. if(res.success){
  141. if(currentLoad!=this.lastLoad){
  142. return
  143. }
  144. this.options = res.result
  145. console.log("我是第一个",res)
  146. }else{
  147. this.$message.warning(res.message)
  148. }
  149. })
  150. },
  151. initDictData(){
  152. if(!this.async){
  153. //如果字典项集合有数据
  154. if(this.dictOptions && this.dictOptions.length>0){
  155. this.options = [...this.dictOptions]
  156. }else{
  157. //根据字典Code, 初始化字典数组
  158. let dictStr = ''
  159. if(this.dict){
  160. let arr = this.dict.split(',')
  161. if(arr[0].indexOf('where')>0){
  162. let tbInfo = arr[0].split('where')
  163. dictStr = tbInfo[0].trim()+','+arr[1]+','+arr[2]+','+encodeURIComponent(tbInfo[1])
  164. }else{
  165. dictStr = this.dict
  166. }
  167. if (this.dict.indexOf(",") == -1) {
  168. //优先从缓存中读取字典配置
  169. if (getDictItemsFromCache(this.dictCode)) {
  170. this.options = getDictItemsFromCache(this.dictCode);
  171. return
  172. }
  173. }
  174. ajaxGetDictItems(dictStr, null).then((res) => {
  175. if (res.success) {
  176. this.options = res.result;
  177. }
  178. })
  179. }
  180. }
  181. }else{
  182. //异步一开始也加载一点数据
  183. this.loading=true
  184. getAction(`/sys/dict/loadDict/${this.dict}`,{pageSize: this.pageSize, keyword:''}).then(res=>{
  185. this.loading=false
  186. if(res.success){
  187. this.options = res.result
  188. }else{
  189. this.$message.warning(res.message)
  190. }
  191. })
  192. }
  193. },
  194. filterOption(input, option) {
  195. return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
  196. },
  197. handleChange (selectedValue) {
  198. console.log("selectedValue",selectedValue)
  199. this.selectedValue = selectedValue
  200. this.callback()
  201. },
  202. handleAsyncChange(selectedObj){
  203. //update-begin-author:scott date:20201222 for:【搜索】搜索查询组件,删除条件,默认下拉还是上次的缓存数据,不好 JT-191
  204. if(selectedObj){
  205. this.selectedAsyncValue = selectedObj
  206. this.selectedValue = selectedObj.key
  207. }else{
  208. this.selectedAsyncValue = null
  209. this.selectedValue = null
  210. this.options = null
  211. this.loadData("")
  212. }
  213. this.callback()
  214. //update-end-author:scott date:20201222 for:【搜索】搜索查询组件,删除条件,默认下拉还是上次的缓存数据,不好 JT-191
  215. },
  216. callback(){
  217. this.$emit('change', this.selectedValue);
  218. },
  219. setCurrentDictOptions(dictOptions){
  220. this.options = dictOptions
  221. },
  222. getCurrentDictOptions(){
  223. return this.options
  224. },
  225. getParentContainer(node){
  226. if(typeof this.getPopupContainer === 'function'){
  227. return this.getPopupContainer(node)
  228. } else if(!this.popContainer){
  229. return node.parentNode
  230. }else{
  231. return document.querySelector(this.popContainer)
  232. }
  233. },
  234. },
  235. model: {
  236. prop: 'value',
  237. event: 'change'
  238. }
  239. }
  240. </script>
  241. <style scoped>
  242. </style>