index.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <script>
  2. import { REFER } from "../popover-select/api/index";
  3. export default {
  4. name: "PopoverSelectV2",
  5. props: {
  6. // v-model
  7. value: {
  8. type: [Array, String],
  9. require: true,
  10. },
  11. // 参照类型 ,对应后端
  12. referName: {
  13. type: String,
  14. require: true,
  15. },
  16. // 作为 value 唯一标识的键名,绑定值
  17. valueKey: {
  18. type: String,
  19. dafault: () => {
  20. return "code";
  21. },
  22. },
  23. // 默认查询参数
  24. queryParams: {
  25. type: Function,
  26. default: () => {},
  27. },
  28. // 需映射源数据
  29. source: {
  30. type: Object,
  31. default: () => ({}),
  32. },
  33. // 参照内外映射
  34. dataMapping: {
  35. type: Object,
  36. default: () => ({}),
  37. },
  38. // 多选
  39. checkbox: {
  40. type: Boolean,
  41. default: false,
  42. },
  43. },
  44. components: {
  45. ElSuperTable: () => import("@/components/super-table/index.vue"),
  46. },
  47. data() {
  48. return {
  49. size: "mini",
  50. width: "50%",
  51. page: { pageNum: 1, pageSize: 10, total: 0 },
  52. visible: false,
  53. loading: false,
  54. model: {
  55. search: "",
  56. isPage: true,
  57. },
  58. data: [],
  59. selectData: [],
  60. };
  61. },
  62. computed: {
  63. innerValue: {
  64. get() {
  65. return this.value;
  66. },
  67. set(value) {
  68. this.$emit("input", value);
  69. },
  70. },
  71. TableColumns() {
  72. const { referName } = this.$props;
  73. return require(`../popover-select/components/${referName}`).default;
  74. },
  75. },
  76. watch: {
  77. innerValue: {
  78. handler: function (newValue) {
  79. if (!newValue) {
  80. const {
  81. $props: { source, dataMapping },
  82. } = this;
  83. for (let key in dataMapping) {
  84. source[key] = undefined;
  85. }
  86. this.$emit("update:source", source);
  87. }
  88. },
  89. },
  90. },
  91. methods: {
  92. // open dialog
  93. async open() {
  94. this.visible = true;
  95. await this.useReset();
  96. },
  97. // hide dialog
  98. async hide() {
  99. this.visible = false;
  100. },
  101. // fetch list
  102. async fetchList(prop, page) {
  103. try {
  104. // try
  105. this.loading = true;
  106. const { pageNum, pageSize } = page;
  107. const { referName: type, source, queryParams } = this.$props;
  108. const { code, rows, total } = await REFER(
  109. {
  110. ...prop,
  111. ...queryParams(source),
  112. type: type,
  113. },
  114. {
  115. pageNum,
  116. pageSize,
  117. }
  118. );
  119. if (code === 200) {
  120. this.data = rows;
  121. this.page.total = total;
  122. }
  123. } catch (err) {
  124. // catch
  125. console.error(err);
  126. } finally {
  127. // finally
  128. this.loading = false;
  129. }
  130. },
  131. // reset
  132. async useReset() {
  133. this.data = [];
  134. this.page.pageNum = 1;
  135. this.page.pageSize = 10;
  136. this.model.search = null;
  137. await this.fetchList(this.model, this.page);
  138. },
  139. // query
  140. async useQuery() {
  141. await this.fetchList(this.model, this.page);
  142. },
  143. // auto
  144. async useAutocomplete(prop, cb) {
  145. if (prop) {
  146. this.page.pageSize = 25;
  147. this.model.search = prop;
  148. await this.fetchList(this.model, this.page);
  149. await cb(this.data);
  150. } else {
  151. cb([]);
  152. }
  153. },
  154. // select
  155. useSelect(prop) {
  156. this.selectData = prop;
  157. },
  158. // confirm
  159. useConfirm(prop) {
  160. this.hide();
  161. const {
  162. $props: { source, checkbox, valueKey, dataMapping },
  163. } = this;
  164. if (checkbox) {
  165. this.$emit(
  166. "change",
  167. this.selectData.map((item) => {
  168. for (let key in dataMapping) {
  169. item[key] = item[dataMapping[key]];
  170. }
  171. return item;
  172. }),
  173. this.$props
  174. );
  175. } else {
  176. for (let key in dataMapping) {
  177. source[key] = prop[0][dataMapping[key]];
  178. }
  179. this.innerValue = prop[0][valueKey];
  180. this.$emit("update:source", source);
  181. this.$emit("change", prop[0], this.$props);
  182. }
  183. },
  184. },
  185. created() {},
  186. mounted() {},
  187. destroyed() {},
  188. };
  189. </script>
  190. <template>
  191. <div class="popover-select-v2">
  192. <el-autocomplete
  193. v-bind="$attrs"
  194. v-model="innerValue"
  195. :value-key="valueKey"
  196. :fetch-suggestions="useAutocomplete"
  197. @select="useConfirm([$event])"
  198. style="width: 100%"
  199. >
  200. <i class="el-icon-search" slot="suffix" @click="open"> </i>
  201. <template slot-scope="{ item }">
  202. <p
  203. style="
  204. text-overflow: ellipsis;
  205. overflow: hidden;
  206. line-height: 15px;
  207. margin: 5px 0;
  208. "
  209. >
  210. {{ item.name }}
  211. </p>
  212. <p
  213. style="
  214. font-size: 12px;
  215. color: #b4b4b4;
  216. line-height: 15px;
  217. margin: 5px 0;
  218. "
  219. >
  220. {{ item.code }}
  221. </p>
  222. </template>
  223. </el-autocomplete>
  224. <el-dialog
  225. :width="width"
  226. :visible.sync="visible"
  227. :show-close="false"
  228. :close-on-click-modal="false"
  229. :close-on-press-escape="false"
  230. append-to-body
  231. >
  232. <div slot="title" style="display: flex; justify-content: space-between">
  233. <el-form
  234. :size="size"
  235. :inline="true"
  236. :model="model"
  237. @submit.native.prevent
  238. style="display: flex; flex-direction: column"
  239. >
  240. <div>
  241. <el-form-item prop="search">
  242. <el-input
  243. v-model="model.search"
  244. @change="useQuery"
  245. @keydown.enter="useQuery"
  246. >
  247. </el-input>
  248. </el-form-item>
  249. <el-form-item>
  250. <el-button icon="el-icon-refresh" @click="useReset"></el-button>
  251. </el-form-item>
  252. </div>
  253. </el-form>
  254. <div>
  255. <el-button
  256. type="primary"
  257. :size="$attrs.size"
  258. :loading="loading"
  259. @click="useConfirm(selectData)"
  260. >
  261. 确认
  262. </el-button>
  263. <el-button :size="$attrs.size" :loading="loading" @click="hide">
  264. 取消
  265. </el-button>
  266. </div>
  267. </div>
  268. <div
  269. v-loading="loading"
  270. style="height: 40vh; display: flex; margin-top: -30px"
  271. >
  272. <el-super-table
  273. v-model="data"
  274. :size="size"
  275. :page="page"
  276. :columns="TableColumns"
  277. :radio="!checkbox"
  278. :checkbox="checkbox"
  279. pagination
  280. highlight-current-row
  281. @pagination="useQuery"
  282. @row-select="useSelect"
  283. @row-dblclick="useConfirm([$event])"
  284. >
  285. </el-super-table>
  286. </div>
  287. </el-dialog>
  288. </div>
  289. </template>
  290. <style scoped>
  291. .popover-select-v2 .el-autocomplete {
  292. width: inherit;
  293. }
  294. .popover-select-v2 .el-autocomplete .el-icon-search {
  295. cursor: pointer;
  296. }
  297. ::v-deep .el-table--mini .el-table__cell {
  298. height: 50px;
  299. }
  300. </style>