ea81e18b0946c09399df429f1a09f8231096ae3d.svn-base 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <template>
  2. <j-modal
  3. centered
  4. :title="name + '选择'"
  5. :width="width"
  6. :visible="visible"
  7. switchFullscreen
  8. @ok="handleOk"
  9. @cancel="close"
  10. cancelText="关闭">
  11. <a-row :gutter="18">
  12. <a-col :span="16">
  13. <!-- 查询区域 -->
  14. <a-form layout="inline" class="j-inline-form">
  15. <!-- 固定条件 -->
  16. <a-form-item :label="(queryParamText||name)">
  17. <a-input v-model="queryParam[queryParamCode||valueKey]" :placeholder="'请输入' + (queryParamText||name)" @pressEnter="searchQuery"/>
  18. </a-form-item>
  19. <!-- 动态生成的查询条件 -->
  20. <j-select-biz-query-item v-if="queryConfig.length>0" v-show="showMoreQueryItems" :queryParam="queryParam" :queryConfig="queryConfig" @pressEnter="searchQuery"/>
  21. <!-- 按钮 -->
  22. <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
  23. <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
  24. <a v-if="queryConfig.length>0" @click="showMoreQueryItems=!showMoreQueryItems" style="margin-left: 8px">
  25. {{ showMoreQueryItems ? '收起' : '展开' }}
  26. <a-icon :type="showMoreQueryItems ? 'up' : 'down'"/>
  27. </a>
  28. </a-form>
  29. <a-table
  30. size="middle"
  31. bordered
  32. :rowKey="rowKey"
  33. :columns="innerColumns"
  34. :dataSource="dataSource"
  35. :pagination="ipagination"
  36. :loading="loading"
  37. :scroll="{ y: 240 }"
  38. :rowSelection="{selectedRowKeys, onChange: onSelectChange, type: multiple ? 'checkbox':'radio'}"
  39. :customRow="customRowFn"
  40. @change="handleTableChange">
  41. </a-table>
  42. </a-col>
  43. <a-col :span="8">
  44. <a-card :title="'已选' + name" :bordered="false" :head-style="{padding:0}" :body-style="{padding:0}">
  45. <a-table size="middle" :rowKey="rowKey" bordered v-bind="selectedTable">
  46. <span slot="action" slot-scope="text, record, index">
  47. <a @click="handleDeleteSelected(record, index)">删除</a>
  48. </span>
  49. </a-table>
  50. </a-card>
  51. </a-col>
  52. </a-row>
  53. </j-modal>
  54. </template>
  55. <script>
  56. import { getAction } from '@/api/manage'
  57. import Ellipsis from '@/components/Ellipsis'
  58. import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  59. import { cloneObject, pushIfNotExist } from '@/utils/util'
  60. import JSelectBizQueryItem from './JSelectBizQueryItem'
  61. export default {
  62. name: 'JSelectBizComponentModal',
  63. mixins: [JeecgListMixin],
  64. components: {Ellipsis, JSelectBizQueryItem},
  65. props: {
  66. value: {
  67. type: Array,
  68. default: () => []
  69. },
  70. visible: {
  71. type: Boolean,
  72. default: false
  73. },
  74. valueKey: {
  75. type: String,
  76. required: true
  77. },
  78. multiple: {
  79. type: Boolean,
  80. default: true
  81. },
  82. width: {
  83. type: Number,
  84. default: 900
  85. },
  86. name: {
  87. type: String,
  88. default: ''
  89. },
  90. listUrl: {
  91. type: String,
  92. required: true,
  93. default: ''
  94. },
  95. // 根据 value 获取显示文本的地址,例如存的是 username,可以通过该地址获取到 realname
  96. valueUrl: {
  97. type: String,
  98. default: ''
  99. },
  100. displayKey: {
  101. type: String,
  102. default: null
  103. },
  104. columns: {
  105. type: Array,
  106. required: true,
  107. default: () => []
  108. },
  109. // 查询条件Code
  110. queryParamCode: {
  111. type: String,
  112. default: null
  113. },
  114. // 查询条件文字
  115. queryParamText: {
  116. type: String,
  117. default: null
  118. },
  119. // 查询配置
  120. queryConfig: {
  121. type: Array,
  122. default: () => []
  123. },
  124. rowKey: {
  125. type: String,
  126. default: 'id'
  127. },
  128. // 过长裁剪长度,设置为 -1 代表不裁剪
  129. ellipsisLength: {
  130. type: Number,
  131. default: 12
  132. },
  133. },
  134. data() {
  135. return {
  136. innerValue: [],
  137. // 已选择列表
  138. selectedTable: {
  139. pagination: false,
  140. scroll: { y: 240 },
  141. columns: [
  142. {
  143. ...this.columns[0],
  144. width: this.columns[0].widthRight || this.columns[0].width,
  145. },
  146. { title: '操作', dataIndex: 'action', align: 'center', width: 60, scopedSlots: { customRender: 'action' }, }
  147. ],
  148. dataSource: [],
  149. },
  150. renderEllipsis: (value) => (<ellipsis length={this.ellipsisLength}>{value}</ellipsis>),
  151. url: { list: this.listUrl },
  152. /* 分页参数 */
  153. ipagination: {
  154. current: 1,
  155. pageSize: 5,
  156. pageSizeOptions: ['5', '10', '20', '30'],
  157. showTotal: (total, range) => {
  158. return range[0] + '-' + range[1] + ' 共' + total + '条'
  159. },
  160. showQuickJumper: true,
  161. showSizeChanger: true,
  162. total: 0
  163. },
  164. options: [],
  165. dataSourceMap: {},
  166. showMoreQueryItems: false,
  167. }
  168. },
  169. computed: {
  170. // 表头
  171. innerColumns() {
  172. let columns = cloneObject(this.columns)
  173. columns.forEach(column => {
  174. // 给所有的列加上过长裁剪
  175. if (this.ellipsisLength !== -1) {
  176. column.customRender = (text) => this.renderEllipsis(text)
  177. }
  178. })
  179. return columns
  180. },
  181. },
  182. watch: {
  183. value: {
  184. deep: true,
  185. immediate: true,
  186. handler(val) {
  187. this.innerValue = cloneObject(val)
  188. this.selectedRowKeys = []
  189. this.valueWatchHandler(val)
  190. this.queryOptionsByValue(val)
  191. }
  192. },
  193. dataSource: {
  194. deep: true,
  195. handler(val) {
  196. this.emitOptions(val)
  197. this.valueWatchHandler(this.innerValue)
  198. }
  199. },
  200. selectedRowKeys: {
  201. immediate: true,
  202. deep: true,
  203. handler(val) {
  204. //update--begin--autor:scott-----date:20200927------for:选取职务名称出现全选 #1753-----
  205. if(this.innerValue){
  206. this.innerValue.length=0;
  207. }
  208. //update--end--autor:scott-----date:20200927------for:选取职务名称出现全选 #1753-----
  209. this.selectedTable.dataSource = val.map(key => {
  210. for (let data of this.dataSource) {
  211. if (data[this.rowKey] === key) {
  212. pushIfNotExist(this.innerValue, data[this.valueKey])
  213. return data
  214. }
  215. }
  216. for (let data of this.selectedTable.dataSource) {
  217. if (data[this.rowKey] === key) {
  218. pushIfNotExist(this.innerValue, data[this.valueKey])
  219. return data
  220. }
  221. }
  222. console.warn('未找到选择的行信息,key:' + key)
  223. return {}
  224. })
  225. },
  226. }
  227. },
  228. methods: {
  229. /** 关闭弹窗 */
  230. close() {
  231. this.$emit('update:visible', false)
  232. },
  233. valueWatchHandler(val) {
  234. val.forEach(item => {
  235. this.dataSource.concat(this.selectedTable.dataSource).forEach(data => {
  236. if (data[this.valueKey] === item) {
  237. pushIfNotExist(this.selectedRowKeys, data[this.rowKey])
  238. }
  239. })
  240. })
  241. },
  242. queryOptionsByValue(value) {
  243. if (!value || value.length === 0) {
  244. return
  245. }
  246. // 判断options是否存在value,如果已存在数据就不再请求后台了
  247. let notExist = false
  248. for (let val of value) {
  249. let find = false
  250. for (let option of this.options) {
  251. if (val === option.value) {
  252. find = true
  253. break
  254. }
  255. }
  256. if (!find) {
  257. notExist = true
  258. break
  259. }
  260. }
  261. if (!notExist) return
  262. getAction(this.valueUrl || this.listUrl, {
  263. // 这里最后加一个 , 的原因是因为无论如何都要使用 in 查询,防止后台进行了模糊匹配,导致查询结果不准确
  264. [this.valueKey]: value.join(',') + ',',
  265. pageNo: 1,
  266. pageSize: value.length
  267. }).then((res) => {
  268. if (res.success) {
  269. let dataSource = res.result
  270. if (!(dataSource instanceof Array)) {
  271. dataSource = res.result.records
  272. }
  273. this.emitOptions(dataSource, (data) => {
  274. pushIfNotExist(this.innerValue, data[this.valueKey])
  275. pushIfNotExist(this.selectedRowKeys, data[this.rowKey])
  276. pushIfNotExist(this.selectedTable.dataSource, data, this.rowKey)
  277. })
  278. }
  279. })
  280. },
  281. emitOptions(dataSource, callback) {
  282. dataSource.forEach(data => {
  283. let key = data[this.valueKey]
  284. this.dataSourceMap[key] = data
  285. pushIfNotExist(this.options, { label: data[this.displayKey || this.valueKey], value: key }, 'value')
  286. typeof callback === 'function' ? callback(data) : ''
  287. })
  288. this.$emit('options', this.options, this.dataSourceMap)
  289. },
  290. /** 完成选择 */
  291. handleOk() {
  292. let value = this.selectedTable.dataSource.map(data => data[this.valueKey])
  293. this.$emit('input', value)
  294. this.close()
  295. },
  296. /** 删除已选择的 */
  297. handleDeleteSelected(record, index) {
  298. this.selectedRowKeys.splice(this.selectedRowKeys.indexOf(record[this.rowKey]), 1)
  299. //update--begin--autor:wangshuai-----date:20200722------for:JSelectBizComponent组件切换页数值问题------
  300. this.selectedTable.dataSource.splice(this.selectedTable.dataSource.indexOf(record), 1)
  301. this.innerValue.splice(this.innerValue.indexOf(record[this.valueKey]), 1)
  302. console.log("this.selectedRowKeys:",this.selectedRowKeys)
  303. console.log("this.selectedTable.dataSource:",this.selectedTable.dataSource)
  304. //update--begin--autor:wangshuai-----date:20200722------for:JSelectBizComponent组件切换页数值问题------
  305. },
  306. customRowFn(record) {
  307. return {
  308. on: {
  309. click: () => {
  310. let key = record[this.rowKey]
  311. if (!this.multiple) {
  312. this.selectedRowKeys = [key]
  313. this.selectedTable.dataSource = [record]
  314. } else {
  315. let index = this.selectedRowKeys.indexOf(key)
  316. if (index === -1) {
  317. this.selectedRowKeys.push(key)
  318. this.selectedTable.dataSource.push(record)
  319. } else {
  320. this.handleDeleteSelected(record, index)
  321. }
  322. }
  323. }
  324. }
  325. }
  326. },
  327. }
  328. }
  329. </script>
  330. <style lang="less" scoped>
  331. .full-form-item {
  332. display: flex;
  333. margin-right: 0;
  334. /deep/ .ant-form-item-control-wrapper {
  335. flex: 1 1;
  336. display: inline-block;
  337. }
  338. }
  339. .j-inline-form {
  340. /deep/ .ant-form-item {
  341. margin-bottom: 12px;
  342. }
  343. /deep/ .ant-form-item-label {
  344. line-height: 32px;
  345. width: auto;
  346. }
  347. /deep/ .ant-form-item-control {
  348. height: 32px;
  349. line-height: 32px;
  350. }
  351. }
  352. </style>