Get selection of document list items for display on homepage - umbraco

I have followed the article :-
https://our.umbraco.com/documentation/tutorials/creating-basic-site/articles-parent-and-article-items/
This gets me a list of article items for a particular article parent. However if I had a number of article items under various article parents (eg news items, features, reviews etc) How would I get the last 5 of any of those items for the homepage? (All the items would be of the same document type).

Using the page you referenced above, simply add a .Take(5) to the end of the query like so:
#{
var selection = Umbraco.Content(Guid.Parse("c4b9c457-7182-4cfb-a1af-f0211d67ea51"))
.Children("articlesItem")
.Where(x => x.IsVisible())
.OrderByDescending(x => x.CreateDate)
.Take(5);
}

Related

Get list of all root nodes from child node in Umbraco

I have three document types and its corresponding templates. My tree is as shown below.
1. Video List
2. Video Item
3. Item Details
The Video List page list all items in the Video Item template. Item Details is the child node of Video Item template. I want all nodes in the Video Item from the Item Details page.
i have tried the below code. But it is showing as Xpath need some assembly reference.
Model.Content.XPath("//*[#isDoc and #level = 1]");
Please help
This one is not hard to do. Use #Ancestor and then #Children in a #foreach loop. Read more about traversing with Umbraco on the Umbraco Page.
Assuming we're on the Item Details template (or page, whatever you feel like calling it) and you need to list all your parents and your parents' siblings (if I understand correctly). So you go to the 'grandparent' and you ask for the children:
#foreach (var item in Model.Content.Ancestor("VideoList").Children)
{
//do whatever you feel like in the collection.
//This foreach loop will list you all the Video Item nodes.
}
P.S. I assumed that VideoList is the docTypeAlias that your Video List page has.
i got one solution. Here is my code:
var parentClass = Umbraco.TypedContent(Model.Content.Id).Parent.Parent.Children.Where(x => x.DocumentTypeAlias == "videoItem" && x.IsVisible());
But is this a right way to get parent nodes?

ViewModel not keeping data on postback

I have a ListBox implemented as below.
#if (Model.SelectListVendorBranchSearched != null )
{
#Html.ListBoxFor(model => model.SelectedVendorBranchLeft, Model.SelectListVendorBranchSearched, new {#class="listbox", #size ="10" })
}
But when the form is posted back the viewModel object does not have the original Data Source that the list was bound to. But it has the selected items posted back.
Is that the right behavior?
Let me try to explain what I am trying to achive.
I am trying to implement the following in ASP.NET MVC.
I have a search functionality that populates a list box say List box A. Now I need to select some items in the List Box A and move the items to another List Box say List Box B. Then do another search that refreshes the List Box A with fresh results. Then again select some more items in the List Box A and append to the items already in List Box B. In the end get the items in the List Box B and save it to DB. How can I do this without any JavaScript?

Grails dynamic display implementation (Partial page update)

I have a select field to choose the department. I want to implement a view that displays the list of employees working in that department. I want to display both the select field and list of employees in the same page(view). How is it possible to send the selected field parameter to the same controller and update the list of employees object and display the list in the same page.
There are three main parts to a partial page update. I will give you a general overview of how each part is composed with some examples. This should get you started on the right path. I'm having to make lots of assumptions since your question is light on details. However you should be able to take this information and write your won implementation.
Part 1 (Making the request)
This involves monitoring the select list for a change and sending the value of the selected option to the controller. JQuery is well suited for this. I am going to pretend that the select list has an id of "departmentId" and we want to put the contents of the employee list into an element with the id of "displayList".
<script type='text/javascript'>
<!--
(function() {
$("#departmentId").on("change", function() {
$.get("${g.createLink(controller: 'employeee', action: 'listForDepartment')}/"+$(this).val(), function(data) {
$("#displayList").html(data);
});
});
})();
// -->
</script>
Part 2 (Fulfilling the request)
The second part is our controller code for getting the list of employees. This is just an example of what it might look like. Notice that we are going to render a template instead of a view. This is very important because we don't want Grails (Sitemesh) to apply any layout to the results.
class EmployeeController {
...
def listByDepartment() {
def model = [:]
model['department'] = Department.get(params.id)
model['employees'] = Employee.findAllByDepartment(model['department'])
render template: 'employeeListForDepartment', model: model
}
...
}
Part 3 (Displaying the results in a template)
Finally, our template grails-app/views/employee/_employeeListForDepartment.gsp is going to display our employees for the department. Since we used the employee controller the template is inside the employee views and all templates begin with an underscore.
<h1>Employees for ${department.name}</h1>
<ul>
<g:each in="${employees}" var="employee">
<li>${employee.name}</li>
</g:each>
</ul>
Of course, the details of your implementation my be very different but the overall concept should be the same. This just touches on the main part of this and leaves a lot of details out, such as spinners, canceling requests, and such.

