12.0: Add Star Option
Add isStarred
Property to Database
isStarred
Property to DatabaseLet's add an option to star a note. This is an example of how to make changes after you've completed the first version of something. It's a good idea to branch off and do this on a separate branch.
First, we need to update our database to have a property of wether or not a note is starred.
Go to ElevenNote.Data -> Note.cs
Add the following property between the
Content
andCreatedUtc
properties[DefaultValue(false)] public bool IsStarred { get; set; }
CTRL .
to bring in the using statementIn order for the database to be updated with this new column, we need to:
Update the NoteListItem
Model
NoteListItem
ModelWe'll want a clickable star on the page that shows all of our notes. The model for that view is NoteListItem
Go to ElevenNote.Models -> NoteListItem.cs and add the
IsStarred
property between theTitle
andCreatedUtc
[UIHint("Starred")] public bool IsStarred { get; set; }
The
UIHint
will link up to a view we make later
Update NoteService.cs
NoteService.cs
We'll need the service to know about the star as well???
Add the
IsStarred
option inIEnumerable<NoteListItem>
.new NoteListItem { NoteId = e.NoteId, Title = e.Title, IsStarred = e.IsStarred, CreatedUtc = e.CreatedUtc }
Add Star to Index View
We need to add a column and column name for the isStarred
property in our table that displays the list of notes.
Open ElevenNote.Web -> Views -> Note -> Index.cshtml
Add the
DisplayNameFor
forIsStarred
<th> @Html.DisplayNameFor(model => model.Title) </th> <th> @Html.DisplayNameFor(model => model.IsStarred) </th> <th> @Html.DisplayNameFor(model => model.CreatedUtc) </th> <th></th> </tr>
Add the
DisplayFor
forIsStarred
<td> @Html.DisplayFor(modelItem => item.Title) </td> <td> @Html.DisplayFor(modelItem => item.IsStarred, new { Id = item.NoteId }) </td> <td> @Html.DisplayFor(modelItem => item.CreatedUtc) </td> <td> @Html.ActionLink("Edit", "Edit", new { id = item.NoteId }) | @Html.ActionLink("Details", "Details", new { id = item.NoteId }) | @Html.ActionLink("Delete", "Delete", new { id = item.NoteId }) </td> </tr>
Now, we need some JavaScript to make the star functionality work
Add this code to the very end of
Index.cshtml
, after</table>
:@section scripts { <script language="javascript" type="text/javascript"> $(document).ready(function () { $("i[data-toggle='star']") .css({ "cursor": "pointer" }) .click(function (e) { var target = $(e.target); var noteId = target.attr("data-id"); var starSet = target.hasClass("glyphicon-star"); $.ajax({ method: starSet ? "DELETE" : "PUT", url: "/api/note/" + noteId + "/star", data: null }) .done(function (r) { target.removeClass(); target.addClass("glyphicon " + (starSet ? "glyphicon-star-empty" : "glyphicon-star")); }) .fail(function (r) { alert("Failed to change star status"); }); }); }); </script> }
Add the Starred Display Template
Similar to the DateTime display changes we made, we'll make a view specifically for the star. Notice that the name of this view, Starred
, is the same as the UIHint
from the NoteListItem
model.
Right click on the ElevenNote.Web -> Views -> Shared -> Display Templates folder and select Add -> View
Name it
Starred
and fill it out like this:Add this code:
@model bool <span> <i class="glyphicon @(Model ? "glyphicon-star" : "glyphicon-star-empty")" data-toggle="star" data-id="@ViewBag.Id"></i> </span>
Next, we'll do more setup to get the star function working.
Last updated