326036c23d6e4e18b6156662b8000eaea0bd772e.svn-base 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <template>
  2. <a-select
  3. ref="select"
  4. :value="innerValue"
  5. allowClear
  6. :filterOption="handleSelectFilterOption"
  7. v-bind="selectProps"
  8. style="width: 100%;"
  9. @blur="handleBlur"
  10. @change="handleChange"
  11. @search="handleSearchSelect"
  12. >
  13. <div v-if="loading" slot="notFoundContent">
  14. <a-icon type="loading" />
  15. <span>&nbsp;加载中…</span>
  16. </div>
  17. <template v-for="option of selectOptions">
  18. <a-select-option :key="option.value" :value="option.value" :disabled="option.disabled">
  19. <span>{{option.text || option.label || option.title|| option.value}}</span>
  20. </a-select-option>
  21. </template>
  22. </a-select>
  23. </template>
  24. <script>
  25. import JVxeCellMixins, { dispatchEvent } from '@/components/jeecg/JVxeTable/mixins/JVxeCellMixins'
  26. import { JVXETypes } from '@comp/jeecg/JVxeTable/index'
  27. import { filterDictText } from '@comp/dict/JDictSelectUtil'
  28. export default {
  29. name: 'JVxeSelectCell',
  30. mixins: [JVxeCellMixins],
  31. data(){
  32. return {
  33. loading: false,
  34. // 异步加载的options(用于多级联动)
  35. asyncOptions: null,
  36. }
  37. },
  38. computed: {
  39. selectProps() {
  40. let props = {...this.cellProps}
  41. // 判断select是否允许输入
  42. let {allowSearch, allowInput} = this.originColumn
  43. if (allowInput === true || allowSearch === true) {
  44. props['showSearch'] = true
  45. }
  46. return props
  47. },
  48. // 下拉选项
  49. selectOptions() {
  50. if (this.asyncOptions) {
  51. return this.asyncOptions
  52. }
  53. let {linkage} = this.renderOptions
  54. if (linkage) {
  55. let {getLinkageOptionsSibling, config} = linkage
  56. let res = getLinkageOptionsSibling(this.row, this.originColumn, config, true)
  57. // 当返回Promise时,说明是多级联动
  58. if (res instanceof Promise) {
  59. this.loading = true
  60. res.then(opt => {
  61. this.asyncOptions = opt
  62. this.loading = false
  63. }).catch(e => {
  64. console.error(e)
  65. this.loading = false
  66. })
  67. } else {
  68. this.asyncOptions = null
  69. return res
  70. }
  71. }
  72. return this.originColumn.options
  73. },
  74. },
  75. created() {
  76. let multiple = [JVXETypes.selectMultiple, JVXETypes.list_multi]
  77. let search = [JVXETypes.selectSearch, JVXETypes.sel_search]
  78. if (multiple.includes(this.$type)) {
  79. // 处理多选
  80. let props = this.originColumn.props || {}
  81. props['mode'] = 'multiple'
  82. props['maxTagCount'] = 1
  83. this.$set(this.originColumn, 'props', props)
  84. } else if (search.includes(this.$type)) {
  85. // 处理搜索
  86. this.$set(this.originColumn, 'allowSearch', true)
  87. }
  88. },
  89. methods: {
  90. handleChange(value) {
  91. debugger
  92. // 处理下级联动
  93. let linkage = this.renderOptions.linkage
  94. if (linkage) {
  95. linkage.linkageSelectChange(this.row, this.originColumn, linkage.config, value)
  96. }
  97. this.handleChangeCommon(value)
  98. },
  99. /** 处理blur失去焦点事件 */
  100. handleBlur(value) {
  101. let {allowInput, options} = this.originColumn
  102. if (allowInput === true) {
  103. // 删除无用的因搜索(用户输入)而创建的项
  104. if (typeof value === 'string') {
  105. let indexes = []
  106. options.forEach((option, index) => {
  107. if (option.value.toLocaleString() === value.toLocaleString()) {
  108. delete option.searchAdd
  109. } else if (option.searchAdd === true) {
  110. indexes.push(index)
  111. }
  112. })
  113. // 翻转删除数组中的项
  114. for (let index of indexes.reverse()) {
  115. options.splice(index, 1)
  116. }
  117. }
  118. }
  119. this.handleBlurCommon(value)
  120. },
  121. /** 用于搜索下拉框中的内容 */
  122. handleSelectFilterOption(input, option) {
  123. let {allowSearch, allowInput} = this.originColumn
  124. if (allowSearch === true || allowInput === true) {
  125. //update-begin-author:taoyan date:20200820 for:【专项任务】大连项目反馈行编辑问题处理 下拉框搜索
  126. return option.componentOptions.children[0].children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
  127. //update-end-author:taoyan date:20200820 for:【专项任务】大连项目反馈行编辑问题处理 下拉框搜索
  128. }
  129. return true
  130. },
  131. /** select 搜索时的事件,用于动态添加options */
  132. handleSearchSelect(value) {
  133. let {allowSearch, allowInput, options} = this.originColumn
  134. if (allowSearch !== true && allowInput === true) {
  135. // 是否找到了对应的项,找不到则添加这一项
  136. let flag = false
  137. for (let option of options) {
  138. if (option.value.toLocaleString() === value.toLocaleString()) {
  139. flag = true
  140. break
  141. }
  142. }
  143. // !!value :不添加空值
  144. if (!flag && !!value) {
  145. // searchAdd 是否是通过搜索添加的
  146. options.push({title: value, value: value, searchAdd: true})
  147. }
  148. }
  149. },
  150. },
  151. // 【组件增强】注释详见:JVxeCellMixins.js
  152. enhanced: {
  153. aopEvents: {
  154. editActived(event) {
  155. dispatchEvent.call(this, event, 'ant-select')
  156. },
  157. },
  158. translate: {
  159. enabled: true,
  160. async handler(value,) {
  161. let options
  162. let {linkage} = this.renderOptions
  163. // 判断是否是多级联动,如果是就通过接口异步翻译
  164. if (linkage) {
  165. let {getLinkageOptionsSibling, config} = linkage
  166. options = getLinkageOptionsSibling(this.row, this.originColumn, config, true)
  167. if (options instanceof Promise) {
  168. return new Promise(resolve => {
  169. options.then(opt => {
  170. resolve(filterDictText(opt, value))
  171. })
  172. })
  173. }
  174. } else {
  175. options = this.column.own.options
  176. }
  177. return filterDictText(options, value)
  178. },
  179. },
  180. getValue(value) {
  181. if (Array.isArray(value)) {
  182. return value.join(',')
  183. } else {
  184. return value
  185. }
  186. },
  187. setValue(value) {
  188. let {column: {own: col}, params: {$table}} = this
  189. // 判断是否是多选
  190. if ((col.props || {})['mode'] === 'multiple') {
  191. $table.$set(col.props, 'maxTagCount', 1)
  192. }
  193. if (value != null && value !== '') {
  194. if (typeof value === 'string') {
  195. return value === '' ? [] : value.split(',')
  196. }
  197. return value
  198. } else {
  199. return undefined
  200. }
  201. }
  202. }
  203. }
  204. </script>
  205. <style scoped>
  206. </style>