Developer forum

Forum » Templates » Razor: Filtering list based on parameter

Razor: Filtering list based on parameter

Thomas Overgaard Nielsen
Reply

Hi all

This is really a question on best practices

Problem:

I have an itemtype: Contact
As one of the fields in this itemtype is a checkboxlist (called Areas) by which you can assign a contact to one or more company areas.

Now on one page of the site I would like to show all contacts belonging to area "X" and of course on another page contacts belonging to area "Y"
The obvious solution is using an Item publisher on each of these pages and filtering the itemlist before showing, which works just fine.
The filtering is done as follows:

    var areaToShow= "1";
    var contactList = GetLoop("ItemPublisher:Items.List").Where(e => e.GetString("ItemPublisher:Item.Areas").Split(',').Any(s => s == areaToShow));

Now this implies that I need a separate list template for each area, I would like to show. It would be much more handy, if I could set the area, which I want to show, in the backend.

I tried creating a new Item Type called ContactList with just one field: AreaToShow.
In the item template I can read this parameter and I can dynamically create an itempublisher listing contacts.
But what I can't figure out is how to transfer the parameter AreaToShow from the Item template to the itempublisher list template, and thus filtering the list by this parameter.

I guess you could register the paramter as a session variable, but that is not really what these are for.
You probably also could skip the itempublisher and do a db select instead.

Any input?

Best regards
Thomas


Replies

 
Mikkel Ricky
Reply

If I understand you correctly, you just need the current website id for the filterering, and in this case

Pageview.Area.ID

is your friend. And the Pageview object is your friend in many case.

Best regards,
Mikkel

 
Thomas Overgaard Nielsen
Reply

Thanks for your reply, Mikkel

I guess I made my question a bit confusing because of my naming.
When I write, that a contact is assigned to an area, I am not referring to a website area. Area is just an abitrary grouping of my contacts .. I should have called it 'departments' instead.

A contact belongs to one or more departments (areas), and when making a listing on a page, it would be nice to be able to specify the filtering in the backend.

I cannot use the existing filtering option in the ItemPublisher settings due to the fact, that a contact can be assigned to more than one department (area). The filter only allows "x equals y" .. and not .. "x exists in array y"

 
Mikkel Ricky
Reply
This post has been marked as an answer

I have made a product backlog item for the missing feature to be able to filter an item list by "list contains value".

You can render an item publisher directly in a template using @RenderItemList (cf. http://templates.dynamicweb.com/TemplateTags/Dynamicweb-template-tags/Module-tags/Item-publisher/Item-Render.aspx) and in this list you can actually filter on the selected list values:

@{
var areaId = GetInteger("…");
if (areaId > 0) {
  @RenderItemList(new {
    ItemType = "Contact",
    ListSourceType = "SelfArea",
    ItemFieldsList = "*",
    ListTemplate = "ItemPublisher/List/List.cshtml",
    ListPageSize = 10,
    ListOrderBy = "Name",
    Filter = "Areas contains \""+areaId+"\""
  });
}
}

Now all you need is to find a place to store the actual area id. One way to do this is to create a custom item based paragraph with an area selector and then use the code above in the paragraph template. Or you could add the area id as a page property (using an item type) and look for the setting in the root path (see http://developer.dynamicweb.com/forum/templates/getting-data-from-parent-page.aspx#Reply39422).

Best regards,
Mikkel

 

Votes for this answer: 1
 
Thomas Overgaard Nielsen
Reply

Now that 'filter' property of the Item Publisher was exactly what I needed :)

Thanks a lot for taking your time, Mikkel.
And great if we get the enhanced filtering options in the backend.

Best regards
Thomas 

 

You must be logged in to post in the forum