index.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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,Number],
  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. // 处理新增字段无法映射
  122. let that = this;
  123. this.TableColumns.forEach(({item,attr}) =>{
  124. that.data = that.data.map(d =>{
  125. if(!attr.is && attr.formatter){
  126. d[item.key] = attr.formatter(d);
  127. }
  128. return d;
  129. })
  130. })
  131. this.page.total = total;
  132. }
  133. } catch (err) {
  134. // catch
  135. console.error(err);
  136. } finally {
  137. // finally
  138. this.loading = false;
  139. }
  140. },
  141. // reset
  142. async useReset() {
  143. this.data = [];
  144. this.page.pageNum = 1;
  145. this.page.pageSize = 10;
  146. this.model.search = null;
  147. await this.fetchList(this.model, this.page);
  148. },
  149. // query
  150. async useQuery() {
  151. await this.fetchList(this.model, this.page);
  152. },
  153. // auto
  154. async useAutocomplete(prop, cb) {
  155. if (prop) {
  156. this.page.pageSize = 1000000;
  157. this.model.search = prop;
  158. await this.fetchList(this.model, this.page);
  159. await cb(this.data);
  160. } else {
  161. cb([]);
  162. }
  163. },
  164. // select
  165. useSelect(prop) {
  166. this.selectData = prop;
  167. },
  168. // confirm
  169. useConfirm(prop) {
  170. this.hide();
  171. const {
  172. $props: { source, checkbox, valueKey, dataMapping },
  173. } = this;
  174. if (checkbox) {
  175. this.selectData = [...prop];
  176. this.$emit(
  177. "change",
  178. this.selectData.map((item) => {
  179. for (let key in dataMapping) {
  180. item[key] = item[dataMapping[key]];
  181. }
  182. return item;
  183. }),
  184. this.$props
  185. );
  186. } else {
  187. for (let key in dataMapping) {
  188. source[key] = prop[0][dataMapping[key]];
  189. }
  190. this.innerValue = prop[0][valueKey];
  191. this.$emit("update:source", source);
  192. this.$emit("change", prop[0], this.$props);
  193. }
  194. },
  195. },
  196. created() {},
  197. mounted() {},
  198. destroyed() {},
  199. };
  200. </script>
  201. <template>
  202. <div class="popover-select-v2">
  203. <el-autocomplete
  204. clearable
  205. v-bind="$attrs"
  206. v-model="innerValue"
  207. :value-key="valueKey"
  208. :fetch-suggestions="useAutocomplete"
  209. @select="useConfirm([$event])"
  210. style="width: 100%"
  211. >
  212. <i class="el-icon-search" slot="suffix" @click="open"> </i>
  213. <template slot-scope="{ item }">
  214. <p
  215. style="
  216. text-overflow: ellipsis;
  217. overflow: hidden;
  218. line-height: 15px;
  219. margin: 5px 0;
  220. "
  221. >
  222. {{ item.name }}
  223. </p>
  224. <p
  225. style="
  226. font-size: 12px;
  227. color: #b4b4b4;
  228. line-height: 15px;
  229. margin: 5px 0;
  230. "
  231. >
  232. {{ item.code }}
  233. </p>
  234. </template>
  235. </el-autocomplete>
  236. <el-dialog
  237. :width="width"
  238. :visible.sync="visible"
  239. :show-close="false"
  240. :close-on-click-modal="false"
  241. :close-on-press-escape="false"
  242. append-to-body
  243. >
  244. <div slot="title" style="display: flex; justify-content: space-between">
  245. <el-form
  246. :size="size"
  247. :inline="true"
  248. :model="model"
  249. @submit.native.prevent
  250. style="display: flex; flex-direction: column"
  251. >
  252. <div>
  253. <el-form-item prop="search">
  254. <el-input
  255. v-model="model.search"
  256. @change="useQuery"
  257. @keydown.enter="useQuery"
  258. >
  259. </el-input>
  260. </el-form-item>
  261. <el-form-item>
  262. <el-button icon="el-icon-refresh" @click="useReset"></el-button>
  263. </el-form-item>
  264. </div>
  265. </el-form>
  266. <div>
  267. <el-button
  268. type="primary"
  269. :size="$attrs.size"
  270. :loading="loading"
  271. @click="useConfirm(selectData)"
  272. >
  273. 确认
  274. </el-button>
  275. <el-button :size="$attrs.size" :loading="loading" @click="hide">
  276. 取消
  277. </el-button>
  278. </div>
  279. </div>
  280. <div
  281. v-loading="loading"
  282. style="height: 40vh; display: flex; margin-top: -30px"
  283. >
  284. <el-super-table
  285. v-model="data"
  286. :size="size"
  287. :page="page"
  288. :columns="TableColumns"
  289. :radio="!checkbox"
  290. :checkbox="checkbox"
  291. pagination
  292. highlight-current-row
  293. @pagination="useQuery"
  294. @row-select="useSelect"
  295. @row-dblclick="useConfirm([$event])"
  296. >
  297. </el-super-table>
  298. </div>
  299. </el-dialog>
  300. </div>
  301. </template>
  302. <style scoped>
  303. .popover-select-v2 .el-autocomplete {
  304. width: inherit;
  305. }
  306. .popover-select-v2 .el-autocomplete .el-icon-search {
  307. cursor: pointer;
  308. }
  309. ::v-deep .el-table--mini .el-table__cell {
  310. height: 50px;
  311. }
  312. </style>