index.vue 13 KB

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