vue3 + element-plus table 树形数据 展开关闭

tabel default-expand-all属性 boolean 单独使用起效 加上判断就不在正常工作
所以就换了一种方式 使用 toggleRowExpansion
在这里插入图片描述

 先绑定元素 这边使用的是vue3 引入 ref
 注册元素
 //页面操作
  <el-table
      :data="demo1.tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      :props="treeConfig"
      ref="xTree"
    >
 </el-table> 
 /**
	treeConfig: {    
	     children: "children",
	        expandAll:false,
	        reserve:true
	 },
	**/
 <el-button @click="openRowExpansion">展开所有<
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
以下是drf+vue3+element-plus搭建CRUD的案例: 1. 创建Django项目,命名为backend,创建Django APP,命名为api。 2. 在api中创建models.py文件,定义模型: ``` from django.db import models class Book(models.Model): title = models.CharField(max_length=100) author = models.CharField(max_length=100) price = models.DecimalField(max_digits=10, decimal_places=2) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) def __str__(self): return self.title ``` 3. 在api中创建serializers.py文件,定义序列化器: ``` from rest_framework import serializers from .models import Book class BookSerializer(serializers.ModelSerializer): class Meta: model = Book fields = '__all__' ``` 4. 在api中创建views.py文件,定义视图: ``` from rest_framework import viewsets from .models import Book from .serializers import BookSerializer class BookViewSet(viewsets.ModelViewSet): queryset = Book.objects.all() serializer_class = BookSerializer ``` 5. 在backend中配置api的url: ``` from django.urls import path, include from rest_framework import routers from api.views import BookViewSet router = routers.DefaultRouter() router.register(r'books', BookViewSet) urlpatterns = [ path('api/', include(router.urls)), ] ``` 6. 在frontend中创建vue3项目,命名为frontend,安装element-ui和axios: ``` yarn add element-plus axios ``` 7. 在frontend中创建src/api/index.js文件,定义请求: ``` import axios from 'axios' const instance = axios.create({ baseURL: 'http://localhost:8000/api/', timeout: 1000, headers: {'Content-Type': 'application/json'} }) export default { getBooks() { return instance.get('books/') }, createBook(data) { return instance.post('books/', data) }, updateBook(id, data) { return instance.put(`books/${id}/`, data) }, deleteBook(id) { return instance.delete(`books/${id}/`) } } ``` 8. 在frontend中创建src/views/BookList.vue文件,定义书籍列表组件: ``` <template> <el-table :data="books" style="width: 100%"> <el-table-column prop="title" label="书名"></el-table-column> <el-table-column prop="author" label="作者"></el-table-column> <el-table-column prop="price" label="价格"></el-table-column> <el-table-column label="操作"> <template #default="{row}"> <el-button type="primary" size="small" @click="handleEdit(row)">编辑</el-button> <el-button type="danger" size="small" @click="handleDelete(row)">删除</el-button> </template> </el-table-column> </el-table> </template> <script> import api from '@/api' export default { name: 'BookList', data() { return { books: [] } }, async created() { const res = await api.getBooks() this.books = res.data }, methods: { handleEdit(row) { this.$router.push({name: 'BookEdit', params: {id: row.id}}) }, async handleDelete(row) { try { await api.deleteBook(row.id) this.books = this.books.filter(book => book.id !== row.id) this.$message.success('删除成功') } catch (error) { this.$message.error('删除失败') } } } } </script> ``` 9. 在frontend中创建src/views/BookCreate.vue文件,定义创建书籍组件: ``` <template> <el-form :model="form" :rules="rules" ref="form" label-width="80px"> <el-form-item label="书名" prop="title"> <el-input v-model="form.title"></el-input> </el-form-item> <el-form-item label="作者" prop="author"> <el-input v-model="form.author"></el-input> </el-form-item> <el-form-item label="价格" prop="price"> <el-input v-model="form.price"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="handleSubmit">创建</el-button> </el-form-item> </el-form> </template> <script> import api from '@/api' export default { name: 'BookCreate', data() { return { form: { title: '', author: '', price: '' }, rules: { title: [ {required: true, message: '请输入书名', trigger: 'blur'} ], author: [ {required: true, message: '请输入作者', trigger: 'blur'} ], price: [ {required: true, message: '请输入价格', trigger: 'blur'}, {type: 'number', message: '价格必须为数字', trigger: 'blur'} ] } } }, methods: { async handleSubmit() { try { await this.$refs.form.validate() await api.createBook(this.form) this.$message.success('创建成功') this.$router.push({name: 'BookList'}) } catch (error) { this.$message.error('创建失败') } } } } </script> ``` 10. 在frontend中创建src/views/BookEdit.vue文件,定义编辑书籍组件: ``` <template> <el-form :model="form" :rules="rules" ref="form" label-width="80px"> <el-form-item label="书名" prop="title"> <el-input v-model="form.title"></el-input> </el-form-item> <el-form-item label="作者" prop="author"> <el-input v-model="form.author"></el-input> </el-form-item> <el-form-item label="价格" prop="price"> <el-input v-model="form.price"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="handleSubmit">保存</el-button> </el-form-item> </el-form> </template> <script> import api from '@/api' export default { name: 'BookEdit', data() { return { id: this.$route.params.id, form: { title: '', author: '', price: '' }, rules: { title: [ {required: true, message: '请输入书名', trigger: 'blur'} ], author: [ {required: true, message: '请输入作者', trigger: 'blur'} ], price: [ {required: true, message: '请输入价格', trigger: 'blur'}, {type: 'number', message: '价格必须为数字', trigger: 'blur'} ] } } }, async created() { const res = await api.getBooks() const book = res.data.find(book => book.id == this.id) this.form = book }, methods: { async handleSubmit() { try { await this.$refs.form.validate() await api.updateBook(this.id, this.form) this.$message.success('保存成功') this.$router.push({name: 'BookList'}) } catch (error) { this.$message.error('保存失败') } } } } </script> ``` 11. 在frontend中创建src/router/index.js文件,定义路由: ``` import {createRouter, createWebHistory} from 'vue-router' import BookList from '@/views/BookList.vue' import BookCreate from '@/views/BookCreate.vue' import BookEdit from '@/views/BookEdit.vue' const routes = [ {path: '/', name: 'BookList', component: BookList}, {path: '/create', name: 'BookCreate', component: BookCreate}, {path: '/edit/:id', name: 'BookEdit', component: BookEdit}, ] const router = createRouter({ history: createWebHistory(), routes }) export default router ``` 12. 在frontend中创建src/main.js文件,初始化vue3和element-plus: ``` import {createApp} from 'vue' import App from './App.vue' import router from './router' import ElementPlus from 'element-plus' import 'element-plus/lib/theme-chalk/index.css' createApp(App).use(router).use(ElementPlus).mount('#app') ``` 至此,我们成功地实现了一个简单的CRUD功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值