Date filter not working

4 posts, 0 answers
  1. Jhon
    Jhon avatar
    2 posts
    Member since:
    May 2018

    Posted 10 Jan Link to this post

    Hi, I´m using a date filter, but it shows all the data. I´ve done it like this:

    On properties > Report parameters > Add > Type: DateTime Visible: True

    On properties > Filters > on Expression I have =Trim(Fields.Date) on Operator = on Value = Fields.Date

    On preview I choose a date, but all records appear.

  2. Alex Hajigeorgieva
    Admin
    Alex Hajigeorgieva avatar
    771 posts

    Posted 14 Jan Link to this post

    Hello, Jhon,

    Thank you for trying out the Kendo UI for ASP.NET MVC chart.

    I suspect that the Fields.Date property in the data is not a DateTime but it is rather a string type since the Trim() method is a string method. (Unless it is a custom static method of course).

    You can see a runnable example which adds a filter as part of its data source and it works as expected:

    https://dojo.telerik.com/@bubblemaster/iGofebeQ

    So in case, the field is indeed a string, it needs to be transformed into a DateTime on the server or to use a data source with a schema in the Razor definition so that the Chart can work with the correct types:

    Example of Razor data source with a schema:

    .DataSource(d => d
       .Model(m => {
          m.Id(f => f.Id);
          m.Field("CreatedAt", typeof(DateTime));
       })

    In case you need further assistance, it would be helpful to see the model as well as the Chart and data source declaration.

    Kind Regards,
    Alex Hajigeorgieva
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  3. Jhon
    Jhon avatar
    2 posts
    Member since:
    May 2018

    Posted 14 Jan in reply to Alex Hajigeorgieva Link to this post

    Hi. But the data comes from a data base, do you have an example with one?
  4. Alex Hajigeorgieva
    Admin
    Alex Hajigeorgieva avatar
    771 posts

    Posted 14 Jan Link to this post

    Hi, Jhon,

    It does not matter where the data comes from. The database querying can be done in any way that you prefer, however, the expected response is Json with an IEnumerable of the model to which the chart is bound to.

    public ActionResult Orders_Read()
    {       // data base call
        var result = Enumerable.Range(1, 10).Select(i => new OrderViewModel
        {
            OrderID = i,
            Freight = i * 10,
            OrderDate = DateTime.Now.AddDays(i),
            ShipName = "ShipName " + i,
            ShipCity = "ShipCity " + i
        });
     
        return Json(result);
    }

    @(Html.Kendo().Chart<ChartWithFilter.Models.OrderViewModel>()
        .Name("chart")
        .Series(series =>
        {
            series
                .Line(model => model.Freight, categoryExpression: model => model.OrderDate);
        })
        .CategoryAxis(axis => axis
        .Date()
        .BaseUnit(ChartAxisBaseUnit.Weeks)
        )
        .DataSource(dataSource => dataSource
            .Read(read => read.Action("Orders_Read", "Chart"))
            .Filter(f=>f.Add(x=>x.OrderDate).IsGreaterThanOrEqualTo(new DateTime(2019,1,10)))
        )
    )

    If you can provide the model, chart definition and data source, I can attempt to replicate this behaviour. Since I do not have it, I am sending you a runnable example for MVC attached here. You should be able to build it, restore the packages and run it. (let me know in case you face any difficulties)

    You may find other examples in this repository as well:

    https://github.com/telerik/ui-for-aspnet-mvc-examples/tree/master/chart/

    Regards,
    Alex Hajigeorgieva
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top