index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. copy: {
  79. type: Boolean,
  80. default: () => {
  81. return false;
  82. },
  83. },
  84. // 需映射源数据
  85. source: Object,
  86. // 参照内外映射
  87. dataMapping: Object,
  88. },
  89. components: {},
  90. data() {
  91. return {
  92. width: "50%",
  93. page: { pageNum: 1, pageSize: 10, total: 0 },
  94. visible: false,
  95. loading: false,
  96. model: {
  97. search: "",
  98. isPage: true,
  99. },
  100. data: [],
  101. selectData: [],
  102. lastSelectData: [],
  103. };
  104. },
  105. computed: {
  106. innerValue: {
  107. get() {
  108. const { value, multiple } = this.$props;
  109. return multiple ? "" : value;
  110. },
  111. set(val) {
  112. const {
  113. $props: { source, dataMapping, multiple },
  114. } = this;
  115. if (!multiple) {
  116. for (let key in dataMapping) {
  117. if (dataMapping[key] === "id" || dataMapping[key] === "code") {
  118. if (source[key] && source[key] !== "") {
  119. source[key] = undefined;
  120. }
  121. }
  122. }
  123. }
  124. this.$emit("input", val);
  125. },
  126. },
  127. TableColumnTemp() {
  128. const { type } = this.$props;
  129. const documents = require(`./components/${type}`).default;
  130. return documents.filter((document) => document.item.key);
  131. },
  132. },
  133. watch: {
  134. "$props.value": {
  135. handler: function (newProp) {
  136. if (!newProp) this.lastSelectData = [];
  137. },
  138. immediate: true,
  139. },
  140. innerValue: {
  141. handler: function (newValue) {
  142. const {
  143. $props: { source, dataMapping, multiple },
  144. } = this;
  145. if (!newValue) {
  146. for (let key in dataMapping) {
  147. source[key] = undefined;
  148. }
  149. this.$emit("update:source", source);
  150. }
  151. },
  152. },
  153. },
  154. methods: {
  155. //
  156. emitChange(prop) {
  157. const { type, source, multiple } = this.$props;
  158. this.$emit("change", multiple ? prop : prop[0], type, source);
  159. },
  160. // open dialog
  161. async open() {
  162. if (!this.disabled) {
  163. this.visible = true;
  164. await this.useReset();
  165. }
  166. },
  167. // hide dialog
  168. async hide() {
  169. this.visible = false;
  170. },
  171. // fetch list
  172. async fetchList(prop, page) {
  173. try {
  174. this.loading = true;
  175. const { pageNum, pageSize } = page;
  176. const { code, rows, total } = await REFER(prop, {
  177. pageNum,
  178. pageSize,
  179. });
  180. if (code === 200) {
  181. this.data = rows;
  182. // 处理新增字段无法映射
  183. let that = this;
  184. this.TableColumnTemp.forEach(({ item, attr }) => {
  185. that.data = that.data.map((d) => {
  186. if (!attr.is && attr.formatter) {
  187. d[item.key] = attr.formatter(d);
  188. }
  189. return d;
  190. });
  191. });
  192. this.page.total = total;
  193. }
  194. } catch (err) {
  195. //
  196. console.error(err);
  197. } finally {
  198. this.loading = false;
  199. }
  200. },
  201. // reset
  202. async useReset() {
  203. const { type, source, queryParams } = this.$props;
  204. this.model = {
  205. type,
  206. ...this.model,
  207. search: "",
  208. ...queryParams(source),
  209. };
  210. console.log(source, "source");
  211. await this.fetchList(this.model, this.page);
  212. },
  213. // query
  214. async useQuery() {
  215. await this.fetchList(this.model, this.page);
  216. },
  217. // cancel
  218. useCancel(prop) {
  219. if (prop.length) {
  220. const { multiple } = this.$props;
  221. this.useUpdate(multiple ? prop : prop[0]);
  222. }
  223. this.hide();
  224. },
  225. // confirm
  226. useConfirm(prop) {
  227. const { multiple } = this.$props;
  228. this.selectData = [...prop];
  229. console.log(this.selectData, "this.selectData");
  230. this.useUpdate(multiple ? prop : prop[0]);
  231. this.emitChange(this.selectData);
  232. this.lastSelectData = deepCopy(this.selectData);
  233. this.hide();
  234. },
  235. // delete
  236. useDelete(prop) {
  237. this.selectData.splice(prop, 1);
  238. this.useUpdate(this.selectData);
  239. this.emitChange(this.selectData);
  240. this.lastSelectData = deepCopy(this.selectData);
  241. },
  242. // update
  243. useUpdate(prop) {
  244. const { source, multiple, valueKey, dataMapping } = this.$props;
  245. // update data mapping
  246. if (multiple) {
  247. const vModel = prop.map((item) => item[valueKey]);
  248. this.$emit("input", vModel);
  249. } else {
  250. const vModel = prop[valueKey];
  251. this.$emit("input", vModel);
  252. for (let key in dataMapping) {
  253. source[key] = prop[dataMapping[key]];
  254. }
  255. this.$emit("update:source", source);
  256. }
  257. },
  258. // row click
  259. onceClick(prop) {
  260. const { multiple } = this.$props;
  261. // 单选
  262. if (!multiple) this.$refs.multipleTable.clearSelection();
  263. [prop].forEach((row) => this.$refs.multipleTable.toggleRowSelection(row));
  264. },
  265. // row double click
  266. doubleClick(prop) {
  267. const { multiple } = this.$props;
  268. if (!multiple) this.useConfirm([prop]);
  269. },
  270. // selection change
  271. selectionChange(prop) {
  272. if (prop && prop.length) {
  273. this.selectData = prop;
  274. }
  275. },
  276. handleClear() {
  277. const { source, multiple, dataMapping } = this.$props;
  278. if (!multiple) {
  279. this.innerValue = "";
  280. // for (let key in dataMapping) {
  281. // source[key] = "";
  282. // }
  283. // this.$emit("update:source", source);
  284. }
  285. },
  286. async useAutocomplete(prop, cb) {
  287. const { type, source, queryParams } = this.$props;
  288. if (prop) {
  289. this.page.pageSize = 10;
  290. this.model = {
  291. ...this.model,
  292. search: prop,
  293. type: type,
  294. ...queryParams(source),
  295. };
  296. //
  297. await this.fetchList(this.model, this.page);
  298. await cb(this.data);
  299. } else {
  300. cb([]);
  301. }
  302. },
  303. async handleChange() {
  304. const { type, source } = this.$props;
  305. this.$emit("copyChange", this.innerValue.split(/,|,|\s+/));
  306. },
  307. useBlur() {
  308. const {
  309. $props: { source, dataMapping, multiple },
  310. } = this;
  311. if (!multiple) {
  312. for (let key in dataMapping) {
  313. if (dataMapping[key] === "id" || dataMapping[key] === "code") {
  314. if (!source[key] || source[key] === "") {
  315. this.innerValue = "";
  316. }
  317. }
  318. }
  319. }
  320. },
  321. },
  322. created() {},
  323. mounted() {},
  324. destroyed() {},
  325. };
  326. </script>
  327. <template>
  328. <div class="popover-select">
  329. <el-input
  330. v-if="copy"
  331. v-model="innerValue"
  332. :size="size"
  333. :disabled="disabled"
  334. :readonly="readonly"
  335. :clearable="clearable"
  336. :placeholder="placeholder"
  337. @clear="handleClear"
  338. @change="handleChange"
  339. @keyup.enter.native="handleChange"
  340. >
  341. <i :size="size" class="el-icon-search" slot="suffix" @click="open"> </i>
  342. </el-input>
  343. <el-autocomplete
  344. v-else
  345. clearable
  346. v-bind="$attrs"
  347. v-model="innerValue"
  348. :size="size"
  349. :disabled="disabled"
  350. :value-key="valueKey"
  351. :fetch-suggestions="useAutocomplete"
  352. @blur="useBlur"
  353. @select="useConfirm([$event])"
  354. style="width: 100%"
  355. >
  356. <i :size="size" class="el-icon-search" slot="suffix" @click="open"> </i>
  357. <template slot-scope="{ item }">
  358. <p
  359. style="
  360. text-overflow: ellipsis;
  361. overflow: hidden;
  362. line-height: 15px;
  363. margin: 5px 0;
  364. "
  365. >
  366. {{ item.name }}
  367. </p>
  368. <p
  369. style="
  370. font-size: 12px;
  371. color: #b4b4b4;
  372. line-height: 15px;
  373. margin: 5px 0;
  374. "
  375. >
  376. {{ item.code }}
  377. </p>
  378. </template>
  379. </el-autocomplete>
  380. <el-dialog
  381. :title="`${title}(${multiple ? '多选' : '单选'})`"
  382. :width="width"
  383. :visible.sync="visible"
  384. :close-on-click-modal="false"
  385. :close-on-press-escape="false"
  386. append-to-body
  387. >
  388. <el-form
  389. v-loading="loading"
  390. :size="size"
  391. :inline="true"
  392. :model="model"
  393. @submit.native.prevent
  394. >
  395. <el-form-item prop="search">
  396. <el-input
  397. size="mini"
  398. v-model="model.search"
  399. @change="useQuery"
  400. @keydown.enter="useQuery"
  401. >
  402. </el-input>
  403. </el-form-item>
  404. <el-form-item>
  405. <el-button @click="useQuery" size="mini">搜 索</el-button>
  406. <el-button
  407. icon="el-icon-refresh"
  408. @click="useReset"
  409. size="mini"
  410. ></el-button>
  411. </el-form-item>
  412. <el-table
  413. ref="multipleTable"
  414. :data="data"
  415. :size="size"
  416. height="45vh"
  417. highlight-current-row
  418. style="width: 100%; margin-bottom: 20px"
  419. @row-click="onceClick"
  420. @row-dblclick="doubleClick"
  421. @selection-change="selectionChange"
  422. >
  423. <el-table-column
  424. v-if="multiple"
  425. width="55"
  426. type="selection"
  427. align="center"
  428. >
  429. </el-table-column>
  430. <el-table-column
  431. v-for="({ item, attr }, index) in TableColumnTemp"
  432. :key="index"
  433. :prop="item.key"
  434. :label="item.title"
  435. :width="item.width"
  436. show-overflow-tooltip
  437. >
  438. <template slot-scope="scope">
  439. <dr-computed-input
  440. v-if="attr.type === 'ComputedInput'"
  441. v-model="scope.row[item.key]"
  442. :source="scope.row"
  443. :formatter="attr.formatter"
  444. :placeholder="attr.placeholder"
  445. style="width: 100%"
  446. ></dr-computed-input>
  447. <span v-else> {{ scope.row[item.key] }}</span>
  448. </template>
  449. </el-table-column>
  450. </el-table>
  451. <pagination
  452. :total="page.total"
  453. :page.sync="page.pageNum"
  454. :limit.sync="page.pageSize"
  455. @pagination="useQuery"
  456. />
  457. </el-form>
  458. <div style="margin-top: 20px; text-align: right">
  459. <el-button :size="size" @click="useConfirm(selectData)">
  460. 确 定
  461. </el-button>
  462. <el-button :size="size" @click="useCancel(lastSelectData)">
  463. 取 消
  464. </el-button>
  465. </div>
  466. </el-dialog>
  467. <div
  468. style="
  469. position: absolute;
  470. left: 10px;
  471. top: 50%;
  472. transform: translateY(-50%);
  473. padding-right: 30px;
  474. overflow: hidden;
  475. "
  476. >
  477. <div v-if="multiple && lastSelectData.length">
  478. <el-popover
  479. :offset="-10"
  480. :width="width"
  481. :visible-arrow="false"
  482. title=""
  483. content=""
  484. trigger="click"
  485. placement="bottom-start"
  486. >
  487. <el-tag slot="reference" :size="size" style="margin-right: 10px">
  488. + {{ lastSelectData.length }}
  489. </el-tag>
  490. <el-tag
  491. v-for="(tag, index) in lastSelectData"
  492. :size="size"
  493. hit
  494. closable
  495. :style="{
  496. display: 'flex',
  497. justifyContent: 'space-between',
  498. alignItems: 'center',
  499. margin: lastSelectData.length - 1 === index ? '0' : '0 0 5px 0',
  500. }"
  501. @close="useDelete(index)"
  502. >
  503. {{ tag.name }}
  504. </el-tag>
  505. </el-popover>
  506. </div>
  507. </div>
  508. </div>
  509. </template>
  510. <style scoped>
  511. .popover-select .el-autocomplete {
  512. width: inherit;
  513. }
  514. .popover-select .el-autocomplete .el-icon-search {
  515. cursor: pointer;
  516. }
  517. ::v-deep .el-table--mini .el-table__cell {
  518. height: 50px;
  519. }
  520. </style>