123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <script>
- import { REFER } from "../popover-select/api/index";
- export default {
- name: "PopoverSelectV2",
- props: {
-
- value: {
- type: [Array, String],
- require: true,
- },
-
- referName: {
- type: String,
- require: true,
- },
-
- valueKey: {
- type: String,
- dafault: () => {
- return "code";
- },
- },
-
- queryParams: {
- type: Function,
- default: () => {},
- },
-
- source: {
- type: Object,
- default: () => ({}),
- },
-
- dataMapping: {
- type: Object,
- default: () => ({}),
- },
- },
- components: {},
- data() {
- return {
- size: "mini",
- width: "50%",
- page: { pageNum: 1, pageSize: 10, total: 0 },
- visible: false,
- loading: false,
- model: {
- search: "",
- isPage: true,
- },
- data: [],
- selectData: [],
- };
- },
- computed: {
- innerValue: {
- get() {
- return this.value;
- },
- set(value) {
- this.$emit("input", value);
- },
- },
- tableColumns() {
- const { referName } = this.$props;
- return require(`../popover-select/components/${referName}`).default.filter(
- (document) => document.key
- );
- },
- },
- watch: {},
- methods: {
-
- async open() {
- this.visible = true;
- await this.useReset();
- },
-
- async hide() {
- this.visible = false;
- },
-
- async fetchList(prop, page) {
- try {
-
- this.loading = true;
- const { pageNum, pageSize } = page;
- const { referName: type, source, queryParams } = this.$props;
- const { code, rows, total } = await REFER(
- {
- ...prop,
- ...queryParams(source),
- type: type,
- },
- {
- pageNum,
- pageSize,
- }
- );
- if (code === 200) {
- this.data = rows;
- this.page.total = total;
- }
- } catch (err) {
-
- console.error(err);
- } finally {
-
- this.loading = false;
- }
- },
-
- async useReset() {
- this.data = [];
- this.model.search = null;
- await this.fetchList(this.model, this.page);
- },
-
- async useQuery() {
- await this.fetchList(this.model, this.page);
- },
-
- async useAutocomplete(prop, cb) {
- if (prop) {
- this.model.search = prop;
- await this.fetchList(this.model, this.page);
- await cb(this.data);
- } else {
- cb([]);
- }
- },
-
- useSelect(prop) {
- this.selectData = prop;
- },
-
- useConfirm(prop) {
- this.hide();
- const {
- $props: { source, valueKey, dataMapping },
- } = this;
- for (let key in dataMapping) {
- source[key] = prop[dataMapping[key]];
- }
- this.$emit("update:source", source);
- this.$emit("input", prop[valueKey]);
- this.$emit("change", prop, this.$props);
- },
- },
- created() {},
- mounted() {},
- destroyed() {},
- };
- </script>
- <template>
- <div class="popover-select-v2">
- <el-autocomplete
- v-bind="$attrs"
- v-model="innerValue"
- :value-key="valueKey"
- :fetch-suggestions="useAutocomplete"
- @select="useConfirm"
- style="width: 100%"
- >
- <i class="el-icon-search" slot="suffix" @click="open"> </i>
- <template slot-scope="{ item }">
- <p
- style="
- text-overflow: ellipsis;
- overflow: hidden;
- line-height: 16px;
- margin: 5px 0;
- "
- >
- {{ item.name }}
- </p>
- <p
- style="
- font-size: 12px;
- color: #b4b4b4;
- line-height: 16px;
- margin: 5px 0;
- "
- >
- {{ item.code }}
- </p>
- </template>
- </el-autocomplete>
- <el-dialog
- :width="width"
- :show-close="false"
- :visible.sync="visible"
- :close-on-click-modal="false"
- :close-on-press-escape="false"
- append-to-body
- >
- <template slot="title">
- <div
- style="
- display: flex;
- justify-content: space-between;
- align-items: center;
- "
- >
- <span>选 择</span>
- <span>
- <el-button
- :size="size"
- circle
- icon="el-icon-check"
- @click="useConfirm(selectData[0])"
- >
- </el-button>
- <el-button
- :size="size"
- circle
- type="danger"
- icon="el-icon-close"
- @click="hide"
- ></el-button>
- </span>
- </div>
- </template>
- <el-form
- v-loading="loading"
- :inline="true"
- :model="model"
- :size="size"
- @submit.native.prevent
- >
- <el-form-item prop="search">
- <el-input
- v-model="model.search"
- @change="useQuery"
- @keydown.enter="useQuery"
- >
- </el-input>
- </el-form-item>
- <el-form-item>
- <el-button icon="el-icon-refresh" @click="useReset"></el-button>
- </el-form-item>
- <el-table
- :data="data"
- :size="size"
- height="45vh"
- highlight-current-row
- @row-click="useSelect([$event])"
- @row-dblclick="useConfirm"
- >
- <el-table-column
- v-for="(column, index) in tableColumns"
- :key="index"
- :prop="column.key"
- :label="column.title"
- :width="column.width || 'auto'"
- show-overflow-tooltip
- >
- <template slot-scope="scope">
- <dr-computed-input
- v-if="column.type === 'ComputedInput'"
- v-model="scope.row[column.key]"
- :source="scope.row"
- :formatter="column.formatter"
- :placeholder="column.placeholder"
- style="width: 100%"
- ></dr-computed-input>
- <span v-else> {{ scope.row[column.key] }}</span>
- </template>
- </el-table-column>
- </el-table>
- <pagination
- v-show="!loading"
- :total="page.total"
- :page.sync="page.pageNum"
- :limit.sync="page.pageSize"
- @pagination="useQuery"
- />
- </el-form>
- </el-dialog>
- </div>
- </template>
- <style scoped>
- .popover-select-v2 .el-autocomplete {
- width: inherit;
- }
- .popover-select-v2 .el-autocomplete .el-icon-search {
- cursor: pointer;
- }
- ::v-deep .el-table--mini .el-table__cell {
- height: 50px;
- }
- </style>
|