123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 |
- <script>
- import { refer } from "./api/index";
- export default {
- name: "PopoverSelect",
- props: {
- // 参照类型 ,对应后端
- type: {
- type: String,
- require: true,
- },
- // v-model
- value: {
- type: [Array, String],
- require: true,
- },
- // 参照弹窗标题
- title: {
- type: String,
- dafault: () => {
- return "TITEL";
- },
- },
- // 作为 value 唯一标识的键名,绑定值
- valueKey: {
- type: String,
- dafault: () => {
- return "code";
- },
- },
- // 默认查询参数
- queryParams: {
- type: Object,
- default: () => ({}),
- },
- // 组件大小
- size: {
- type: String,
- dafault: () => {
- return "mini";
- },
- },
- // 提示
- placeholder: {
- type: String,
- dafault: () => {
- return "";
- },
- },
- // 是否可读
- readonly: {
- type: Boolean,
- dafault: () => {
- return false;
- },
- },
- // 是否禁止
- disabled: {
- type: Boolean,
- dafault: () => {
- return false;
- },
- },
- // 是否清除
- clearable: {
- type: Boolean,
- dafault: () => {
- return false;
- },
- },
- // 是否多选
- multiple: {
- type: Boolean,
- dafault: () => {
- return false;
- },
- },
- // 需映射源数据
- source: Object,
- // 参照内外映射
- dataMapping: Object,
- },
- components: {},
- data() {
- return {
- width: "50%",
- page: { pageNum: 1, pageSize: 25, total: 0 },
- layout: "total, prev, pager, next, sizes, jumper",
- pageSizes: [25, 50, 100],
- visible: false,
- loading: false,
- model: {
- search: "",
- isPage: true,
- },
- data: [],
- selectData: [],
- };
- },
- computed: {
- innerValue() {
- const { value, multiple } = this.$props;
- return multiple ? "" : value;
- },
- TableColumnTemp() {
- const { type } = this.$props;
- const documents = require(`./components/${type}`).default;
- return documents.filter((document) => document.key);
- },
- },
- watch: {
- "$props.value": {
- handler: function (newProp) {
- if (!newProp) this.selectData = [];
- },
- immediate: true,
- },
- },
- methods: {
- // open dialog
- async open() {
- this.visible = true;
- await this.resetList();
- },
- // hide dialog
- async hide() {
- this.visible = false;
- },
- // fetch list
- async fetchList(prop, page) {
- try {
- this.loading = true;
- const { pageNum, pageSize } = page;
- const { code, msg, rows, total } = await refer(prop, {
- pageNum,
- pageSize,
- });
- if (code === 200) {
- this.data = rows;
- this.page.total = total;
- }
- } catch (err) {
- //
- } finally {
- this.loading = false;
- }
- },
- // reset list
- async resetList() {
- const { type, queryParams } = this.$props;
- this.model = {
- ...queryParams,
- ...this.model,
- search: "",
- type,
- };
- await this.fetchList(this.model, this.page);
- },
- // query list
- async queryList() {
- await this.fetchList(this.model, this.page);
- },
- // row click
- rowClick(prop) {
- const { multiple } = this.$props;
- // 单选
- if (!multiple) this.$refs.multipleTable.clearSelection();
- [prop].forEach((row) => this.$refs.multipleTable.toggleRowSelection(row));
- },
- // row double click
- async rowDblclick(prop) {
- const { multiple } = this.$props;
- if (!multiple) await this.confirm([prop]);
- },
- // selection change
- selectionChange(prop) {
- this.selectData = prop;
- },
- // page size change
- async pageSizeChange(prop) {
- this.page.pageSize = prop;
- await this.queryList();
- },
- // page number change
- async pageNumberChange(prop) {
- this.page.pageNum = prop;
- await this.queryList();
- },
- // confirm
- async confirm(prop) {
- const { multiple } = this.$props;
- await this.hide();
- await this.update(multiple ? prop : prop[0]);
- },
- // delete tag
- deleteTag(prop) {
- this.selectData.splice(prop, 1);
- this.update(this.selectData);
- },
- // update
- update(prop) {
- const { source, multiple, valueKey, dataMapping, type } = this.$props;
- // update v-model
- const vModel = multiple
- ? prop.map((item) => item[valueKey])
- : prop[valueKey];
- this.$emit("input", vModel);
- // update data mapping
- for (let key in dataMapping) {
- source[key] = prop[dataMapping[key]];
- }
- this.$emit("update:source", source);
- // emit change
- this.$emit("change", prop, source, type);
- },
- },
- created() {},
- mounted() {},
- destroyed() {},
- };
- </script>
- <template>
- <div>
- <el-input
- v-model="innerValue"
- :size="size"
- :disabled="disabled"
- :readonly="readonly"
- :clearable="clearable"
- :placeholder="placeholder"
- >
- <el-button
- :disabled="disabled"
- slot="append"
- icon="el-icon-search"
- @click="open"
- ></el-button>
- </el-input>
- <el-dialog
- :title="`${title}(${multiple ? '多选' : '单选'})`"
- :width="width"
- :visible.sync="visible"
- :close-on-click-modal="false"
- :close-on-press-escape="false"
- append-to-body
- @close="hide"
- >
- <el-form
- v-loading="loading"
- :size="size"
- :inline="true"
- :model="model"
- @submit.native.prevent
- >
- <el-form-item prop="search">
- <el-input
- v-model="model.search"
- @change="queryList"
- @keydown.enter="queryList"
- >
- </el-input>
- </el-form-item>
- <el-form-item>
- <el-button icon="el-icon-refresh" @click="resetList"></el-button>
- </el-form-item>
- <el-table
- ref="multipleTable"
- :data="data"
- :size="size"
- height="45vh"
- highlight-current-row
- style="width: 100%; margin-bottom: 20px"
- @row-click="rowClick"
- @row-dblclick="rowDblclick"
- @selection-change="selectionChange"
- >
- <el-table-column width="55" type="selection" align="center">
- </el-table-column>
- <el-table-column
- v-for="(column, index) in TableColumnTemp"
- :key="index"
- :prop="column.key"
- :label="column.title"
- :width="column.width"
- show-overflow-tooltip
- >
- </el-table-column>
- </el-table>
- <el-pagination
- :layout="layout"
- :total="page.total"
- :page-sizes="pageSizes"
- :small="size === 'mini'"
- :page-size="page.pageSize"
- :current-page="page.pageNum"
- background
- @size-change="pageSizeChange"
- @current-change="pageNumberChange"
- >
- </el-pagination>
- </el-form>
- <div style="margin-top: 20px; text-align: right">
- <el-button :size="size" @click="hide"> 取 消 </el-button>
- <el-button :size="size" @click="confirm(selectData)"> 确 定 </el-button>
- </div>
- </el-dialog>
- <div
- style="
- position: absolute;
- left: 10px;
- top: 50%;
- transform: translateY(-50%);
- padding-right: 30px;
- overflow: hidden;
- "
- >
- <div v-if="multiple && selectData.length">
- <el-popover
- :offset="-10"
- :width="width"
- :visible-arrow="false"
- title=""
- content=""
- trigger="click"
- placement="bottom-start"
- >
- <el-tag slot="reference" :size="size" style="margin-right: 10px">
- + {{ selectData.length }}
- </el-tag>
- <el-tag
- v-for="(tag, index) in selectData"
- :size="size"
- hit
- closable
- :style="{
- display: 'flex',
- justifyContent: 'space-between',
- alignItems: 'center',
- margin: selectData.length - 1 === index ? '0' : '0 0 5px 0',
- }"
- @close="deleteTag(index)"
- >
- {{ tag.name }}
- </el-tag>
- </el-popover>
- </div>
- </div>
- </div>
- </template>
- <style scoped></style>
|