aabbcc/src/views/labelPage.vue
2024-06-17 16:48:19 +08:00

264 lines
5.8 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<div>
<header>
<navigationBar />
</header>
<main class="main-content">
<section class="box w">
<header class="box-hd">
<h3>课程笔记</h3>
</header>
<div class="box-bd">
<ul class="clearfix">
<!-- v-for 循环中为每个项目添加一个删除按钮/图标 -->
<li v-for="(item, index) in items" :key="index">
<router-link :to="`/paiListPage/${index + 1}`">
<!-- 这里的index + 1对应着表的序号 -->
<img :src="item.url" :alt="item.title" />
<h4>{{ item.title }}</h4>
</router-link>
<!-- 删除按钮/图标 -->
<button class="delete-btn" @click="deleteItem(index)">删除</button>
</li>
</ul>
</div>
</section>
</main>
<div class="add">
<button @click="openDialog">添加笔记</button>
</div>
<!-- 添加笔记弹窗 -->
<el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
<el-form ref="form" :model="form" label-width="80px" :rules="rules">
<el-form-item label="笔记标题" prop="addContent">
<el-input v-model="form.addContent" placeholder="请输入标题"></el-input>
</el-form-item>
<el-form-item label="表名" prop="tableName">
<el-input v-model="form.tableName" placeholder="请输入表名(英文)"></el-input>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="handleCancel">取消</el-button>
<el-button type="primary" @click="handleAdd">确定</el-button>
</span>
</el-dialog>
<musIc />
</div>
</template>
<script>
import { getTags, addTag, deleteTag } from '@/api/tags'
export default {
data () {
return {
items: [],
dialogVisible: false,
form: {
addContent: '',
tableName: ''
},
rules: {
addContent: [
{ required: true, message: '请输入标题', trigger: 'blur' }
],
tableName: [{ required: true, message: '请输入表名', trigger: 'blur' }]
}
}
},
created () {
this.fetchTags()
},
methods: {
fetchTags () {
getTags().then((res) => {
this.items = res.data
})
},
openDialog () {
this.dialogVisible = true
},
// 取消按钮
handleCancel () {
this.dialogVisible = false
this.form.addContent = ''
this.form.tableName = ''
this.$refs.form.resetFields()
},
/** 添加标签 */
handleAdd () {
this.$refs.form.validate((valid) => {
if (valid) {
const newTag = {
title: this.form.addContent,
table_name: this.form.tableName
}
console.log('Sending data:', newTag)
addTag(newTag)
.then(() => {
this.fetchTags()
this.dialogVisible = false
this.form.addContent = ''
this.form.tableName = ''
this.$message({
message: '添加成功',
type: 'success'
})
this.handleCancel()
})
.catch((error) => {
console.error('添加标签失败:', error)
this.$message({
message: '添加标签失败',
type: 'error'
})
})
}
})
},
/** 删除标签 */
deleteItem (index) {
if (confirm('确定要删除此项目吗?')) {
// 执行删除操作
deleteTag(this.items[index].id)
.then(() => {
// 从列表中删除该项目
this.items.splice(index, 1)
this.$message({
message: '项目删除成功',
type: 'success'
})
})
.catch((error) => {
console.error('删除项目时出错:', error)
// 可选,显示错误消息
this.$message({
message: '删除项目时出错',
type: 'error'
})
})
}
}
}
}
</script>
<style lang="less" scoped>
.w {
width: 1200px;
margin: auto;
}
li {
list-style: none;
position: relative;
}
a {
text-decoration: none;
}
.clearfix:before,
.clearfix:after {
content: "";
display: table;
}
.clearfix:after {
clear: both;
}
.clearfix img {
width: 100%;
height: 220px;
display: block;
}
.box {
margin-top: 80px;
border-bottom: 1px solid #d3d3d3;
}
.box-hd {
height: 45px;
}
.box-hd h3 {
float: left;
font-size: 20px;
color: #494949;
}
.box-bd ul {
width: 1225px;
}
.box-bd ul li {
position: relative;
top: 0;
float: left;
width: 228px;
height: 270px;
background-color: #fff;
margin-right: 15px;
margin-bottom: 15px;
transition: all 0.3s;
}
.box-bd ul li a {
display: block;
}
.box-bd ul li:hover {
top: -8px;
box-shadow: 0 15px 30px rgb(0 0 0 / 10%);
}
.box-bd ul li img {
width: 100%;
}
.box-bd ul li h4 {
margin: 20px 20px 20px 25px;
font-size: 16px;
color: #050505;
font-weight: 500;
text-align: center;
opacity: 0;
transition: all 0.3s;
}
.box-bd ul li:hover h4 {
color: #006cff;
opacity: 1;
}
.box-bd ul li .delete-btn {
position: absolute;
top: 0;
right: 0;
background-color: rgba(255, 106, 106, 0.2);
color: rgba(248, 146, 51, 0.897);
border: none;
width: 50px;
height: 30px;
cursor: pointer;
font-size: 16px;
// 阴影
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
opacity: 0;
}
.box-bd ul li:hover .delete-btn {
opacity:1;
}
.add {
position: fixed;
bottom: 110px;
right: 20px;
button {
background-color: #006cff;
color: #fff;
border: none;
width: 100px;
height: 40px;
border-radius: 30px;
font-size: 16px;
cursor: pointer;
}
}
</style>