Now, it’s time to begin the road map established in the last post. The first item on our list? Renaming. I decided to go with an ajax request here, to avoid having to refresh or needing a new page. I suspect that I will use a lot of ajax requests in the near future.
First, I added button with simple CSS style next to the recipe name.
And the CSS:
This created a simple button to use:
Then, I wrote a script to hide these buttons and replace them with an input field and a button that, when clicked, sent an ajax request to an as-yet uncreated route.
Now, when clicked, the button produced an input field and the necessary “Confirm” button:
No CSS styling here but we’ll fix that in a bit. Now I added the ajax code, which sends the new name to the still-uncreated route, and when successful, changes the name on the list name and returns the items to their original look.
Now I created the route, which just uses the database call here to rename the list and change the name. It sends the new name back, and I use that piece of data, rather than my input field on the client, to update the name. I do that to make sure the name in the database and the one on the client are the same.
Then I added a few quick CSS stylings to the input box and btn btn-secondary
classes to the button, to give them a bit of styling:
One final area of housekeeping before I checked this piece off the list: the list name changed in the title, but it did not change the list name in my navbar. This would change the next time the page was refreshed, but for the sake of completeness, I wanted to make it change when everything else did.
It was a simple solution. First, I added a dynamic id
attribute to the <a>
link for each list:
Then, changing the text was as simple as a single line of javascript:
There. Everything changes. While I would like to change the styling on the edit process a bit more, I’m going to classify that as part of my later “style overhaul” and continue with my general mantra of “if it functions, it’s good for now.”
Next Steps
- add the ability to check an item off the list
- add the ability to delete a list item
- add links to the recipes if made from a url