Incrementing an index on a has_many dependent resource

I have an project with many Books. Each book has many Pages. I would like each page to have a pageIndex that makes it unique in relation to the book, so that each time I build a page through the book this index is incremented. Obviously all models have their own id, but that id is unique across all Pages. I would like the first Page built by a Book to have a a pageIndex of 0, the second to have a pageIndex of 1, the third; 2 etc.
I could do something like this:
book = User.books.last
pageIndex = book.pages.count
book.pages.create(:pageIndex => pageIndex)
But it seems to me that it would be much nicer if this index was set automatically by the book when it created a dependant resource of type Page. Is there a way of doing this?
Please note that I have seen this question but what I am looking for is a hook in the parent/owning Resource that allows me to do something to the dependant resource it has created after it has created it and before it saves it. Something like the before_save callback but related to creation of a dependent resource: after_dependent_create.
Associations also have some callbacks. See here http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html "Association callbacks". Possible callbacks are: before_add, after_add, before_remove and after_remove.
Also I would add a counter_cache on the book so you'll know how many pages you have and when creating a new page you won't have to count the existing pages

Determining which checkboxes are not checked in ASP.NET MVC?

Imagine a page which uses checkboxes to keep track of mailing list subscriptions about pets:
cats-list ( )
dogs-list (*)
birds-list ( )
horses-list (*)
I use ASP.NET MVC's Html.Checkbox extension method in a loop (not shown):
<%= Html.Checkbox("subscriptions[" + i +"]", item.subscribed, item.listName) %>
"i" is the iteration variable in the loop. Using this convention allows my controller action to model bind subscriptions[i] to a List<string> which then contains a list of mailing list names to subscribe to.
How can I keep track of what's not checked? For example, if a user unchecks "dogs-list" to unsubscribe from that list, how can I tell? I'll only get "false" back from the hidden form field.
EDIT: The only solution which comes to mind is for the controller action to unsubscribe from all mailing lists, and then resubscribe to lists contained in the List<string> from the checkboxes. That's not ideal though.
You basically answered it yourself with the edit but something a bit more elegant would be better. I.e. Don't delete them all first, just determin which need to be added, which need to be deleted and which need to just remain.
E.g. (air code)
List<string> selectedSubscriptions; //This is passed in from the form using the model binder
List<string> mailingListsToRemove = GetCurrentMailingLists(); //Looks odd but bear with me
List<string> mailingListsToAdd = new List<string>();
//Loop through all the mailing lists they had selected
foreach (string selectedMailingList in selectedSubscriptions)
{
//if the mailing list exists in the list to remove, remove it from that list
//because they obviously don't want it removed
//Remember that this list also contains their current lists.
if (mailingListsToRemove.Contains(selectedMailingList))
{
mailingListsToRemove.Remove(selectedMailingList);
}
else //If it's not in the list to remove then add it
{
mailingListsToAdd.Add(selectedMailingList);
}
}
So at the end of that loop you should have two lists - one containing all the mailing lists that the user wants removed and one containing all the new mailing lists they want to be subscribed to. The mailing lists that they were already subscribed to and still wish to be subscribed to should just be left alone.
HTHs,
Charles

Resources