This episode is for members only

Sign up to access "Build a Trello Clone With Livewire" right now.

Get started
Already a member? Sign in to continue
Playing
11. Moving cards between columns

Transcript

00:00
To sort cards within our columns, we could take the same approach as we took with the column functionality.
00:07
We could define these as sortable when we iterate through them and then attach the handler and ID to each one. Now, unfortunately, it's not quite going to work like this because we also want these cards to be movable between columns. What we're going to need to do is set up a sortable group for these particular items so we can move them across columns and then we're going to need to do a little bit more work to update the cards,
00:34
but also sort the cards as well. OK, let's take a look at how this works by defining out a group, first of all. So let's say wire sortable and group and let's give a new method. So this is going to be a completely new method within our live wire component, which handles when a card is moved.
00:52
I'm just going to call this moved for now, but I might think of a better name later. OK, so let's go over to our board show and we're going to create out a new moved method in here. Remember that moving cards between columns is going to need different functionality because we're going to need to update the ID of the column that the card is in. OK, so let's head over to our column and let's see what we need to do around each of these cards.
01:20
To start with, let's go ahead and just pull all of this down so we can start to add these in. And we want to use wire sortable group and we want the group item group to be a column ID. So if we think of it this way, each of our columns is a group and we want to be able to move these items inside of here between each of the groups. Now, what we need to do is a similar thing to what we did with our columns and wrap this in a container which has the handler directly on this div.
01:54
So once again, we're going to set the wire key in here so live wire can update everything. And this is just going to be the card ID. And let's just make sure we specify that properly that we missed out before that hydration error. OK, so inside each of these now we're going to have wire sortable and the group.
02:12
But the item now is going to be the card ID. So basically, this means that we can move the item here with an ID of one to whatever the card is inside of the sortable group. This is the group. This is the group item.
02:28
And as we saw over on board show, when something has moved between groups, we invoke this move method. OK, so we're going to go over to our board show and let's bring in the array of items that we're going to get in here. And let's just die dump on them items and see what happens. OK, so I'm going to grab one of these.
02:48
And as you can see at the moment, I can't actually grab one of the cards. The reason for this is that over in our card, we need to specifically set a handler here. So let's say wire sortable group and handle. And that will mean that this thing can now be moved between each ones.
03:07
And it won't try and move the entire group when we are using this handler. OK, let's go back over and try this out. And now, as you can see, I can start to move these. I can still move the columns, but I can also move these cards.
03:18
And now because we've set this up as a group, you can see that it can be dropped into another column. OK, let's go ahead and just sort the cards within this column, first of all, and then we'll look at moving them between columns. So I'm going to move this one up to the top. And as you can see, that method is called.
03:35
But this is now the handle or the move method. And you can see that we've got the group here and then inside we've got each of these items. So a nice structure. We just can need to manipulate this a little bit to find out where things need to fit in.
03:50
So as you can see, we've got one, three and two. That makes sense. That's been ordered properly. One moving to the top means one, three and two, which we saw there.
04:00
Now, if I move card one, which is an ID of one over to column two, which is an ID of two. Let's take a look. So we've got now only two cards in here. See both of them.
04:11
Two now has an additional item and that's one. Great. So everything is working and we're getting the data we need to actually sort this. Now all that's left to do is implement the moved method.
31 episodes2 hrs 27 mins

Overview

Get ready to master drag and drop sorting in Livewire, by building a Trello clone.

We’ll start out by building the interface completely from scratch, then add the ability to sort columns and cards, including moving cards around columns. As we sort everything, we’ll keep the database perfectly updated with the new order.

Our Trello clone will also allow us to edit column titles inline, edit cards and add notes, archive cards and columns, and put them back on the board.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!