index.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. get() {
  102. const { value, multiple } = this.$props;
  103. return multiple ? "" : value;
  104. },
  105. set(val) {
  106. this.$emit("input", val);
  107. },
  108. },
  109. TableColumnTemp() {
  110. const { type } = this.$props;
  111. const documents = require(`./components/${type}`).default;
  112. return documents.filter((document) => document.key);
  113. },
  114. },
  115. watch: {
  116. "$props.value": {
  117. handler: function (newProp) {
  118. if (!newProp) this.lastSelectData = [];
  119. },
  120. immediate: true,
  121. },
  122. },
  123. methods: {
  124. //
  125. emitChange(prop) {
  126. const { type, source, multiple } = this.$props;
  127. this.$emit("change", multiple ? prop : prop[0], type, source);
  128. },
  129. // open dialog
  130. async open() {
  131. this.visible = true;
  132. await this.useReset();
  133. },
  134. // hide dialog
  135. async hide() {
  136. this.visible = false;
  137. },
  138. // fetch list
  139. async fetchList(prop, page) {
  140. try {
  141. this.loading = true;
  142. const { pageNum, pageSize } = page;
  143. const { code, rows, total } = await REFER(prop, {
  144. pageNum,
  145. pageSize,
  146. });
  147. if (code === 200) {
  148. this.data = rows;
  149. this.page.total = total;
  150. }
  151. } catch (err) {
  152. //
  153. console.error(err);
  154. } finally {
  155. this.loading = false;
  156. }
  157. },
  158. // reset
  159. async useReset() {
  160. const { type, source, queryParams } = this.$props;
  161. this.model = {
  162. type,
  163. ...this.model,
  164. search: "",
  165. ...queryParams(source),
  166. };
  167. await this.fetchList(this.model, this.page);
  168. },
  169. // query
  170. async useQuery() {
  171. await this.fetchList(this.model, this.page);
  172. },
  173. // cancel
  174. useCancel(prop) {
  175. if (prop.length) {
  176. const { multiple } = this.$props;
  177. this.useUpdate(multiple ? prop : prop[0]);
  178. }
  179. this.hide();
  180. },
  181. // confirm
  182. useConfirm(prop) {
  183. const { multiple } = this.$props;
  184. this.useUpdate(multiple ? prop : prop[0]);
  185. this.emitChange(this.selectData);
  186. this.lastSelectData = deepCopy(this.selectData);
  187. this.hide();
  188. },
  189. // delete
  190. useDelete(prop) {
  191. this.selectData.splice(prop, 1);
  192. this.useUpdate(this.selectData);
  193. this.emitChange(this.selectData);
  194. this.lastSelectData = deepCopy(this.selectData);
  195. },
  196. // update
  197. useUpdate(prop) {
  198. const { source, multiple, valueKey, dataMapping } = this.$props;
  199. // update data mapping
  200. if (multiple) {
  201. const vModel = prop.map((item) => item[valueKey]);
  202. this.$emit("input", vModel);
  203. } else {
  204. const vModel = prop[valueKey];
  205. this.$emit("input", vModel);
  206. for (let key in dataMapping) {
  207. source[key] = prop[dataMapping[key]];
  208. }
  209. this.$emit("update:source", source);
  210. }
  211. },
  212. // row click
  213. onceClick(prop) {
  214. const { multiple } = this.$props;
  215. // 单选
  216. if (!multiple) this.$refs.multipleTable.clearSelection();
  217. [prop].forEach((row) => this.$refs.multipleTable.toggleRowSelection(row));
  218. },
  219. // row double click
  220. doubleClick(prop) {
  221. const { multiple } = this.$props;
  222. if (!multiple) this.useConfirm([prop]);
  223. },
  224. // selection change
  225. selectionChange(prop) {
  226. if (prop && prop.length) {
  227. this.selectData = prop;
  228. }
  229. },
  230. handleClear() {
  231. if (!this.$props.multiple) {
  232. this.innerValue = "";
  233. }
  234. },
  235. },
  236. created() {
  237. },
  238. mounted() {},
  239. destroyed() {},
  240. };
  241. </script>
  242. <template>
  243. <div class="popover-select">
  244. <el-input
  245. v-model="innerValue"
  246. :size="size"
  247. :disabled="disabled"
  248. :readonly="readonly"
  249. :clearable="clearable"
  250. :placeholder="placeholder"
  251. @clear="handleClear"
  252. >
  253. <el-button
  254. :disabled="disabled"
  255. slot="append"
  256. icon="el-icon-search"
  257. @click="open"
  258. ></el-button>
  259. </el-input>
  260. <el-dialog
  261. :title="`${title}(${multiple ? '多选' : '单选'})`"
  262. :width="width"
  263. :visible.sync="visible"
  264. :close-on-click-modal="false"
  265. :close-on-press-escape="false"
  266. append-to-body
  267. >
  268. <el-form
  269. v-loading="loading"
  270. :size="size"
  271. :inline="true"
  272. :model="model"
  273. @submit.native.prevent
  274. >
  275. <el-form-item prop="search">
  276. <el-input
  277. v-model="model.search"
  278. @change="useQuery"
  279. @keydown.enter="useQuery"
  280. >
  281. </el-input>
  282. </el-form-item>
  283. <el-form-item>
  284. <el-button icon="el-icon-refresh" @click="useReset"></el-button>
  285. </el-form-item>
  286. <el-table
  287. ref="multipleTable"
  288. :data="data"
  289. :size="size"
  290. height="45vh"
  291. highlight-current-row
  292. style="width: 100%; margin-bottom: 20px"
  293. @row-click="onceClick"
  294. @row-dblclick="doubleClick"
  295. @selection-change="selectionChange"
  296. >
  297. <el-table-column
  298. v-if="multiple"
  299. width="55"
  300. type="selection"
  301. align="center"
  302. >
  303. </el-table-column>
  304. <el-table-column
  305. v-for="(column, index) in TableColumnTemp"
  306. :key="index"
  307. :prop="column.key"
  308. :label="column.title"
  309. :width="column.width"
  310. show-overflow-tooltip
  311. >
  312. <template slot-scope="scope">
  313. <dr-computed-input
  314. v-if="column.type === 'ComputedInput'"
  315. v-model="scope.row[column.key]"
  316. :source="scope.row"
  317. :computed="column.computed"
  318. :placeholder="column.placeholder"
  319. style="width: 100%"
  320. ></dr-computed-input>
  321. <span v-else> {{ scope.row[column.key] }}</span>
  322. </template>
  323. </el-table-column>
  324. </el-table>
  325. <pagination
  326. :total="page.total"
  327. :page.sync="page.pageNum"
  328. :limit.sync="page.pageSize"
  329. @pagination="useQuery"
  330. />
  331. </el-form>
  332. <div style="margin-top: 20px; text-align: right">
  333. <el-button :size="size" @click="useCancel(lastSelectData)">
  334. 取 消
  335. </el-button>
  336. <el-button :size="size" @click="useConfirm(selectData)">
  337. 确 定
  338. </el-button>
  339. </div>
  340. </el-dialog>
  341. <div
  342. style="
  343. position: absolute;
  344. left: 10px;
  345. top: 50%;
  346. transform: translateY(-50%);
  347. padding-right: 30px;
  348. overflow: hidden;
  349. "
  350. >
  351. <div v-if="multiple && lastSelectData.length">
  352. <el-popover
  353. :offset="-10"
  354. :width="width"
  355. :visible-arrow="false"
  356. title=""
  357. content=""
  358. trigger="click"
  359. placement="bottom-start"
  360. >
  361. <el-tag slot="reference" :size="size" style="margin-right: 10px">
  362. + {{ lastSelectData.length }}
  363. </el-tag>
  364. <el-tag
  365. v-for="(tag, index) in lastSelectData"
  366. :size="size"
  367. hit
  368. closable
  369. :style="{
  370. display: 'flex',
  371. justifyContent: 'space-between',
  372. alignItems: 'center',
  373. margin: lastSelectData.length - 1 === index ? '0' : '0 0 5px 0',
  374. }"
  375. @close="useDelete(index)"
  376. >
  377. {{ tag.name }}
  378. </el-tag>
  379. </el-popover>
  380. </div>
  381. </div>
  382. </div>
  383. </template>
  384. <style scoped></style>