SFDC Stop - Always the latest about Salesforce


Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.


Telegram logo   Join our Telegram Channel

Thursday, 27 August 2020

Salesforce LWC Tutorial Part 3 | ToDo App Project | Removing elements from ToDo List

Hello Trailblazers,

Welcome to the third tutorial in LWC Tutorial Series where we're building a ToDo App Project. We're focusing on the concept of Learning By Doing in this tutorial series. In this tutorial, we'll be modifying the JavaScript and HTML code of our ToDo Application so that we're able to remove a ToDo Task from the list by clicking on the delete button icon. The final result will be as follows:-


So, let's continue building the above application. This tutorial is published on SFDC Stop YouTube Channel. So, if you want to learn in detail, have a look at the tutorial video down below. Otherwise, you can scroll down to have a look at the code gist with explanation.

Tutorial Video


Code Gist with Explanation

I highly recommend you to have a look at the video to understand in detail. However, let's have a quick look at the code below to understand in short:-

HTML Snippet (todo.html)

Have a look at the code above, if you carefully see the delete button-icon, you'll notice that, I've added a name attribute and an onclick attribute. The name attribute consist of the id of current todo task which is being rendered in the list, specified by todoTask.Id. Using this name attribute we can identify which task is being deleted in the js and remove that task from the todo list array. The onclick attribute is binded with a js function named as:- deleteTaskFromList

Let's have a look at the changes in JavaScript code now.

JS Snippet (todo.js)

As you can see above, I added a new method named as:- deleteTaskFromList() which will be used to remove a task from the ToDo List. Whenever we're clicking on the delete button icon, we're getting the id of the task which we have to delete by using event.target.name and storing it in idToDelete variable. We're assigning the list of todo tasks to a local variable named as todoTasks that we're going to use in this method. We declared another variable named as todoTaskIndex which will be used to store the index of todo task that we have to delete.

After that we're iterating all the todo tasks using a for loop and we're storing the index of the todo task in the todoTaskIndex variable whose id is equal to the id stored in idToDelete variable.

Finally, we're removing the todo task from the list by calling the splice() method on the todoTasks array. The splice() method takes up the index of the element to remove from the array as the first parameter and the number of elements to remove from the array (starting from that index) as the second parameter.

In the method 2, which is commented in the above code, we're using the findIndex() method to find the index of the todo task that we need to remove from the list.

We've re-written the same code specified in method 2 in method 3 also, the only difference is that we've used the arrow function => there to reduce the code to one line. Please have a look at the video for a detailed explanation of those.

You can use any of the methods out of:- method 1, method 2 or method 3. All will do the same work i.e. removing the element from the todo list.

That's all for this tutorial. I hope you liked it. All the code used in this tutorial is available on GitHub and you can have a look at that by clicking here. Make sure to have a look at the video if you want to learn in detail and let me know your feedback in the comments down below.

Happy Trailblazing..!!

Sunday, 23 August 2020

Salesforce LWC Tutorial Part 2 | ToDo App Project | Data Binding and Adding elements to ToDo List

Hello Trailblazers,

Welcome to the second tutorial in LWC Tutorial Series where we're building a ToDo App Project. We're focusing on the concept of Learning By Doing in this tutorial series. In this tutorial, we'll be adding the JavaScript to our ToDo Application and update the HTML code as well so that it renders dynamically and we'll be moving one step forward to the below result at the end as we'll be able to add elements to todo list dynamically on click of the Add button.


So, let's continue building the above application. This tutorial is published on SFDC Stop YouTube Channel. So, if you want to learn in detail, have a look at the tutorial video down below. Otherwise, you can scroll down to have a look at the code gist with explanation.

Tutorial Video


Code Gist with Explanation

I highly recommend you to have a look at the video to understand in detail. However, let's have a quick look at the code below to understand in short:-

JS Snippet (todo.js)


As you can see above, I created an array of todo tasks with name todoTasks and initialized it as an empty array. along with the newTask variable which is an empty string initially. The array is annotated with @track annotation because we need to re-render the HTML whenever a new task is added to the list. The newTask variable will store the name of new todo task that we're going to add to the list.

After that we've defined an updateNewTask() method which is used to update the value of the new task variable as we type in the new task name in the input field. event.target.value will give us the value as we're going to bind this method to the onchange attribute of input field in HTML.

Finally, we have an addTaskToList() method which is used to add the task to the todo list. This method will create a new todo task object with an id and a name and push it to the todoTasks array, in the new object, the id of the todo task will be the current array length (i.e. number of tasks in the list) + 1 and the name of the task will be the value stored in newTask variable. After adding the new task to the list, we're setting up the newTask variable as blank again.

HTML Snippet (todo.html)


As you can see in the above code, I have removed all the list items under the unordered list and kept only a single list item which is surrounded by a template tag, that template tag is basically a for:each loop which is responsible to iterate the todoTasks array specified in js and in each iteration, the current element can be accessed by the todoTask variable which is specified as the value for for:item in the template. For each list item, I am keeping the todoTask id as the key.

Note:- The first HTML element under the template tag running a for loop must have a key element and that key should be unique.

So, in our case the key is specified by todoTask.id as we're setting up the id each time we're adding an element to the todo tasks list. At the place of task name, I have specified {todoTask.name} which will replace this expression with the name of current todo task as this list is being rendered.

If you carefully see the Enter New Task input field, I have binded the newTask javascript variable as the value of input field and I have also binded the updateNewTask() method in the onchange handler of the input field.

So, whenever we're going to write something in this input field, the updateNewTask method will be called automatically and it'll update the value of newTask variable which is being set as the value of input field.

Finally, we specified our addTaskToList method in the onclick handler of the Add button. So, that whenever we're going to click this button, our todo task will be added to the list.

That's all for this tutorial. I hope you liked it. All the code used in this tutorial is available on GitHub and you can have a look at that by clicking here. Make sure to have a look at the video if you want to learn in detail and let me know your feedback in the comments down below.

Happy Trailblazing..!!