需求实战一

效果展示

代码展示

<template>
<div> <a-table :dataSource="dataSource" :columns="columns" /> </div>
</template> <script setup lang="ts"> const dataSource= ref([
{
key: '1',
name: '胡彦斌',
age: 32,
address: '西湖区湖底公园1号',
},
{
key: '2',
name: '胡彦祖',
age: 42,
address: '西湖区湖底公园1号',
},
]) const columns=ref([
{
title: '姓名',
dataIndex: 'name',
key: 'name',
},
{
title: '年龄',
dataIndex: 'age',
key: 'age',
},
{
title: '住址',
dataIndex: 'address',
key: 'address',
},
])
</script>

代码解读

这段代码是一个使用Vue 3的组件,用于展示一个表格。代码中使用了Vue的模板语法,通过<template>标签定义了组件的模板部分。在模板中,使用了<a-table>标签来展示表格,通过:dataSource和:columns属性绑定了数据源和列的配置。
 
在<script setup>标签中,使用了Vue 3的新特性<script setup>,它可以简化组件的写法。在这里,定义了两个响应式变量dataSource和columns,它们分别存储了表格的数据源和列的配置。ref函数用于将普通变量转换为响应式变量。
 
dataSource是一个数组,存储了表格的数据。每个数据对象都有key、name、age和address属性,分别表示数据的唯一标识、姓名、年龄和住址。
 
columns也是一个数组,存储了表格的列的配置。每个列配置对象都有title、dataIndex和key属性,分别表示列的标题、数据索引和唯一标识。
 
这段代码的作用是展示一个简单的表格,表格的数据和列的配置都是通过响应式变量来管理的,可以方便地进行数据的更新和操作。

需求实战二

效果展示

代码展示

<template>

    <div>
<a-button type="primary" @click="addRow">新增</a-button>
<br>
<br>
<br>
<a-table :dataSource="dataSource" :columns="columns" bordered> <template #operation="{ record }">
<a-button type="primary" @click="editRow(record)">编辑</a-button>
&nbsp
&nbsp
&nbsp
<a-button type="danger" @click="deleteRow(record)">删除</a-button>
</template>
<template #title>
<div style="text-align: center;">个人信息表格</div>
</template>
</a-table>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
const dataSource = ref([
{
key: '1',
name: '张三',
age: '32',
address: '天安门广场',
},
{
key: '2',
name: '李四',
age: '42',
address: '紫禁城',
},
])
const columns = ref([
{
title: '姓名',
dataIndex: 'name',
key: 'name',
},
{
title: '年龄',
dataIndex: 'age',
key: 'age',
},
{
title: '住址',
dataIndex: 'address',
key: 'address',
},
{
title: '操作',
dataIndex: 'operation',
slots: { customRender: 'operation' },
},
])
const editRow = (record: any) => {
const index = dataSource.value.findIndex((item: any) => item.key === record.key);
if (index !== -1) {
const newName = prompt('Enter new name:');
const newAge = prompt('Enter new age:');
const newAddress = prompt('Enter new address:');
if (newName && newAge && newAddress) {
dataSource.value[index].name = newName;
dataSource.value[index].age = newAge;
dataSource.value[index].address = newAddress;
}
}
}
const deleteRow = (record: any) => {
const index = dataSource.value.findIndex((item) => item.key === record.key)
if (index !== -1) {
dataSource.value.splice(index, 1)
}
}
const addRow = () => {
const newName = prompt('Enter name:');
const newAge = prompt('Enter age:');
const newAddress = prompt('Enter address:');
if (newName && newAge && newAddress) {
const newKey = String(dataSource.value.length + 1);
dataSource.value.push({
key: newKey,
name: newName,
age: newAge,
address: newAddress,
});
}
}
</script>

代码解读

这段代码是一个基于Vue框架的前端页面,用于展示一个个人信息表格,并提供了新增、编辑和删除功能。
 
在模板部分,使用了Ant Design Vue组件库中的<a-table>和<a-button>组件来构建页面。<a-table>组件用于展示表格数据,通过:dataSource和:columns属性绑定数据源和列配置。在<a-table>组件内部,使用了两个<template>标签来自定义表格的标题和操作列。
 
在脚本部分,使用了Vue 3中的<script setup>语法,引入了ref函数来创建响应式数据。dataSource和columns分别是表格的数据源和列配置,通过ref函数将其转换为响应式数据。editRow、deleteRow和addRow是处理编辑、删除和新增操作的函数。
 
editRow函数用于编辑表格行数据,首先根据传入的record参数找到对应的数据索引,然后通过prompt函数弹出输入框,获取新的姓名、年龄和地址。如果输入框有值,则更新对应数据的属性值。
 
deleteRow函数用于删除表格行数据,同样根据传入的record参数找到对应的数据索引,然后使用splice方法从数据源中删除该数据。
 
addRow函数用于新增表格行数据,同样使用prompt函数弹出输入框,获取新的姓名、年龄和地址。如果输入框有值,则生成一个新的key值,并将新数据添加到数据源中。
 
