Grid - How to filter clienttemplate text

1 Answer 685 Views
Grid
Tim
Top achievements
Rank 1
Tim asked on 24 Sep 2021, 02:02 PM

I have a grid with a clienttemplate. How do I filter on the template text instead of the bound field?

                    columns.Bound(e => e.ProjectAddress).Title("Address").Width(150).Filterable(f => f.Extra(false))
                        .ClientTemplate("#=ProjectAddress#<br/>#=ProjectCity#, #=ProjectStateCode# #=ProjectPostalCode#");

I want to filter on address, city, state and zip code not just ProjectAddress.

Thanks.

 

 

 

1 Answer, 1 is accepted

Sort by
0
Ivan Danchev
Telerik team
answered on 29 Sep 2021, 10:28 AM

Hello Tim,

Built-in the Grid filters only by the fields the respective column is bound to. The ClientTemplate is for visualization purposes only. That said, you can implement your custom filtering logic, if you need a different behavior than the default one. The Grid exposes a Filter event, which you can attach a handler to:

.Events(ev => ev.Filter("onFilter"))

In the handler you have access to the Grid's dataSource, the typed value in the filter input and the filter operator, and you can use them to filter the data. For example:

function onFilter(e) {
	//a reference to the Grid's dataSource:
	var dataSource = e.sender.dataSource;

	//e.field holds the name of the field the user attempts to filter:
	if (e.field == "ProjectAddress") {
		//e.filter holds the filter descriptor:
		if (e.filter != null) {
			//prevent the default filtering:
			e.preventDefault();

			//implement custom filtering:
			var filterValue = e.filter.filters[0].value;
			var filterOperator = e.filter.filters[0].operator;

			this.dataSource.filter({
				logic: "or",
				filters: [
				  {
				  	field: "ProjectAddress",
				  	operator: filterOperator,
				  	value: filterValue
				  },
				  {
				  	field: "ProjectCity",
				  	operator: filterOperator,
				  	value: filterValue
				  },
				  {
				  	field: "ProjectStateCode",
				  	operator: filterOperator,
				  	value: filterValue
				  },
				  {
				  	field: "ProjectPostalCode",
				  	operator: filterOperator,
				  	value: filterValue
				  }
				]
			})
		}
	}
}

Regards,
Ivan Danchev
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Grid
Asked by
Tim
Top achievements
Rank 1
Answers by
Ivan Danchev
Telerik team
Share this question
or