main
LeJingS 3 months ago
parent 573c44a239
commit 05e94f373d

@ -8,8 +8,10 @@
"build": "run-p type-check \"build-only {@}\" --", "build": "run-p type-check \"build-only {@}\" --",
"preview": "vite preview", "preview": "vite preview",
"build-only": "vite build", "build-only": "vite build",
"type-check": "vue-tsc --build" "type-check": "vue-tsc --build",
"serve": "vue-cli-service serve --port 5173"
}, },
"dependencies": { "dependencies": {
"axios": "^1.7.8", "axios": "^1.7.8",
"vue": "^3.5.13" "vue": "^3.5.13"

@ -1,27 +1,26 @@
<template> <template>
<div class="person"> <div class="person">
<!-- 输入区域 -->
<div class="operate-container">
<div class="operate"> <div class="operate">
id<input type="text" name="id"> <label>id<input type="text" name="id" v-model="queryId"></label>
姓名<input type="text" name="name"> <label>姓名<input type="text" name="name" v-model="queryName"></label>
密码<input type="text" name="password"> <label>密码<input type="text" name="password" v-model="queryPassword"></label>
邮箱<input type="text" name="email"> <label>邮箱<input type="text" name="email" v-model="queryEmail"></label>
<button @click="findList"></button> <button @click="findList(queryId, queryName, queryPassword, queryEmail)">查询</button>
</div> </div>
<div class="operate"> <div class="operate">
<!-- id<input type="text" name="addId" v-model="addId"> --> <label>姓名<input type="text" name="addName" v-model="addName"></label>
姓名<input type="text" name="addName" v-model="addName"> <label>密码<input type="text" name="addPassword" v-model="addPassword"></label>
密码<input type="text" name="addPassword" v-model="addPassword"> <label>邮箱<input type="text" name="addEmail" v-model="addEmail"></label>
邮箱<input type="text" name="addEmail" v-model="addEmail"> <button @click="addList(addName, addPassword, addEmail)">添加</button>
<button @click="addList"></button> </div>
</div> </div>
<div class="list"> <!-- 表格区域 -->
<div class="list-container">
<table border="1"> <table border="1">
<thead>
<tr> <tr>
<th>id</th> <th>id</th>
<th>name</th> <th>name</th>
@ -29,100 +28,220 @@
<th>email</th> <th>email</th>
<th>操作</th> <th>操作</th>
</tr> </tr>
</thead>
<tbody>
<tr v-for="g in people" :key="g.id"> <tr v-for="g in people" :key="g.id">
<td>{{ g.id }}</td> <td>{{ g.id }}</td>
<td>{{ g.name }}</td> <td>{{ g.name }}</td>
<td>{{ g.password }}</td> <td>{{ g.password }}</td>
<td>{{ g.email }}</td> <td>{{ g.email }}</td>
<td> <td>
<button @click="updateItem(g.id)"></button> <button @click="openModal(g)"></button>
<button @click="deleteItem(g.id)"></button> <button @click="deleteItem(g.id)"></button>
</td> </td>
</tr> </tr>
</tbody>
</table> </table>
</div> </div>
<!-- 模态框 -->
<div v-if="showModal" class="modal-overlay" @click="closeModal">
<div class="modal" @click.stop>
<h3>修改信息</h3>
<label>姓名<input type="text" v-model="editingItem.name"></label>
<label>密码<input type="text" v-model="editingItem.password"></label>
<label>邮箱<input type="text" v-model="editingItem.email"></label>
<button @click="updateItemConfirm"></button>
<button @click="closeModal"></button>
</div>
</div>
</div> </div>
</template> </template>
<script setup> <script setup>
import { ref, reactive, onMounted } from 'vue' import { ref, reactive, onMounted } from 'vue'
import axios from 'axios' import axios from 'axios'
let people = reactive([]) let people = reactive([])
//
let addName = ref('')
let addPassword = ref('')
let addEmail = ref('')
//
let queryId = ref('')
let queryName = ref('')
let queryPassword = ref('')
let queryEmail = ref('')
//
let editingItem = ref(null)
let showModal = ref(false)
// api // api
function getAll() { function getAll() {
axios.get('http://localhost:8080/selectAll') axios.get('http://localhost:8080/selectAll')
.then(res => { .then(res => {
// 使 // 使
people.splice(0, people.length); people.splice(0, people.length)
people.push(...res.data) people.push(...res.data)
}) })
.catch(error => console.error('There was an error fetching the data!', error)) .catch(error => console.error('There was an error fetching the data!', error))
} }
//
function findList(queryId, queryName, queryPassword, queryEmail) {
axios.get(`http://localhost:8080/select?id=${queryId}&name=${queryName}&password=${queryPassword}&email=${queryEmail}`)
.then(res => {
console.log(res)
people.splice(0, people.length)
people.push(...res.data)
if (res.data.length === 0) {
alert("没有该数据")
getAll()
}
})
}
//
// //
let addId = ref('');
let addName = ref('');
let addPassword = ref('');
let addEmail = ref('');
function addList() { function addList() {
games.push({ if (!addName.value || !addPassword.value || !addEmail.value) {
id: addId.value, alert("请输入完整的姓名、密码和邮箱");
name: addName.value, return;
password: addPassword.value, }
email: addEmail.value
}); axios.get(`http://localhost:8080/insert?name=${addName.value}&password=${addPassword.value}&email=${addEmail.value}`)
.then(res => {
console.log(res)
// //
addId.value = '';
addName.value = ''; addName.value = '';
addPassword.value = ''; addPassword.value = '';
addEmail.value = ''; addEmail.value = '';
getAll();
})
.catch(error => {
console.error('There was an error adding the item!', error);
if (error.response && error.response.status === 500) {
alert("服务器内部错误,请检查输入数据是否符合要求");
}
})
}
//
function openModal(item) {
editingItem.value = { ...item }
showModal.value = true
}
//
function closeModal() {
showModal.value = false
}
console.log(games); //
function updateItemConfirm() {
const { id, name, password, email } = editingItem.value
axios.get(`http://localhost:8080/update?id=${id}&name=${name}&password=${password}&email=${email}`)
.then(res => {
console.log(res)
closeModal()
getAll()
})
.catch(error => console.error('There was an error updating the item!', error))
} }
// deleteItem(g.id) // deleteItem(g.id)
function deleteItem(id) { function deleteItem(id) {
console.log(id); console.log(id)
axios.delete(`http://localhost:8080/delete?id=${id}`) axios.get(`http://localhost:8080/delete?id=${id}`)
getAll(); .then(() => {
getAll()
})
.catch(error => console.error('There was an error deleting the item!', error))
} }
onMounted(() => { onMounted(() => {
getAll(); getAll()
}); })
</script>
<style>
.person {
display: flex;
flex-direction: column;
height: 100vh;
}
.operate-container {
padding: 10px;
background-color: #f9f9f9;
border-bottom: 1px solid #ddd;
}
.operate {
display: flex;
justify-content: center;
gap: 10px;
align-items: center;
margin-bottom: 10px;
}
.list-container {
flex: 1;
overflow-y: auto;
padding: 10px;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
</script> th {
background-color: #f2f2f2;
}
<style> /* 模态框样式 */
.person .list { .modal-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
display: flex; display: flex;
justify-content: center; /* 水平居中 */ justify-content: center;
align-items: center; /* 垂直居中 */ align-items: center;
height: 100vh; /* 使容器高度占满整个视口 */ }
.modal {
background: white;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
width: 300px;
}
.modal label {
display: block;
margin-bottom: 10px;
}
.modal input {
width: 100%;
padding: 5px;
margin-bottom: 10px;
} }
.person .operate {
display: flex; /* 启用 Flexbox 布局 */
justify-content: center; /* 水平居中 */
gap: 10px; /* 设置元素之间的间距 */
align-items: center; /* 垂直居中对齐 */
.modal button {
margin-right: 10px;
} }
</style> </style>

@ -0,0 +1,6 @@
{
"name": "Font",
"lockfileVersion": 3,
"requires": true,
"packages": {}
}
Loading…
Cancel
Save