This project is read-only.

List filtering?

May 1, 2013 at 12:06 PM
Edited May 1, 2013 at 12:08 PM

I encountered a small problem and have no idea how to fix it.
I want to filter a set of countries/continents that are part of an ad.

Ad class:
    public class Ad
        //Link to Country       
        public virtual List<Country> Countries { get; set; }

        //Link to Continents       
        public virtual List<Continent> Continents { get; set; }     
If I click on the filter icon nothing happens.


Is there a way to fixe this?

Thanks in advance!
May 2, 2013 at 7:48 AM

Unfortunately, the grid does no built-in support of filtering columns with many-to-many relationships. The main reason is grid mvc doesn't know how to build filtering clause on your collection. Now, api not formalized for this operation. For example, In your case I guess that filtering clause can be:
ad=> ad.Countries.Any(c=>c.CountryId == <filterValue>)
As you can see grid mvc doesn't know which property use of the Country to filter collection of Ads.
I will think how to add the support of this filtering type in the next releases, but now you can solve this problem in two ways:

option 1:

Build filtering interface out of the grid, filter items in controller and pass filtered collection to the grid

option 2:

Use grid interface. This method involves that you create custom column filter and overrides deefault grid filter to filter your collection:
    public class CountriesColumn : GridColumn<Ad, IEnumerable<Country>>
        private readonly IColumnFilter<Ad> _customFilter;

        public CountriesColumn(Expression<Func<Order, IEnumerable<Country>>> expression, Grid<Ad> grid)
            : base(expression, grid)
            _customFilter = new CountriesFilter();

        public override Filtering.IColumnFilter<Ad> Filter
            get { return _customFilter; } //overriding default grid filter

    public class CountriesFilter : IColumnFilter<Ad>
        public IQueryable<Ad> ApplyFilter(IQueryable<Ad> items, ColumnFilterValue value)
            //You need to pass ID of the country which you want to filter
            var countryId = int.Parse(value.FilterValue);
            return items.Where(o => o.Countries.Any(country => country.CountryId == countryId));
Then you need to create custom filter widget that user can pick country from select list in the filter popup. You can see how to do this in the sample app
May 2, 2013 at 11:02 PM
Oct 23, 2013 at 4:42 PM
Jdruwe wrote:
Can you post what your solution was?
Aug 25, 2017 at 1:54 PM
Hey, im very interesting about sample to filter cell containing icollection.

Could you please give some help to change js file to add the new filter widget regarding new class we created .

thanks and best regards