当前位置:网站首页>138-查询案例-涉及知识点:forEach遍历&computed计算属性&v-for循环
138-查询案例-涉及知识点:forEach遍历&computed计算属性&v-for循环
2022-07-23 19:38:00 【longfei815】
138-查询案例-涉及知识点:forEach遍历&computed计算属性&v-for循环
效果如下:

项目目录:
Home.vue代码如下:
<template>
<div class="home">
<HelloWorld/>
</div>
</template>
<script>
// @ is an alias to /src
import HelloWorld from '@/components/Comp1.vue'
export default {
name: 'Home',
components: {
HelloWorld
},
created(){
console.log(this.$route.meta.isKeep);
},
// 两个特殊的生命周期函数: 因为被keep-alive包着,才有这两个函数
activated(){
console.log("组件被激活的时候执行,被切换回来的时候,被展示在页面上的时候");
},
deactivated(){
console.log("组件被取消激活的时候执行,被切换出去的时候,消失在页面上的时候");
}
}
</script>
关键组件Comp1.vue代码如下:
<template>
<!-- 作业讲解-点击查询案例 -->
<div>
<div class="top">
<span>名称</span>
<input type="text" v-model.trim="txtVal" @keyup="hdKeyup">
<!-- .trim作用是:前后去空格 -->
<button @click="search">查询</button>
</div>
<table>
<tr>
<th v-for="item in titles" :key="item">{
{item}}</th>
</tr>
<tr v-for="item,index in info" :key="item.id" v-show="item.is_show">
<td>{
{item.id}}</td>
<td>{
{item.title}}</td>
<td>{
{item.price_info.toFixed(2)}}</td>
<td>
<button @click="del(index)">删除</button>
</td>
</tr>
<tr>
<td colspan=5>总价为:{
{total}}</td>
</tr>
</table>
</div>
</template>
<script>
export default {
data () {
return {
txtVal:"",
titles:["ID","主标题","起步价格","操作"],
info:[{
"id": 287,
"title": "严选新式样板间",
"price_info": 29.9,
"is_show": true
}, {
"id": 286,
"title": "无“油”无虑的甜蜜酥脆",
"price_info": 45,
"is_show": true
}, {
"id": 283,
"title": "孩子成长中少不了的一双鞋",
"price_info": 78,
"is_show": true
}, {
"id": 282,
"title": "成就一室笋香1",
"price_info": 121,
"is_show": true
}, {
"id": 281,
"title": "条纹新风尚",
"price_info": 29,
"is_show": true
}, {
"id": 277,
"title": "治愈生活的满怀柔软",
"price_info": 66.78,
"is_show": true
}, {
"id": 274,
"title": "没有软木拖,怎么过夏天",
"price_info": 50.99,
"is_show": true
}, {
"id": 272,
"title": "料理也要精细简单",
"price_info": 69,
"is_show": true
}, {
"id": 271,
"title": "选式新懒人",
"price_info": 15.3,
"is_show": true
}, {
"id": 268,
"title": "米饭好吃的秘诀:会呼吸的锅",
"price_info": 20.1,
"is_show": true
}]
}
},
methods:{
hdKeyup(){
// 如果用户输入的值为空, 就把所有的数据展示出来(每一项的is_show设置为true)
if(!this.txtVal){
this.info.forEach(item=>{
item.is_show=true
})
}
},
search(){
if(!this.txtVal){
alert("请输入要查询的内容!");
return
}
// 查询的逻辑: 利用v-show的显示和隐藏.把title中包含用户输入的内容的这一项的is_show设置为true,否则设置为false
// 因为每一项的is_show都要重新设置,所以遍历info数组
this.info.forEach(item=>{
// 看看如果这一项item的title包含用户输入的信息this.txtVal, 如果包含item.is_show=true
// if(item.title.includes(this.txtVal)){
// item.is_show=true
// }else{
// item.is_show=false
// }
item.is_show=item.title.includes(this.txtVal)
})
},
del(i){
this.info.splice(i,1)
}
},
computed:{
total(){
let ret = this.info.reduce((pre,cur)=>{
// 判断这一项的is_show,如果为真就把价格加进去,否则就不加进去(直接返回pre)
if(cur.is_show){
return pre+cur.price_info
}else{
return pre
}
// return cur.is_show?(pre+cur.price_info):pre
},0)
return "¥"+ret.toFixed(2)+"元"
}
}
}
</script>
<style lang = "less" scoped>
*{margin: 0;padding: 0;}
body{font-size: 14px; color:#666; padding-left: 50px;padding-top: 30px;}
input,button{outline-style: none;}
table{border: 1px solid #ddd;border-collapse: collapse; }
td,th{border: 1px solid #ddd; width: 130px; height: 30px;text-align: left; padding: 10px;}
.top{
display: flex;
margin-bottom: 10px;
}
.top>*{
margin-right: 20px;
}
.top span{
line-height: 40px;
}
.top input{
border:1px solid #ccc;
border-radius:4px;
padding-left: 8px;
color:#666;
}
button{
border:0;
padding:10px 15px; background-color: skyblue;
color:#fff;
border-radius:4px;
}
</style>边栏推荐
- 【ASP.NET Core】选项模式的相关接口
- Compiler llvm MLIR introductions llvm backend instruction
- Win11小组件怎么添加待办事项?Win11添加待办事项小组件的方法
- Leetcode 228. 汇总区间(可以,已解决)
- Leetcode 209. 长度最小的子数组
- How to solve the problem that the solid state disk cannot be found when installing win11?
- 梅科爾工作室-小熊派開發筆記2
- Failure after reinstalling the system (error: Reboot and select proper boot device or insert boot media in selected boot device)
- 搭建自己的目标检测环境,模型配置,数据配置 MMdetection
- Complex data processing of multi subsystem and multi business modules -- project development practice based on instruction set Internet of things operating system
猜你喜欢

关于网段CIDR的笔记

Applet avatar group style

Drools(1):Drools简介

2022DASCTF MAY

Energy principle and variational method note 15: solution of differential element method

Parity rearrangement of Bm14 linked list

Compiler llvm MLIR introductions llvm backend instruction

千呼万唤,5G双卡双通到底有多重要?

How to reset the computer system? The method is actually very simple

【Unity项目实践】关卡解锁
随机推荐
Leetcode 238. product of arrays other than itself
ODrive应用 #6 编码器
数组——27. 移除元素
数组——11. 盛最多水的容器
2022/7/21 training summary
2022DASCTF MAY
Relevant interfaces of [asp.net core] option mode
2022上半年中国十大收缩行业
【ASP.NET Core】选项模式的相关接口
Failure after reinstalling the system (error: Reboot and select proper boot device or insert boot media in selected boot device)
设置ASP.NET MVC站点默认页为指定页面
17. Life cycle
Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 4
[unity project practice] level unlocking
Win11没有Word文档怎么办?Win11没有Word文档解决教程
Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 5
数仓4.0笔记——数仓环境搭建—— DataGrip准备和数据准备
数组——59. 螺旋矩阵 II
20.ref与props
Baidu map data visualization