整体来说,这段代码实现了一个简单的个人信息表格,并提供了编辑、删除和新增功能。

需求实战三

效果展示

代码展示

<template>
<ARow>
<a-button type="primary" @click="handleAdd" >新增</a-button>
</ARow>
<br>
<br>
<br>
<ARow>
<a-table :columns="columns" :data-source="dataSource" bordered>
<template v-for="col in ['name', 'age', 'address']" #[col]="{ text, record }" :key="col">
<div>
<a-input
v-if="editableData[record.key]"
v-model:value="editableData[record.key][col]"
style="margin: -5px 0"
/>
<template v-else>
{{ text }}
</template>
</div>
</template>
<template #operation="{ record }">
<div class="editable-row-operations">
<span v-if="editableData[record.key]">
<div style="display: flex; justify-content: space-between;"> <a-button type="primary" @click="save(record.key)">保存</a-button>
&nbsp&nbsp&nbsp <a-button type="danger" @click="cancel(record.key)">
取消
</a-button>
</div> </span>
<span v-else>
<div style="display: flex; justify-content: space-between;">
<a-button type="primary" @click="edit(record.key)">编辑</a-button>
&nbsp&nbsp&nbsp
<a-button type="danger" @click="onDelete(record.key)">删除</a-button>
</div>
</span>
</div>
</template>
</a-table>
</ARow>
</template>
<script setup lang="ts">
import { cloneDeep } from 'lodash-es';
import { reactive, UnwrapRef } from 'vue'; const columns = [
{
title: 'name',
dataIndex: 'name', slots: { customRender: 'name' },
},
{
title: 'age',
dataIndex: 'age', slots: { customRender: 'age' },
},
{
title: 'address',
dataIndex: 'address', slots: { customRender: 'address' },
},
{
title: 'operation',
dataIndex: 'operation',
slots: { customRender: 'operation' },
},
];
interface DataItem {
key: string;
name: string;
age: number;
address: string;
}
const data: DataItem[] = [];
for (let i = 0; i < 100; i++) {
data.push({
key: i.toString(),
name: `Edrward ${i}`,
age: 32,
address: `London Park no. ${i}`,
});
} const handleAdd = () => {
const newData = {
key: `${count.value}`,
name: `Edward King ${count.value}`,
age: 32,
address: `London, Park Lane no. ${count.value}`,
};
dataSource.value.push(newData);
};
const count = computed(() => dataSource.value.length + 1); const dataSource = ref(data);
const editableData: UnwrapRef<Record<string, DataItem>> = reactive({}); const edit = (key: string) => {
editableData[key] = cloneDeep(dataSource.value.filter(item => key === item.key)[0]);
};
const save = (key: string) => {
Object.assign(dataSource.value.filter(item => key === item.key)[0], editableData[key]);
delete editableData[key];
};
const cancel = (key: string) => {
delete editableData[key];
}; const onDelete = (key: string) => {
dataSource.value = dataSource.value.filter(item => item.key !== key);
};
</script>

代码解读

这段代码是一个包含表格和编辑功能的Vue组件。主要功能是展示一个包含姓名、年龄、地址等信息的表格,并提供编辑、保存、取消和删除功能。
 
在模板部分,使用了Ant Design Vue组件库的<a-table>和<a-button>组件来实现表格和按钮的展示。表格的列定义在columns数组中,每一列都有一个对应的dataIndex属性指定数据源中的字段名,以及一个slots属性指定自定义渲染的插槽名。
 
在数据部分,定义了一个DataItem接口表示表格中的每一行数据,以及一个data数组用于存储所有的数据。通过循环生成了100条测试数据,并将其赋值给dataSource变量。
 
在脚本部分,使用了Vue 3的<script setup>语法来定义组件的逻辑部分。首先引入了lodash-es库中的cloneDeep函数和Vue的reactive和ref函数。然后定义了一些响应式的变量,包括dataSource用于存储表格数据,editableData用于存储正在编辑的数据,以及count用于生成新数据的键值。
 
接下来定义了一些处理函数,包括handleAdd用于新增数据,edit用于编辑数据,save用于保存编辑后的数据,cancel用于取消编辑,以及onDelete用于删除数据。这些函数通过操作dataSource和editableData来实现相应的功能。
 
最后,将定义的变量和函数导出供模板部分使用。

