index.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <script>
  2. import { REFER } from "./api/index";
  3. import deepCopy from "@gby/deep-copy";
  4. export default {
  5. name: "PopoverSelect",
  6. props: {
  7. // 参照类型 ,对应后端
  8. type: {
  9. type: String,
  10. require: true,
  11. },
  12. // v-model
  13. value: {
  14. type: [Array, String],
  15. require: true,
  16. },
  17. // 参照弹窗标题
  18. title: {
  19. type: String,
  20. dafault: () => {
  21. return "TITEL";
  22. },
  23. },
  24. // 作为 value 唯一标识的键名,绑定值
  25. valueKey: {
  26. type: String,
  27. dafault: () => {
  28. return "code";
  29. },
  30. },
  31. // 默认查询参数
  32. queryParams: {
  33. type: Function,
  34. default: () => {},
  35. },
  36. // 组件大小
  37. size: {
  38. type: String,
  39. dafault: () => {
  40. return "mini";
  41. },
  42. },
  43. // 提示
  44. placeholder: {
  45. type: String,
  46. dafault: () => {
  47. return "";
  48. },
  49. },
  50. // 是否可读
  51. readonly: {
  52. type: Boolean,
  53. dafault: () => {
  54. return false;
  55. },
  56. },
  57. // 是否禁止
  58. disabled: {
  59. type: Boolean,
  60. dafault: () => {
  61. return false;
  62. },
  63. },
  64. // 是否清除
  65. clearable: {
  66. type: Boolean,
  67. dafault: () => {
  68. return false;
  69. },
  70. },
  71. // 是否多选
  72. multiple: {
  73. type: Boolean,
  74. dafault: () => {
  75. return false;
  76. },
  77. },
  78. // 需映射源数据
  79. source: Object,
  80. // 参照内外映射
  81. dataMapping: Object,
  82. },
  83. components: {},
  84. data() {
  85. return {
  86. width: "50%",
  87. page: { pageNum: 1, pageSize: 10, total: 0 },
  88. visible: false,
  89. loading: false,
  90. model: {
  91. search: "",
  92. isPage: true,
  93. },
  94. data: [],
  95. selectData: [],
  96. lastSelectData: [],
  97. };
  98. },
  99. computed: {
  100. // innerValue() {
  101. // const { value, multiple } = this.$props;
  102. // return multiple ? "" : value;
  103. // },
  104. innerValue:{
  105. get(){
  106. const { value, multiple } = this.$props;
  107. return multiple ? "" : value;
  108. },
  109. set(val){
  110. this.$emit("input", val);
  111. }
  112. },
  113. TableColumnTemp() {
  114. const { type } = this.$props;
  115. const documents = require(`./components/${type}`).default;
  116. return documents.filter((document) => document.key);
  117. },
  118. },
  119. watch: {
  120. "$props.value": {
  121. handler: function (newProp) {
  122. if (!newProp) this.lastSelectData = [];
  123. },
  124. immediate: true,
  125. },
  126. },
  127. methods: {
  128. //
  129. emitChange(prop) {
  130. const { type, source, multiple } = this.$props;
  131. this.$emit("change", multiple ? prop : prop[0], source, type);
  132. },
  133. // open dialog
  134. async open() {
  135. this.visible = true;
  136. await this.useReset();
  137. },
  138. // hide dialog
  139. async hide() {
  140. this.visible = false;
  141. },
  142. // fetch list
  143. async fetchList(prop, page) {
  144. try {
  145. this.loading = true;
  146. const { pageNum, pageSize } = page;
  147. const { code, rows, total } = await REFER(prop, {
  148. pageNum,
  149. pageSize,
  150. });
  151. if (code === 200) {
  152. this.data = rows;
  153. this.page.total = total;
  154. }
  155. } catch (err) {
  156. //
  157. console.error(err);
  158. } finally {
  159. this.loading = false;
  160. }
  161. },
  162. // reset
  163. async useReset() {
  164. const { type, source, queryParams } = this.$props;
  165. this.model = {
  166. type,
  167. search: "",
  168. ...this.model,
  169. ...queryParams(source),
  170. };
  171. await this.fetchList(this.model, this.page);
  172. },
  173. // query
  174. async useQuery() {
  175. await this.fetchList(this.model, this.page);
  176. },
  177. // cancel
  178. useCancel(prop) {
  179. if (prop.length) {
  180. const { multiple } = this.$props;
  181. this.useUpdate(multiple ? prop : prop[0]);
  182. }
  183. this.hide();
  184. },
  185. // confirm
  186. useConfirm(prop) {
  187. const { multiple } = this.$props;
  188. this.useUpdate(multiple ? prop : prop[0]);
  189. this.emitChange(this.selectData);
  190. this.lastSelectData = deepCopy(this.selectData);
  191. this.hide();
  192. },
  193. // delete
  194. useDelete(prop) {
  195. this.selectData.splice(prop, 1);
  196. this.useUpdate(this.selectData);
  197. this.emitChange(this.selectData);
  198. this.lastSelectData = deepCopy(this.selectData);
  199. },
  200. // update
  201. useUpdate(prop) {
  202. const { source, multiple, valueKey, dataMapping } = this.$props;
  203. // update data mapping
  204. if (multiple) {
  205. const vModel = prop.map((item) => item[valueKey]);
  206. this.$emit("input", vModel);
  207. } else {
  208. const vModel = prop[valueKey];
  209. this.$emit("input", vModel);
  210. for (let key in dataMapping) {
  211. source[key] = prop[dataMapping[key]];
  212. }
  213. this.$emit("update:source", source);
  214. }
  215. },
  216. // row click
  217. onceClick(prop) {
  218. const { multiple } = this.$props;
  219. // 单选
  220. if (!multiple) this.$refs.multipleTable.clearSelection();
  221. [prop].forEach((row) => this.$refs.multipleTable.toggleRowSelection(row));
  222. },
  223. // row double click
  224. doubleClick(prop) {
  225. const { multiple } = this.$props;
  226. if (!multiple) this.useConfirm([prop]);
  227. },
  228. // selection change
  229. selectionChange(prop) {
  230. if (prop && prop.length) {
  231. this.selectData = prop;
  232. }
  233. },
  234. handleClear(){
  235. if(!this.$props.multiple){
  236. this.innerValue = '';
  237. }
  238. }
  239. },
  240. created() {},
  241. mounted() {},
  242. destroyed() {},
  243. };
  244. </script>
  245. <template>
  246. <div class="popover-select">
  247. <el-input
  248. v-model="innerValue"
  249. :size="size"
  250. :disabled="disabled"
  251. :readonly="readonly"
  252. :clearable="clearable"
  253. :placeholder="placeholder"
  254. @clear="handleClear"
  255. >
  256. <el-button
  257. :disabled="disabled"
  258. slot="append"
  259. icon="el-icon-search"
  260. @click="open"
  261. ></el-button>
  262. </el-input>
  263. <el-dialog
  264. :title="`${title}(${multiple ? '多选' : '单选'})`"
  265. :width="width"
  266. :visible.sync="visible"
  267. :close-on-click-modal="false"
  268. :close-on-press-escape="false"
  269. append-to-body
  270. >
  271. <el-form
  272. v-loading="loading"
  273. :size="size"
  274. :inline="true"
  275. :model="model"
  276. @submit.native.prevent
  277. >
  278. <el-form-item prop="search">
  279. <el-input
  280. v-model="model.search"
  281. @change="useQuery"
  282. @keydown.enter="useQuery"
  283. >
  284. </el-input>
  285. </el-form-item>
  286. <el-form-item>
  287. <el-button icon="el-icon-refresh" @click="useReset"></el-button>
  288. </el-form-item>
  289. <el-table
  290. ref="multipleTable"
  291. :data="data"
  292. :size="size"
  293. height="45vh"
  294. highlight-current-row
  295. style="width: 100%; margin-bottom: 20px"
  296. @row-click="onceClick"
  297. @row-dblclick="doubleClick"
  298. @selection-change="selectionChange"
  299. >
  300. <el-table-column
  301. v-if="multiple"
  302. width="55"
  303. type="selection"
  304. align="center"
  305. >
  306. </el-table-column>
  307. <el-table-column
  308. v-for="(column, index) in TableColumnTemp"
  309. :key="index"
  310. :prop="column.key"
  311. :label="column.title"
  312. :width="column.width"
  313. show-overflow-tooltip
  314. >
  315. <template slot-scope="scope">
  316. <dr-computed-input
  317. v-if="column.type === 'ComputedInput'"
  318. v-model="scope.row[column.key]"
  319. :source="scope.row"
  320. :computed="column.computed"
  321. :placeholder="column.placeholder"
  322. style="width: 100%"
  323. ></dr-computed-input>
  324. <span v-else> {{ scope.row[column.key] }}</span>
  325. </template>
  326. </el-table-column>
  327. </el-table>
  328. <pagination
  329. :total="page.total"
  330. :page.sync="page.pageNum"
  331. :limit.sync="page.pageSize"
  332. @pagination="useQuery"
  333. />
  334. </el-form>
  335. <div style="margin-top: 20px; text-align: right">
  336. <el-button :size="size" @click="useCancel(lastSelectData)">
  337. 取 消
  338. </el-button>
  339. <el-button :size="size" @click="useConfirm(selectData)">
  340. 确 定
  341. </el-button>
  342. </div>
  343. </el-dialog>
  344. <div
  345. style="
  346. position: absolute;
  347. left: 10px;
  348. top: 50%;
  349. transform: translateY(-50%);
  350. padding-right: 30px;
  351. overflow: hidden;
  352. "
  353. >
  354. <div v-if="multiple && lastSelectData.length">
  355. <el-popover
  356. :offset="-10"
  357. :width="width"
  358. :visible-arrow="false"
  359. title=""
  360. content=""
  361. trigger="click"
  362. placement="bottom-start"
  363. >
  364. <el-tag slot="reference" :size="size" style="margin-right: 10px">
  365. + {{ lastSelectData.length }}
  366. </el-tag>
  367. <el-tag
  368. v-for="(tag, index) in lastSelectData"
  369. :size="size"
  370. hit
  371. closable
  372. :style="{
  373. display: 'flex',
  374. justifyContent: 'space-between',
  375. alignItems: 'center',
  376. margin: lastSelectData.length - 1 === index ? '0' : '0 0 5px 0',
  377. }"
  378. @close="useDelete(index)"
  379. >
  380. {{ tag.name }}
  381. </el-tag>
  382. </el-popover>
  383. </div>
  384. </div>
  385. </div>
  386. </template>
  387. <style scoped></style>