index.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. title: {
  45. type: String,
  46. dafault: () => {
  47. return "TITEL";
  48. },
  49. },
  50. },
  51. components: {
  52. ElSuperTable: () => import("@/components/super-table/index.vue"),
  53. },
  54. data() {
  55. return {
  56. size: "mini",
  57. width: "50%",
  58. page: { pageNum: 1, pageSize: 10, total: 0 },
  59. visible: false,
  60. loading: false,
  61. model: {
  62. search: "",
  63. isPage: true,
  64. },
  65. data: [],
  66. selectData: [],
  67. };
  68. },
  69. computed: {
  70. innerValue: {
  71. get() {
  72. return this.value;
  73. },
  74. set(value) {
  75. this.$emit("input", value);
  76. },
  77. },
  78. TableColumns() {
  79. const { referName } = this.$props;
  80. return require(`../popover-select/components/${referName}`).default;
  81. },
  82. },
  83. watch: {
  84. innerValue: {
  85. handler: function (newValue) {
  86. if (!newValue) {
  87. const {
  88. $props: { source, dataMapping },
  89. } = this;
  90. for (let key in dataMapping) {
  91. source[key] = undefined;
  92. }
  93. this.$emit("update:source", source);
  94. }
  95. },
  96. },
  97. },
  98. methods: {
  99. // open dialog
  100. async open() {
  101. this.visible = true;
  102. await this.useReset();
  103. },
  104. // hide dialog
  105. async hide() {
  106. this.visible = false;
  107. },
  108. // fetch list
  109. async fetchList(prop, page) {
  110. try {
  111. // try
  112. this.loading = true;
  113. const { pageNum, pageSize } = page;
  114. const { referName: type, source, queryParams } = this.$props;
  115. const { code, rows, total } = await REFER(
  116. {
  117. ...prop,
  118. ...queryParams(source),
  119. type: type,
  120. },
  121. {
  122. pageNum,
  123. pageSize,
  124. }
  125. );
  126. if (code === 200) {
  127. this.data = rows;
  128. // 处理新增字段无法映射
  129. let that = this;
  130. this.TableColumns.forEach(({item,attr}) =>{
  131. that.data = that.data.map(d =>{
  132. if(!attr.is && attr.formatter){
  133. d[item.key] = attr.formatter(d);
  134. }
  135. return d;
  136. })
  137. })
  138. this.page.total = total;
  139. }
  140. } catch (err) {
  141. // catch
  142. console.error(err);
  143. } finally {
  144. // finally
  145. this.loading = false;
  146. }
  147. },
  148. // reset
  149. async useReset() {
  150. this.data = [];
  151. this.page.pageNum = 1;
  152. this.page.pageSize = 10;
  153. this.model.search = null;
  154. await this.fetchList(this.model, this.page);
  155. },
  156. // query
  157. async useQuery() {
  158. await this.fetchList(this.model, this.page);
  159. },
  160. // auto
  161. async useAutocomplete(prop, cb) {
  162. if (prop) {
  163. this.page.pageSize = 1000000;
  164. this.model.search = prop;
  165. await this.fetchList(this.model, this.page);
  166. await cb(this.data);
  167. } else {
  168. cb([]);
  169. }
  170. },
  171. // select
  172. useSelect(prop) {
  173. this.selectData = prop;
  174. },
  175. // confirm
  176. useConfirm(prop) {
  177. this.hide();
  178. const {
  179. $props: { source, checkbox, valueKey, dataMapping },
  180. } = this;
  181. if (checkbox) {
  182. this.selectData = [...prop];
  183. this.$emit(
  184. "change",
  185. this.selectData.map((item) => {
  186. for (let key in dataMapping) {
  187. item[key] = item[dataMapping[key]];
  188. }
  189. return item;
  190. }),
  191. this.$props
  192. );
  193. } else {
  194. for (let key in dataMapping) {
  195. source[key] = prop[0][dataMapping[key]];
  196. }
  197. this.innerValue = prop[0][valueKey];
  198. this.$emit("update:source", source);
  199. this.$emit("change", prop[0], this.$props);
  200. }
  201. },
  202. },
  203. created() {},
  204. mounted() {},
  205. destroyed() {},
  206. };
  207. </script>
  208. <template>
  209. <div class="popover-select-v2">
  210. <el-autocomplete
  211. clearable
  212. v-bind="$attrs"
  213. v-model="innerValue"
  214. :value-key="valueKey"
  215. :fetch-suggestions="useAutocomplete"
  216. @select="useConfirm([$event])"
  217. style="width: 100%"
  218. >
  219. <i class="el-icon-search" slot="suffix" @click="open"> </i>
  220. <template slot-scope="{ item }">
  221. <p
  222. style="
  223. text-overflow: ellipsis;
  224. overflow: hidden;
  225. line-height: 15px;
  226. margin: 5px 0;
  227. "
  228. >
  229. {{ item.name }}
  230. </p>
  231. <p
  232. style="
  233. font-size: 12px;
  234. color: #b4b4b4;
  235. line-height: 15px;
  236. margin: 5px 0;
  237. "
  238. >
  239. {{ item.code }}
  240. </p>
  241. </template>
  242. </el-autocomplete>
  243. <el-dialog
  244. :width="width"
  245. :visible.sync="visible"
  246. :show-close="false"
  247. :close-on-click-modal="false"
  248. :close-on-press-escape="false"
  249. append-to-body
  250. >
  251. <div slot="title" style="display: flex; justify-content: space-between">
  252. <el-form
  253. :size="size"
  254. :inline="true"
  255. :model="model"
  256. @submit.native.prevent
  257. style="display: flex; flex-direction: column"
  258. >
  259. <div>
  260. <el-form-item prop="search">
  261. <el-input
  262. v-model="model.search"
  263. @change="useQuery"
  264. @keydown.enter="useQuery"
  265. >
  266. </el-input>
  267. </el-form-item>
  268. <el-form-item>
  269. <el-button icon="el-icon-search" @click="useQuery">搜索</el-button>
  270. <el-button icon="el-icon-refresh" @click="useReset">重置</el-button>
  271. </el-form-item>
  272. </div>
  273. </el-form>
  274. <div>
  275. <el-button
  276. type="primary"
  277. :size="$attrs.size"
  278. :loading="loading"
  279. @click="useConfirm(selectData)"
  280. >
  281. 确认
  282. </el-button>
  283. <el-button :size="$attrs.size" :loading="loading" @click="hide">
  284. 取消
  285. </el-button>
  286. </div>
  287. </div>
  288. <div
  289. v-loading="loading"
  290. style="height: 40vh; display: flex; margin-top: -30px"
  291. >
  292. <el-super-table
  293. v-model="data"
  294. :size="size"
  295. :page="page"
  296. :columns="TableColumns"
  297. :radio="!checkbox"
  298. :checkbox="checkbox"
  299. pagination
  300. highlight-current-row
  301. @pagination="useQuery"
  302. @row-select="useSelect"
  303. @row-dblclick="useConfirm([$event])"
  304. >
  305. </el-super-table>
  306. </div>
  307. </el-dialog>
  308. </div>
  309. </template>
  310. <style scoped>
  311. .popover-select-v2 .el-autocomplete {
  312. width: inherit;
  313. }
  314. .popover-select-v2 .el-autocomplete .el-icon-search {
  315. cursor: pointer;
  316. }
  317. ::v-deep .el-table--mini .el-table__cell {
  318. height: 50px;
  319. }
  320. </style>