This is a migrated thread and some comments may be shown as answers.

Filtering grid with data from resource file

1 Answer 131 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Vangel
Top achievements
Rank 1
Vangel asked on 21 Mar 2017, 03:17 PM

I have table in database which contains events of a system and these events have severity code and event code. These codes have to be localized, it means the text for the severity and event codes should be taken from resource files.

Here is how we construct data for the grid:

public ActionResult GetEvents([DataSourceRequest] DataSourceRequest request)
{
    IQueryable<SystemEventGridModel> data = SystemEvent.GetSystemEventFromDatabase().Select(systemEvent =>
        new SystemEventGridModel
        {
            Id = systemEvent.Id,
            EventCode = systemEvent.EventCode,
            SeverityCode = systemEvent.SeverityCode,
            Date = systemEvent.Date,
            Node = systemEvent.Node,
            IsActive = systemEvent.IsActive
        });
 
    DataSourceResult dataSourceResult = data.ToDataSourceResult(request);
    foreach (SystemEventGridModel gridModel in dataSourceResult.Data)
    {
        gridModel.EventText = Language.ResourceManager.GetString($"EventText{gridModel.EventCode}");
        gridModel.SeverityText = Language.ResourceManager.GetString($"SeverityText{gridModel.SeverityCode}");
    }
    return Json(dataSourceResult);
}

 

Then in the grid we show the localized text but not the codes:

@(Html.Kendo().Grid<SystemEventGridModel>()
      .Name("SystemEventGrid")
      .Columns(columns =>
      {
          columns.Bound(x => x.Node).Title(Language.SystemEventGridModelTitleNode);
          columns.Bound(x => x.SeverityCode).Title(Language.SystemEventGridModelTitleSeverityText).ClientTemplate("#=SeverityText#").Filterable(x => x.Multi(true).CheckAll(false).ItemTemplate("function(e){return columnFirtalableItemTemplate('SeverityCode', 'SeverityText')}"));
          columns.Bound(x => x.EventCode).Title(Language.SystemEventGridModelTitleEventText).ClientTemplate("#=EventText#");
          columns.Bound(x => x.Date).Title(Language.SystemEventGridModelTitleDate);
          columns.Bound(x => x.IsActive).Title(Language.SystemEventGridModelTitleIsActive);
      })
      .DataSource(binding => binding.Ajax().Read("GetEvents", "Home", null))
      .Sortable()
      .Filterable()
      .Pageable(x => x.Refresh(true)))

 

The problem is that we can't use the standard filtering for the columns (e.g. EventText) which do not existing in database and are taken from different places:

http://imgur.com/a/bcV7i

Is there a way to apply the filtering for the text and search by code from database?

P.S. I attached only the source files (models, views and controllers) for the example code.

1 Answer, 1 is accepted

Sort by
0
Accepted
Konstantin Dikov
Telerik team
answered on 23 Mar 2017, 11:35 AM
Hello Vangel,

A possible solution for the scenario that you have is to use the reversed approach over the filter value in the DataSourceRequest object and create a search for the matching EventCode for example:
public ActionResult Orders_Read([DataSourceRequest]DataSourceRequest request)
{
    foreach (FilterDescriptor filter in request.Filters)
    {
        if (filter.Member == "EventText")
        {
            filter.Member = "EventCode";
            filter.Value = //get the matching EventCode from the EventText;
        }
    }
    return Json(GetOrders().ToDataSourceResult(request));
}

As for the filtering over the EventText field, since it does not exist in the database, you could remove the filters from the request and make custom filtering over the collection.


Regards,
Konstantin Dikov
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Vangel
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or