官术网_书友最值得收藏!

  • JavaScript by Example
  • Dani Akash S
  • 236字
  • 2021-07-02 18:39:07

Loading the tasks from data

The first thing we want to do in our application is to load the tasks dynamically from a set of data. Let's declare a class variable that contains the data for tasks along with methods needed to pre-populate the tasks. ES6 does not provide a direct way to declare class variables. We need to declare variables using the constructor. We also need a function to load tasks into the HTML elements. So, we'll create a loadTasks() method:

class ToDoClass {
constructor() {
this.tasks = [
{task: 'Go to Dentist', isComplete: false},
{task: 'Do Gardening', isComplete: true},
{task: 'Renew Library Account', isComplete: false},
];
this.loadTasks();
}

loadTasks() {
}
}

The tasks variable is declared inside the constructor as this.tasks, which means the tasks variable belongs to this (ToDoClass). The variable is an array of objects that contain the task details and its completion status. The second task is set to be completed. Now, we need to generate an HTML code for the data. We'll reuse the code of the <li> element from the HTML to generate a task dynamically:

 <li class="list-group-item checkbox">
<div class="row">
<div class="col-md-1 col-xs-1 col-lg-1 col-sm-1 checkbox">
<label><input type="checkbox" value="" class="" checked></label>
</div>
<div class="col-md-10 col-xs-10 col-lg-10 col-sm-10 task-text complete">
First item
</div>
<div class="col-md-1 col-xs-1 col-lg-1 col-sm-1 delete-icon-area">
<a class="" href="/"><i class="delete-icon glyphicon glyphicon-trash"></i></a>
</div>
</div>
</li>
In JavaScript, an instance of a class is called the class object or simply object. The class objects are structured similarly to JSON objects in key-value pairs. The functions associated with a class object are called its methods and the variables/values associated with a class object are called its properties.
主站蜘蛛池模板: 昌吉市| 平舆县| 名山县| 清流县| 邯郸市| 万宁市| 莱阳市| 武功县| 沈阳市| 昆明市| 游戏| 十堰市| 都安| 明水县| 柳江县| 东光县| 南平市| 南陵县| 桓台县| 资兴市| 宕昌县| 宁夏| 水富县| 昌平区| 朝阳县| 高阳县| 阿巴嘎旗| 新邵县| 库尔勒市| 故城县| 左云县| 自治县| 会东县| 平原县| 清徐县| 禹城市| 科尔| 汉沽区| 浠水县| 固原市| 井冈山市|