index.vue 12 KB

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