index.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <script>
  2. import { refer } from "./api/index";
  3. export default {
  4. name: "PopoverSelect",
  5. props: {
  6. // 参照类型 ,对应后端
  7. type: {
  8. type: String,
  9. require: true,
  10. },
  11. // v-model
  12. value: {
  13. type: [Array, String],
  14. require: true,
  15. },
  16. // 参照弹窗标题
  17. title: {
  18. type: String,
  19. dafault: () => {
  20. return "TITEL";
  21. },
  22. },
  23. // 作为 value 唯一标识的键名,绑定值
  24. valueKey: {
  25. type: String,
  26. dafault: () => {
  27. return "code";
  28. },
  29. },
  30. // 默认查询参数
  31. queryParams: {
  32. type: Object,
  33. default: () => ({}),
  34. },
  35. // 组件大小
  36. size: {
  37. type: String,
  38. dafault: () => {
  39. return "mini";
  40. },
  41. },
  42. // 提示
  43. placeholder: {
  44. type: String,
  45. dafault: () => {
  46. return "";
  47. },
  48. },
  49. // 是否可读
  50. readonly: {
  51. type: Boolean,
  52. dafault: () => {
  53. return false;
  54. },
  55. },
  56. // 是否禁止
  57. disabled: {
  58. type: Boolean,
  59. dafault: () => {
  60. return false;
  61. },
  62. },
  63. // 是否清除
  64. clearable: {
  65. type: Boolean,
  66. dafault: () => {
  67. return false;
  68. },
  69. },
  70. // 是否多选
  71. multiple: {
  72. type: Boolean,
  73. dafault: () => {
  74. return false;
  75. },
  76. },
  77. // 需映射源数据
  78. source: Object,
  79. // 参照内外映射
  80. dataMapping: Object,
  81. },
  82. components: {},
  83. data() {
  84. return {
  85. width: "50%",
  86. page: { pageNum: 1, pageSize: 25, total: 0 },
  87. layout: "total, prev, pager, next, sizes, jumper",
  88. pageSizes: [25, 50, 100],
  89. visible: false,
  90. loading: false,
  91. model: {
  92. search: "",
  93. isPage: true,
  94. },
  95. data: [],
  96. selectData: [],
  97. };
  98. },
  99. computed: {
  100. innerValue() {
  101. const { value, multiple } = this.$props;
  102. return multiple ? "" : value;
  103. },
  104. TableColumnTemp() {
  105. const { type } = this.$props;
  106. const documents = require(`./components/${type}`).default;
  107. return documents.filter((document) => document.key);
  108. },
  109. },
  110. watch: {
  111. "$props.value": {
  112. handler: function (newProp) {
  113. if (!newProp) this.selectData = [];
  114. },
  115. immediate: true,
  116. },
  117. },
  118. methods: {
  119. // open dialog
  120. async open() {
  121. this.visible = true;
  122. await this.resetList();
  123. },
  124. // hide dialog
  125. async hide() {
  126. this.visible = false;
  127. },
  128. // fetch list
  129. async fetchList(prop, page) {
  130. try {
  131. this.loading = true;
  132. const { pageNum, pageSize } = page;
  133. const { code, msg, rows, total } = await refer(prop, {
  134. pageNum,
  135. pageSize,
  136. });
  137. if (code === 200) {
  138. this.data = rows;
  139. this.page.total = total;
  140. }
  141. } catch (err) {
  142. //
  143. } finally {
  144. this.loading = false;
  145. }
  146. },
  147. // reset list
  148. async resetList() {
  149. const { type, queryParams } = this.$props;
  150. this.model = {
  151. ...queryParams,
  152. ...this.model,
  153. search: "",
  154. type,
  155. };
  156. await this.fetchList(this.model, this.page);
  157. },
  158. // query list
  159. async queryList() {
  160. await this.fetchList(this.model, this.page);
  161. },
  162. // row click
  163. rowClick(prop) {
  164. const { multiple } = this.$props;
  165. // 单选
  166. if (!multiple) this.$refs.multipleTable.clearSelection();
  167. [prop].forEach((row) => this.$refs.multipleTable.toggleRowSelection(row));
  168. },
  169. // row double click
  170. async rowDblclick(prop) {
  171. const { multiple } = this.$props;
  172. if (!multiple) await this.confirm([prop]);
  173. },
  174. // selection change
  175. selectionChange(prop) {
  176. this.selectData = prop;
  177. },
  178. // page size change
  179. async pageSizeChange(prop) {
  180. this.page.pageSize = prop;
  181. await this.queryList();
  182. },
  183. // page number change
  184. async pageNumberChange(prop) {
  185. this.page.pageNum = prop;
  186. await this.queryList();
  187. },
  188. // confirm
  189. async confirm(prop) {
  190. const { multiple } = this.$props;
  191. await this.hide();
  192. await this.update(multiple ? prop : prop[0]);
  193. },
  194. // delete tag
  195. deleteTag(prop) {
  196. this.selectData.splice(prop, 1);
  197. this.update(this.selectData);
  198. },
  199. // update
  200. update(prop) {
  201. const { source, multiple, valueKey, dataMapping, type } = this.$props;
  202. // update v-model
  203. const vModel = multiple
  204. ? prop.map((item) => item[valueKey])
  205. : prop[valueKey];
  206. this.$emit("input", vModel);
  207. // update data mapping
  208. for (let key in dataMapping) {
  209. source[key] = prop[dataMapping[key]];
  210. }
  211. this.$emit("update:source", source);
  212. // emit change
  213. this.$emit("change", prop, source, type);
  214. },
  215. },
  216. created() {},
  217. mounted() {},
  218. destroyed() {},
  219. };
  220. </script>
  221. <template>
  222. <div>
  223. <el-input
  224. v-model="innerValue"
  225. :size="size"
  226. :disabled="disabled"
  227. :readonly="readonly"
  228. :clearable="clearable"
  229. :placeholder="placeholder"
  230. >
  231. <el-button
  232. :disabled="disabled"
  233. slot="append"
  234. icon="el-icon-search"
  235. @click="open"
  236. ></el-button>
  237. </el-input>
  238. <el-dialog
  239. :title="`${title}(${multiple ? '多选' : '单选'})`"
  240. :width="width"
  241. :visible.sync="visible"
  242. :close-on-click-modal="false"
  243. :close-on-press-escape="false"
  244. append-to-body
  245. @close="hide"
  246. >
  247. <el-form
  248. v-loading="loading"
  249. :size="size"
  250. :inline="true"
  251. :model="model"
  252. @submit.native.prevent
  253. >
  254. <el-form-item prop="search">
  255. <el-input
  256. v-model="model.search"
  257. @change="queryList"
  258. @keydown.enter="queryList"
  259. >
  260. </el-input>
  261. </el-form-item>
  262. <el-form-item>
  263. <el-button icon="el-icon-refresh" @click="resetList"></el-button>
  264. </el-form-item>
  265. <el-table
  266. ref="multipleTable"
  267. :data="data"
  268. :size="size"
  269. height="45vh"
  270. highlight-current-row
  271. style="width: 100%; margin-bottom: 20px"
  272. @row-click="rowClick"
  273. @row-dblclick="rowDblclick"
  274. @selection-change="selectionChange"
  275. >
  276. <el-table-column width="55" type="selection" align="center">
  277. </el-table-column>
  278. <el-table-column
  279. v-for="(column, index) in TableColumnTemp"
  280. :key="index"
  281. :prop="column.key"
  282. :label="column.title"
  283. :width="column.width"
  284. show-overflow-tooltip
  285. >
  286. </el-table-column>
  287. </el-table>
  288. <el-pagination
  289. :layout="layout"
  290. :total="page.total"
  291. :page-sizes="pageSizes"
  292. :small="size === 'mini'"
  293. :page-size="page.pageSize"
  294. :current-page="page.pageNum"
  295. background
  296. @size-change="pageSizeChange"
  297. @current-change="pageNumberChange"
  298. >
  299. </el-pagination>
  300. </el-form>
  301. <div style="margin-top: 20px; text-align: right">
  302. <el-button :size="size" @click="hide"> 取 消 </el-button>
  303. <el-button :size="size" @click="confirm(selectData)"> 确 定 </el-button>
  304. </div>
  305. </el-dialog>
  306. <div
  307. style="
  308. position: absolute;
  309. left: 10px;
  310. top: 50%;
  311. transform: translateY(-50%);
  312. padding-right: 30px;
  313. overflow: hidden;
  314. "
  315. >
  316. <div v-if="multiple && selectData.length">
  317. <el-popover
  318. :offset="-10"
  319. :width="width"
  320. :visible-arrow="false"
  321. title=""
  322. content=""
  323. trigger="click"
  324. placement="bottom-start"
  325. >
  326. <el-tag slot="reference" :size="size" style="margin-right: 10px">
  327. + {{ selectData.length }}
  328. </el-tag>
  329. <el-tag
  330. v-for="(tag, index) in selectData"
  331. :size="size"
  332. hit
  333. closable
  334. :style="{
  335. display: 'flex',
  336. justifyContent: 'space-between',
  337. alignItems: 'center',
  338. margin: selectData.length - 1 === index ? '0' : '0 0 5px 0',
  339. }"
  340. @close="deleteTag(index)"
  341. >
  342. {{ tag.name }}
  343. </el-tag>
  344. </el-popover>
  345. </div>
  346. </div>
  347. </div>
  348. </template>
  349. <style scoped></style>