fingers10 / JqueryDataTablesServerSide

Asp.Net Core Server Side for Jquery DataTables Multiple Column Filtering and Sorting with Pagination and Excel Export
MIT License
227 stars 37 forks source link

[QUESTION] Search Custom Property Columns #18

Closed cihangll closed 4 years ago

cihangll commented 4 years ago

Hi Abdul,

How can i search custom property columns?

For example;

I have an enum in the entity model like this.

public enum ItemType : short {
    [Display(Name = "Item Card")]
    InventoryCard = 0,
    [Display(Name = "Service Card")]
    ServiceCard = 4
}
[DisplayName("Item Type")]
[Sortable]
[SearchableString]
public string ItemType { get; set; }
CreateMap<ItemTransfer, ItemCardDataTableModel>()
     .ForMember(dest => dest.ItemType, opt => opt.MapFrom(src => src.ItemType.GetDisplayName()))......

I want to search display name on the datatable but i am getting error like this; image

Because UI property type is string and the entity property type is short.

Other Example;

I have a name and surname property in the entity model. I want to show Name + Surname => NameSurname column on the UI. How can i search this column?

Thanks.

chrisxh commented 4 years ago

Hi @cihangll, Depending on the arguments that you're passing to SearchOptionsProcessor.Apply(), I think the server-side part of JqueryDataTablesServerSide may be getting in the way of what you want. Are you passing an IQueryable<ItemTransfer>?
If so, the SearchOptionsProcessor.Apply method will be querying (searching) against the source ItemTransfer type, not the projected ItemCardDataTableModel type.

When the incoming datatables query parameters (JqueryDataTablesParameters) are saying, "please search for the string 'Item Card' in the column named 'ItemType'", JQDT will produce a server-side expression like ItemTransfer.ItemType.Contains("Item Card").

Because ItemTransfer.ItemType cannot be convinced to behave like a string, the exception appears.

In order to search by string on the ItemCardDataTableModel.ItemType, you need to pass an argument of type IQueryable<ItemCardDataTableModel> to SearchOptionsProcessor.Apply(). This is also how you can achieve a search against a NameSurname column.

fingers10 commented 4 years ago

Hi @chrisxh,

Thanks for the explanation.

Hi @cihangll,

Right now the package doest support enum type/custom type. I had a long thought of implementing this. I'll try to see if I can find a way to make this happen with attributes.

Thanks, Abdul

fingers10 commented 4 years ago

@cihangll, look at this enum filtering issue. Looks like if you configure your entity properly in efcore then enum filtering has to work. I'll try from my end and get back to you.

fingers10 commented 4 years ago

@cihangll If your using ef core why don't you try value converters for enum properties? I'm focusing on asp.net core 3.0 upgrade. I'll try a sample after that.

cihangll commented 4 years ago

@fingers10 Thank you for that. I'll try value converters for enum properties.

fingers10 commented 4 years ago

@cihangll , I tried with value converters. Looks like I need to define an extra attribute like [SearchableEnum(typeof(YourEnum))] for that to work. I'll add this in the next release.

fingers10 commented 4 years ago

@cihangll but [SearchableEnum(typeof(YourEnum))] will only allow eq search. when I try co Enum.TryParse<TEnum> throws exception. This has this limitation. But for eq case you enter the enum value with or without space Enum.TryParse<TEnum> will succeed.

cihangll commented 4 years ago

@fingers10 This is awesome. Thank you for this enhancement.