I am trying to create a UIOMaticListViewFilter by using unique years. Now there are multiple values with a different date but with the same year. See the example below:
Haven't tried it but you could add a new property to the model, where you just output the year (not the full date) by fetching it from the full date and then applying the ListViewFilter attribute on that one?
Hmm yeah in that case you'll need to make sure that the sql statement actually returns a year, you can use the event model for that, will try to set you up with an example
Aight I needed to make a change to the core since filters wasn't passed as event args... so you'll have to wait untill v2.0.3 is out (which I can trigger for you).
It's quite a bit of code but this is because filters/search and ordering needs to keep working, I'll see if I can provide a couple of helper methods so we can keep this code shorter...
But it should then work with this code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using UIOMatic.Examples.ContactEntries.Controllers;
using UIOMatic.Examples.ContactEntries.Models;
using Umbraco.Core;
using Umbraco.Core.Persistence;
using Umbraco.Web.UI.JavaScript;
using Umbraco.Web;
namespace UIOMatic.Examples.ContactEntries
{
public class App: ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
UIOMatic.Services.UIOMaticObjectService.BuiltQuery += UIOMaticObjectService_BuiltQuery;
}
private void UIOMaticObjectService_BuiltQuery(object sender, QueryEventArgs e)
{
if(e.ObjectType == typeof(Event))
{
e.Query = Umbraco.Core.Persistence.Sql.Builder
.Append("SELECT DATEPART(yyyy,Event.Begintime) AS [Year], Event.*")
.Append("FROM Event")
.Append("WHERE 1=1");
// Filter by search term
if (!string.IsNullOrEmpty(e.SearhTerm))
{
e.Query.Append("AND (1=0");
var c = 0;
foreach (var property in typeof(Event).GetProperties())
{
var attris = property.GetCustomAttributes(true);
if (attris.All(x => x.GetType() != typeof(IgnoreAttribute)))
{
var columnName = property.Name;
var columnAttri = attris.FirstOrDefault(x => x.GetType() == typeof(ColumnAttribute)) as ColumnAttribute;
if (columnAttri != null)
columnName = columnAttri.Name;
e.Query.Append("OR " + columnName + " like @0", "%" + e.SearhTerm + "%");
c++;
}
}
e.Query.Append(")");
}
if (e.Filters != null && e.Filters.Any())
{
foreach (var filter in e.Filters)
{
if (filter.Key == "Year")
e.Query.Append("AND DATEPART(yyyy,Event.Begintime) = @0", filter.Value);
else
e.Query.Append("AND " + filter.Key + " = @0", filter.Value);
}
}
e.Query.Append("ORDER BY " + (string.IsNullOrEmpty(e.SortColumn) ? " Id desc" : e.SortColumn + " " + e.SortOrder));
}
}
}
}
Awesome, thanks for this fix! I need it before next month, so not really in a hurry. It is good to know that there will be a possibility, thanks for all the work!
Cool well the release is scheduled for next Thursday (currently doing a maintenance releaase every Thursday) so that would be in time, if you need it sooner let me know
UIOMaticListViewFilter - Distinct by year
Hi,
I am trying to create a UIOMaticListViewFilter by using unique years. Now there are multiple values with a different date but with the same year. See the example below:
I am using the following code to create the ListViewFilter.
I do not have any idea how to get the unique years instead of all the dates it is returning right now. Is this even possible yet?
Hope someone can help me out.
Kind regards,
Yanick
Comment author was deleted
Haven't tried it but you could add a new property to the model, where you just output the year (not the full date) by fetching it from the full date and then applying the ListViewFilter attribute on that one?
Hmm, I have added succesful a filter like this:
Filter shows only the years, like I wanted. But now when I select a year it returns the following error:
Logs is saying this, like expected (column Year does not exists):
Any other idea's?
Comment author was deleted
So if you try to sort by year or what are you trying todo? So what does "select a year" mean :)
Comment author was deleted
AH ok if you actually filter by a year, got it :) hmmm thought that is done in memory will check
Comment author was deleted
Hmm yeah in that case you'll need to make sure that the sql statement actually returns a year, you can use the event model for that, will try to set you up with an example
Super! Maybe I have to save it in my database as extra field (What is not a really nice workaround)?
Like to see your example :)
Thanks!
Comment author was deleted
Mind sharing your table create script? THen I can provide you with the code sample
The SQL to create the table is as following:
My POCO is like this:
I hope you can do something with this :)
POCO on Pastebin: http://pastebin.com/UFytV5nz
Comment author was deleted
Ah awesome yeah gonna try to same as you so a filter on distinct year
Comment author was deleted
Ok can confirm the behaviour :) so now looking into a way we can get this working
Comment author was deleted
Aight I needed to make a change to the core since filters wasn't passed as event args... so you'll have to wait untill v2.0.3 is out (which I can trigger for you).
It's quite a bit of code but this is because filters/search and ordering needs to keep working, I'll see if I can provide a couple of helper methods so we can keep this code shorter...
But it should then work with this code
Comment author was deleted
So when would you need this? I can then plan the 2.0.3 release :)
Awesome, thanks for this fix! I need it before next month, so not really in a hurry. It is good to know that there will be a possibility, thanks for all the work!
Comment author was deleted
Cool well the release is scheduled for next Thursday (currently doing a maintenance releaase every Thursday) so that would be in time, if you need it sooner let me know
Comment author was deleted
Maintenance release is out, so you should be able to update to 2.0.3 once it's indexed by nuget
Great, I will try it asap! :)
Comment author was deleted
Super :)
is working on a reply...