-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeywordTable.vue
139 lines (123 loc) · 3.2 KB
/
KeywordTable.vue
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
<template>
<div class="keywords-table">
<h2>关键字</h2>
<div class="table-container">
<table>
<thead>
<tr>
<th>ID</th>
<th>关键字</th>
</tr>
</thead>
<tbody>
<tr v-for="keyword in keywords" :key="keyword.id">
<td>{{ keyword.id }}</td>
<td>
<input v-model="keyword.keyword" />
</td>
</tr>
</tbody>
</table>
</div>
<div class="buttons">
<button @click="fetchKeywords">查询</button>
<button @click="addKeyword">添加</button>
<button @click="updateKeywords">修改</button>
</div>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import axios from 'axios'
// Define reactive data
const keywords = ref([])
// Define methods
const fetchKeywords = async () => {
try {
const response = await axios.get('/keywords')
keywords.value = response.data.data
// console.log('Keywords fetched successfully:', keywords.value)
alert('获取关键字成功!')
} catch (error) {
console.error('Error fetching keywords:', error)
alert('获取关键字失败!' + error.response.data.msg)
}
}
const addKeyword = () => {
const id = keywords.value.length > 0 ? keywords.value[keywords.value.length - 1].id + 1 : 1
keywords.value.push({ id, name: '' })
}
const updateKeywords = async () => {
try {
await axios.put('/keywords', keywords.value)
console.log('Keywords updated successfully')
alert('修改成功!')
} catch (error) {
console.error('Error updating keywords:', error)
alert('修改关键字失败!' + error.response.data.msg)
}
}
// Fetch keywords on component mount
onMounted(fetchKeywords)
</script>
<style scoped>
.keywords-table {
padding: 30px;
border: 1px solid #ccc;
border-radius: 8px;
width: 100%;
max-width: 600px;
margin: 0 auto;
font-family: Arial, sans-serif;
background-color: #f9f9f9;
}
h2 {
margin-top: 0;
font-size: 24px;
color: #333;
}
.table-container {
height: 400px;
overflow-y: auto;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
th,
td {
border: 1px solid #ddd;
padding: 12px;
text-align: center;
background-color: #fff;
}
th {
background-color: #f1f1f1;
}
input {
width: 100%;
padding: 8px;
text-align: center;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 4px;
}
.buttons {
display: flex;
justify-content: space-between;
}
button {
padding: 10px 16px;
border: none;
border-radius: 4px;
background-color: #007bff;
color: white;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #0056b3;
}
</style>