-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathindex.vue
More file actions
175 lines (161 loc) · 4.46 KB
/
index.vue
File metadata and controls
175 lines (161 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<template>
<div class="app-table" :class="quickCreate ? 'table-quick-append' : ''">
<el-table :max-height="tableHeight" v-bind="$attrs" ref="appTableRef">
<template #append v-if="quickCreate">
<div v-if="showInput">
<el-input
ref="quickInputRef"
v-model="inputValue"
:placeholder="`${$t('common.inputPlaceholder')} ${quickCreateName}`"
class="w-500 mr-12"
autofocus
:maxlength="quickCreateMaxlength || '-'"
:show-word-limit="quickCreateMaxlength ? true : false"
@keydown.enter="submitHandle"
clearable
/>
<el-button type="primary" @click="submitHandle" :disabled="loading">{{
$t('common.create')
}}</el-button>
<el-button @click="showInput = false" :disabled="loading">{{
$t('common.cancel')
}}</el-button>
</div>
<div v-else @click="quickCreateHandle" class="w-full">
<el-button type="primary" link class="quich-button">
<AppIcon iconName="app-add-outlined"></AppIcon>
<span class="ml-4">{{ quickCreatePlaceholder }}</span>
</el-button>
</div>
</template>
<slot></slot>
</el-table>
<div class="app-table__pagination mt-16" v-if="$slots.pagination || paginationConfig">
<slot name="pagination">
<el-pagination
v-model:current-page="paginationConfig.current_page"
v-model:page-size="paginationConfig.page_size"
:page-sizes="paginationConfig.page_sizes || pageSizes"
:total="paginationConfig.total"
layout="total, prev, pager, next, sizes"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
</slot>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, nextTick, watch, computed, onMounted } from 'vue'
import { MsgError } from '@/utils/message'
import { t } from '@/locales'
defineOptions({ name: 'AppTable' })
import useStore from '@/stores'
const { common } = useStore()
const props = defineProps({
paginationConfig: {
type: Object,
default: () => {},
},
quickCreate: {
type: Boolean,
default: false,
},
quickCreateName: {
type: String,
default: t('components.quickCreateName'),
},
quickCreatePlaceholder: {
type: String,
default: t('components.quickCreatePlaceholder'),
},
quickCreateMaxlength: {
type: Number,
default: () => 0,
},
storeKey: String,
maxTableHeight: {
type: Number,
default: 300,
},
})
const emit = defineEmits(['changePage', 'sizeChange', 'creatQuick'])
const paginationConfig = computed(() => props.paginationConfig)
const pageSizes = [10, 20, 50, 100]
const quickInputRef = ref()
const appTableRef = ref()
const loading = ref(false)
const showInput = ref(false)
const inputValue = ref('')
const tableHeight = ref(0)
watch(showInput, (bool) => {
if (!bool) {
inputValue.value = ''
}
})
function submitHandle() {
if (inputValue.value) {
loading.value = true
emit('creatQuick', inputValue.value)
setTimeout(() => {
showInput.value = false
loading.value = false
}, 200)
} else {
MsgError(`${props.quickCreateName} ${t('dynamicsForm.tip.requiredMessage')}`)
}
}
function quickCreateHandle() {
showInput.value = true
nextTick(() => {
quickInputRef.value?.focus()
})
}
function handleSizeChange() {
emit('sizeChange')
if (props.storeKey) {
common.savePage(props.storeKey, props.paginationConfig)
}
}
function handleCurrentChange() {
emit('changePage')
if (props.storeKey) {
common.savePage(props.storeKey, props.paginationConfig)
}
}
function clearSelection() {
appTableRef.value?.clearSelection()
}
function toggleRowSelection(row: any, selected?: boolean, ignoreSelectable = true) {
appTableRef.value?.toggleRowSelection(row, selected, ignoreSelectable)
}
function getSelectionRows() {
return appTableRef.value?.getSelectionRows()
}
defineExpose({
clearSelection,
toggleRowSelection,
getSelectionRows,
})
onMounted(() => {
tableHeight.value = window.innerHeight - props.maxTableHeight
window.onresize = () => {
return (() => {
tableHeight.value = window.innerHeight - props.maxTableHeight
})()
}
})
</script>
<style lang="scss" scoped>
.app-table {
&__pagination {
display: flex;
justify-content: flex-end;
}
.quich-button {
&:hover {
color: var(--el-button-text-color);
}
}
}
</style>