123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- <script>
- import { REFER } from "./api/index";
- import deepCopy from "@gby/deep-copy";
- export default {
- name: "PopoverTreeSelect",
- props: {
-
- type: {
- type: String,
- require: true,
- },
-
- value: {
- type: [Array, String],
- require: true,
- },
-
- title: {
- type: String,
- default: "TITLE",
- },
-
- valueKey: {
- type: String,
- default: "code",
- },
-
- queryParams: {
- type: Function,
- default: () => {},
- },
-
- size: {
- type: String,
- default: "mini",
- },
-
- placeholder: {
- type: String,
- default: "",
- },
-
- readonly: {
- type: Boolean,
- default: false,
- },
-
- disabled: {
- type: Boolean,
- default: false,
- },
-
- clearable: {
- type: Boolean,
- default: false,
- },
-
- multiple: {
- type: Boolean,
- default: false,
- },
-
- source: Object,
-
- dataMapping: Object,
- },
- components: {},
- data() {
- return {
- width: "50%",
- visible: false,
- loading: false,
- params: {
- search: "",
- isPage: true,
- },
- data: [],
- selectData: [],
- lastSelectData: [],
- defaultProps: {
- label: "name",
- children: "children",
- },
- };
- },
- 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.lastSelectData = [];
- },
- immediate: true,
- },
- },
- methods: {
-
- emitChange(prop) {
- const { type, source, multiple } = this.$props;
- this.$emit("change", multiple ? prop : prop[0], source, type);
- },
-
- async open() {
- this.visible = true;
- await this.useReset();
- },
-
- async hide() {
- this.visible = false;
- },
-
- async fetchList(prop) {
- try {
- this.loading = true;
- const { code, rows } = await REFER(prop);
- if (code === 200) {
- this.data = rows;
- }
- } catch (err) {
-
- console.error(err);
- } finally {
-
- this.loading = false;
- }
- },
-
- async useReset() {
- const { type, source, queryParams } = this.$props;
- this.model = {
- type,
- search: "",
- ...this.model,
- ...queryParams(source),
- };
- await this.fetchList(this.model);
- },
-
- async useQuery() {
- await this.fetchList(this.model);
- },
-
- useCancel(prop) {
- const { multiple } = this.$props;
- this.useUpdate(multiple ? prop : prop[0]);
- this.hide();
- },
-
- useConfirm(prop) {
- const { multiple } = this.$props;
- this.useUpdate(multiple ? prop : prop[0]);
- this.emitChange(this.selectData);
- this.lastSelectData = deepCopy(this.selectData);
- this.hide();
- },
-
- useDelete(prop) {
- this.selectData.splice(prop, 1);
- this.useUpdate(this.selectData);
- this.emitChange(this.selectData);
- this.lastSelectData = deepCopy(this.selectData);
- },
-
- useUpdate(prop) {
- const { source, multiple, valueKey, dataMapping, type } = this.$props;
-
- if (multiple) {
- const vModel = prop.map((item) => item[valueKey]);
- this.$emit("input", vModel);
- } else {
- const vModel = prop[valueKey];
- this.$emit("input", vModel);
- for (let key in dataMapping) {
- source[key] = prop[dataMapping[key]];
- }
- this.$emit("update:source", source);
- }
- },
-
- onceSelect(prop) {
- if (prop) this.selectData = [prop];
- },
-
- doubleSelect(prop) {
- this.useConfirm([prop]);
- },
-
- selectionChange(prop, data) {
- if (prop) {
- this.selectData.push(data);
- } else {
- const { code } = data;
- const index = this.selectData.findIndex((item) => item.code === code);
- this.selectData.splice(index, 1);
- }
- },
- },
- created() {},
- mounted() {},
- destroyed() {},
- };
- </script>
- <template>
- <div class="popover-tree-select">
- <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="params"
- @submit.native.prevent
- >
- <el-form-item prop="search">
- <el-input
- v-model="params.search"
- @keydown.enter="useQuery"
- @change="useQuery"
- >
- </el-input>
- </el-form-item>
- <el-form-item>
- <el-button icon="el-icon-refresh" @click="useReset"></el-button>
- </el-form-item>
- </el-form>
- <el-tree
- v-if="multiple"
- :data="data"
- :props="defaultProps"
- accordion
- node-key="id"
- >
- <div slot-scope="{ node, data }">
- <el-checkbox
- v-model="data.checked"
- @click.native.stop
- @change="selectionChange($event, data)"
- style="margin: 0 5px 0 0"
- >
- </el-checkbox>
- <span> {{ data.name }}</span>
- </div>
- </el-tree>
- <el-radio-group v-else v-model="radio" style="width: 100%">
- <el-tree
- v-loading="loading"
- :data="data"
- :props="defaultProps"
- accordion
- node-key="id"
- >
- <div slot-scope="{ node, data }">
- <el-radio
- :label="data.code"
- @click.native.stop="onceSelect(data)"
- @dblclick.native.stop="doubleSelect(data)"
- style="margin: 0 5px 0 0"
- >
- {{ data.name }}
- </el-radio>
- </div>
- </el-tree>
- </el-radio-group>
- <div style="margin-top: 20px; text-align: right">
- <el-button :size="size" @click="visible = false">取 消</el-button>
- <el-button :size="size" type="primary" @click="useConfirm(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 && lastSelectData.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">
- + {{ lastSelectData.length }}
- </el-tag>
- <el-tag
- v-for="(tag, index) in lastSelectData"
- :size="size"
- hit
- closable
- :style="{
- display: 'flex',
- justifyContent: 'space-between',
- alignItems: 'center',
- margin: lastSelectData.length - 1 === index ? '0' : '0 0 5px 0',
- }"
- @close="useDelete(index)"
- >
- {{ tag.name }}
- </el-tag>
- </el-popover>
- </div>
- </div>
- </div>
- </template>
- <style scoped></style>
|