【技术实战】Vue技术实战【二】的更多相关文章

  1. Vue应用框架整合与实战--Vue技术生态圈篇

    实用框架以及工具 UI组件 开发框架 实用库 服务端 辅助工具 应用实例 Demo示例 UI组件 Element-UI ★13489 - 饿了么出品的Vue2的web UI工具套件 Vux ★8133 ...

  2. 深度解析SDN——利益、战略、技术、实践(实战派专家力作,业内众多专家推荐)

    深度解析SDN——利益.战略.技术.实践(实战派专家力作,业内众多专家推荐) 张卫峰 编   ISBN 978-7-121-21821-7 2013年11月出版 定价:59.00元 232页 16开 ...

  3. 超级干货:动态防御WAF技术原理及编程实战!

    本文带给大家的内容是动态防御WAF的技术原理及编程实战. 将通过介绍ShareWAF的核心技术点,向大家展示动态防御的优势.实现思路,并以编程实战的方式向大家展示如何在WAF产品开发过程中应用动态防御 ...

  4. 简读《ASP.NET Core技术内幕与项目实战》之3:配置

    特别说明:1.本系列内容主要基于杨中科老师的书籍<ASP.NET Core技术内幕与项目实战>及配套的B站视频视频教程,同时会增加极少部分的小知识点2.本系列教程主要目的是提炼知识点,追求 ...

  5. 快读《ASP.NET Core技术内幕与项目实战》EFCore2.5:集合查询原理揭秘(IQueryable和IEnumerable)

    本节内容,涉及4.6(P116-P130).主要NuGet包:如前述章节 一.LINQ和EFCore的集合查询扩展方法的区别 1.LINQ和EFCore中的集合查询扩展方法,虽然命名和使用完全一样,都 ...

  6. 快读《ASP.NET Core技术内幕与项目实战》WebApi3.1:WebApi最佳实践

    本节内容,涉及到6.1-6.6(P155-182),以WebApi说明为主.主要NuGet包:无 一.创建WebApi的最佳实践,综合了RPC和Restful两种风格的特点 1 //定义Person类 ...

  7. 《ASP.NET Core技术内幕与项目实战》精简集-目录

    本系列是杨中科2022年最新作品<ASP.NET Core技术内幕与项目实战>及B站配套视频(强插点赞)的精简集,是一个读书笔记.总结和提炼了主要知识点,遵守代码优先原则,以利于快速复习和 ...

  8. 躬身入局,干货分享,2023年春招后端技术岗(Python)面试实战教程,Offer今始为君发

    早春二月,研发倍忙,杂花生树,群鸥竟飞.为什么?因为春季招聘,无论是应届生,还是职场老鸟,都在摩拳擦掌,秣马厉兵,准备在面试场上一较身手,既分高下,也决Offer,本次我们打响春招第一炮,躬身入局,让 ...

  9. 基于TC技术的网络流量控制实战

    本文转载在:http://server.it168.com/a2010/0426/878/000000878406.shtml 基于TC技术的网络流量控制实战 650) this.width=650; ...

  10. 从开发一款基于Vue技术栈的全栈热重载生产环境脚手架,我学到了什么

    浏览文章前 这一期,我分享给大家三点看源码的小技巧,这也是从别的大佬那总结的. 被反复使用的代码 这样的代码是一个软件的重点函数,一个大神的写法有很多精华值得学习. 穿越时间的代码 如果一段代码10年 ...

随机推荐

  1. 一个可用于生产项目 基于 .NET 6 自研ORM

    Fast Framework 作者 Mr-zhong 代码改变世界.... 一.前言 Fast Framework 基于NET6.0 封装的轻量级 ORM 框架 支持多种数据库 SqlServer O ...

  2. 30-externals(拒绝某些包被打包进来)

    const { resolve } = require('path') const HtmlWebpackPlugin = require('html-webpack-plugin') module. ...

  3. Win Pycharm + Appium + 真机 实现APP自动化

    前言: 测试的APP:亿通行 手机型号:OPPO 系统:安卓10 一.安装Python3.x 1.直接登录Python官网https://www.python.org/,下载最新版本即可 2.安装Py ...

  4. 【Docker】安装及部署

    一.Ubuntu使用apt安装Docker 官方安装文档:https://docs.docker.com/engine/install/ubuntu/ 1.准备安装环境 [root@Docker-Ub ...

  5. Maven工程开发

    Maven工程开发 编写pom.xml配置文件 <properties></properties>定义项目的基本信息 groupId 反向域名,定义项目组名 artifactI ...

  6. 苦苦搞了半个通宵才搞定的直接使用Sliverlight将文件PUT到阿里云OSS

    为了公司的项目,小的我各种折腾啊,不过高兴的是实现了Silverlight直接提交至阿里云OSS的文件上传,文件上传再也不用通过服务器中转了. 研究SDK发现还有个Node-oss.js,但还没进行测 ...

  7. 解决pod健康检查问题

    解决pod健康检查问题 引自:Solving the mystery of pods health checks failures in Kubernetes.原文中的某些描述并不清晰,本文作了调整. ...

  8. Vulhub activewq_漏洞复现——源码分析

    Vulhub activewq_漏洞复现--源码分析 漏洞简介 Apache ActiveMQ是由美国阿帕奇(Apache)软件基金会开发的开源消息中间件,支持Java消息服务.集群.Spring框架 ...

  9. Mysql列转行, group_concat的使用

    开始业务的查询的时候碰到一个sql的查询语句问题,主要是 group_concat 之前没用过,现在记录一下怎么用 group_concat 用法, 可以将相同的行组合起来 group_concat( ...

  10. HTB靶场之-inject

    准备: 攻击机:虚拟机kali. 靶机:Inject,htb网站:https://www.hackthebox.com/,靶机地址:https://app.hackthebox.com/machine ...