index.vue 8.5 KB

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