当前位置:   article > 正文

HarmonyOs学习笔记_foreach 语法 harmonyos

foreach 语法 harmonyos

hml方面

使用到一个switch组件,书写格式如下:

 <switch showtext="true" checked="{{$item.status}}"
                texton="完成" 
                textoff="待办" 
                class="switch"
                @change="switchChange($idx)"
                >
        </switch>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

css方面

没啥说的,略过

js方面

构建一个函数用来计算剩余多少事情

computed:{
        todoCount(){
            let num =0;
            this.todolist.forEach(element=>{
                if(!element.status){
                    num++
                }
            });
            return num
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

然后给每个按钮设定时间就好了

数据方面:

给代办事项加上内容

export default[
    {
        info:'给人打电话',
        status:true,
    },
    {
        info:'输出工作计划',
        status:false,
    },
    {
        info:'准备7点的会议',
        status:true,
    },
    {
        info:'整理资料',
        status:false,
    },
    {
        info:'上分',
        status:true,
    }
]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

然后在js文件中引入即可

import todolist from "../../common/datas/todolist.js"
  • 1

具体实现效果

在这里插入图片描述

具体代码如下:

hml

<div class="container">
    <text class="title">待办事项</text>
    <div class="item" for="{{todolist}}">
        <text class="todo">{{$item.info}}
        </text>
        <switch showtext="true" checked="{{$item.status}}"
                texton="完成" 
                textoff="待办" 
                class="switch"
                @change="switchChange($idx)"
                >
        </switch>
        <button class="remove" @click="remove($idx)">
            删除
        </button>
    </div>
    <div class="info">
        <text class="info-text">您还有</text>
        <text class="info-num">{{todoCount}}</text>
        <text class="info-text">件事情待办</text>
    </div>
    <div class="add-todo">
     <input class="plan-input" type="text"></input>
        <button class="plan-btn" onclick="assTodo" @click="addTodo">添加待办</button>
    </div>
</div>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

css

.container {
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    padding-bottom: 100px;
}

.title {
    font-size: 25px;
    margin-top:20px;
    margin-bottom: 20px;
    color:#000000;
    opacity: 0.9;
    font-size:28px;
}
.item{
    width:325px;
    padding:10px 0;
    flex-direction:row;
    align-items:center;
    justify-content: space-around;
    border-bottom: 1px solid #eee;
}
.todo{
    color:#000;
    width:180px;
    font-size:18px;
}
.switch{
    font-size:12px;
    texton-color: green;
    textoff-color: red;
    text-padding: 5px;
    width:100px;
    heigh:24px;
    allow-scale: false;
}
.remove{
    font-size:12px;
    margin-left: 10px;
    width:68px;
    height: 22px;
    color:#fff;
    background-color: red;
}
.info{
    width:100%;
    margin-top: 10px;
    justify-content: center;
}
.info-text{
    font-size:18px;
    color:#AD7A1B;
}
.info-num{
    color:orangered;
    margin-left: 10px;
    margin-right: 10px;
}
.add-todo{
    position: fixed;
    left: 0;
    bottom:0;
    width:100%;
    height:60px;
    flex-direction: row;
    justify-content: space-around;
    align-items:center;
    background-color: #ddd;
}
.plan-input{
    width:240px;
    height:35px;
    background-color: #fff;
}
.plan-btn{
    width:90px;
    height: 30px;
    font-size:15px;
}



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83

js

import todolist from "../../common/datas/todolist.js"
export default {
    data: {
       todolist,
    },
   remove(index){
       console.log(index)
       this.todolist.splice(index,)
   },
    switchChange(index){
        this.todolist[index].status = !this.todolist[index].status
    },
    addTodo(){
        this.todolist.push({
            info:'IDE工具无法监听键盘输入',
            status:false
        })
    },
    computed:{
        todoCount(){
            let num =0;
            this.todolist.forEach(element=>{
                if(!element.status){
                    num++
                }
            });
            return num
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

js中引入的todoList.js文件

export default[
    {
        info:'给人打电话',
        status:true,
    },
    {
        info:'输出工作计划',
        status:false,
    },
    {
        info:'准备7点的会议',
        status:true,
    },
    {
        info:'整理资料',
        status:false,
    },
    {
        info:'上分',
        status:true,
    }
]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

(引入时注意路径即可)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/279076?site
推荐阅读
相关标签
  

闽ICP备14008679号