Developer forum

Forum » Templates » RenderItemList and filter

RenderItemList and filter

René Poulsen
Reply

Hi,

I've got an itempublisher template to show some items. On the module I choose 8 items - they have to be shown in a specific way.
Then in the template i render an itemlist of the same itemtype - it has to show all of the items, except the 8 chosen ones on the module.

How can i filter out these 8 items that i choose on the module?

In razor I can create a string (chosenitems) containing the IDs of the 8 items like this: "1,2,3,4,5,6,7,8".
How can i use this in a filter in @RenderItemList ? What I basically want to do is something like this:

Filter = choseitems.IndexOf("Item.Id") = -1

Is this somehow possible?


Replies

 
Bogdan Ciocsan
Reply
This post has been marked as an answer

Hi Rene

I think you have to create a custom item, that uses a Paragraph template that points to your existing ItemPublisher template. 

Something like: 

@{

var pageSize = GetInteger("Item.PageSize");  // here you decide how many you want to show

var sourceAreaId = GetInteger("Item.SourcePageId"); // save which area id you want to use (the page where your items are located)

string filterValue = string.Empty;

var excludedLoop  = GetLoop("chosenItems"); // this should be from DW where you can create a list of numbers in your item that is using the custom RenderItemlist Paragraph template

foreach (var item in excludedLoop)  {

       filterValue += "Id !=\"" + item.GetString("Item.Id") + "\" and"; // before this you should do a check where you do not add "and" to the last item in your loop, you can check if count matches the length of the loop

}

filterValue = "\"@\"" + filterValue + "\"\""; // this would generate a list looks like @"ItemId!=1 or ItemId!=2 or Item!="3"  

RenderItemList(new
{
        ItemType = "ItemName",  
// ItemName should be replaced with the correct system name
        ListSourceType = "Page",
        ListSourcePage = sourceAreaId,
        IncludeAllChildItems = true,
        ItemFieldsList = "*",
        ListTemplate = "ItemPublisher/List/EventListWithoutFilters.cshtml",
        ListPageSize = pageSize,
        Filter = filterValue,
        ListOrderBy = "Date",
// date is an item property, you can replace it with whatever you want
        ListOrderByDirection = "ascending"
})

}

I have not tested this, but it should work :) 

Best regards,

Bogdan 

Votes for this answer: 1
 
René Poulsen
Reply

Hi Bogdan,

I don't undertand what you mean.

1) "I think you have to create a custom item, that uses a Paragraph template that points to your existing ItemPublisher template." <-- What exactly do you mean? Do I have to create an item where I can link to the other paragraph? Or?

2) "var excludedLoop  = GetLoop("chosenItems");" <-- I dont understand where you get this from?!

 
René Poulsen
Reply

Hi again,

Got it working in the itempublisher template (with no custom item poiting to the itempublisher paragraph).

In my itempublisher template for the first 8 items I create the filter like you describe above like this:

filterValue = string.Empty

foreach (var caseitem in GetLoop("ItemPublisher:Items.List")) {

    if (filterValue != string.Empty) {
            filterValue += " and Id!=\"" + caseitem.GetString("ItemPublisher:Item.Field.Id") + "\"";
        } else {
            filterValue = "Id!=\"" + caseitem.GetString("ItemPublisher:Item.Field.Id") + "\"";
        }

}

filterValue = "\"@\"" + filterValue + "\"\"";

@RenderItemList(new {

    ...

    ListTemplate = "itempublisher/list/my-other-list-template.cshtml,

    filter = filterValue,

    ...

}

 
Bogdan Ciocsan
Reply

Hi Rene

If you got it working, I'm glad to hear that. What I meant by a custom item, I would normally make an item called CustomItemPublisher, which can act somehow like a normal ItemPublisher but uses the RenderItemList function, where I can choose what page to show and limit my items (by making an itemlist which has the loop "chosenitems"). So I would know that I am not using the standard ItemPublisher functionality.

But if you got it working by using the normal itemPublisher, there is no point in creating an item.

My only remark would be when you check if filter value is empty, you are still adding "and" to the last item in your loop, but without a condition, which would generate something like: ""Id!="5" and Id!="6" and". It might be that it works, but I would fix that :)

Best regards,

Bogdan

 
René Poulsen
Reply

Okay, now I understand.

I don't add "and" after the last item - I add it before, so it should be working :-) I guess I kind of reversed it.

You want to check if it's the last item (and then not add "and")

I check if it's the first item. If its the first item I do not start with "and". If it's not the first item, I always start with "and". That way my filter would never end with "and" - just like yours won't :-)

 
Bogdan Ciocsan
Reply

That's true, my bad, I looked again at your code, I thought you forgot :)

 
Marius Tudor
Marius Tudor
Reply

Hello Bogdan, 

I also have a problem with filtering some item publisher results 

The context.

I have some Dw_Page items that I want to list based on the following criteria:

 
string filterValue2= "Item.Tip='Articole'";
filterValue2 =  filterValue2 + " and " + "Item.Id !='" + mainArticleID +"'" + " and " + "Item.Id !='" + secondaryArticleID +"'" + " and " + "Item.Id !='" + thirdArticleID +"'"  + " and " ;
string  filterValue6 =  "Item.SectiuneParinteId ='" + GetString("DwPageID")+ "'";
string  filterValue7 =  "Item.SectiuneSecundara ='" + GetString("DwPageID")+ "'";
filterValue2 =   "("+ filterValue2 + filterValue6 + ")" + " or " + "(" + filterValue2 + filterValue7 + ")"  ;
 
if I print filterValue2 it shows me :
Item.Tip='Articole' and Item.Id !='14760' and Item.Id !='14757' and Item.Id !='14756' and Item.SectiuneParinteId ='17' or Item.Tip='Articole' and Item.Id !='14760' and Item.Id !='14757' and Item.Id !='14756' and Item.SectiuneSecundara ='17'
 
 
The result seems to be correct besides the fact that the filter does not really work. Any ideas on how to make that "or " to work by grouping the filter properties using brackets?
Thanks in advance
 
 

 

You must be logged in to post in the forum