How to use the search filter with a custom adaptor in TreeGrid?

Answer:

We can achieve this by handle Filtered actions based on the HierarchyMode of FilterSettings. For example, We have filtered the records on the reading method of the DataAdaptor abstract class based on HierarchyMode after navigating over the pages.

Index.Razor

<SfTreeGrid @ref="treeGrid" TValue="TreeData.BusinessObject" AllowPaging="true" AllowFiltering="true"

HasChildMapping="isParent" IdMapping="TaskId" ParentIdMapping="ParentId"

TreeColumnIndex="1" AllowSorting="true"

Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel", "Search" })">

<SfDataManager AdaptorInstance="@typeof(CustomAdaptor)" Adaptor="Adaptors.CustomAdaptor">SfDataManager>

<TreeGridPageSettings PageSize="1">TreeGridPageSettings>

<TreeGridColumns>

<TreeGridColumn Field="TaskId" HeaderText="Task ID" IsPrimaryKey="true" Width="80" TextAlign="TextAlign.Right">TreeGridColumn>

. . .

TreeGridColumns>

SfTreeGrid>

@code{

public static SfTreeGrid treeGrid { get; set; }

public static List TreeGridData { get; set; }

public static List CurrentView { get; set; }

public static List CompleteData { get; set; }

public static List FilteredData = new List();

public static int? filterCount;

protected override void OnInitialized()

{

TreeGridData = TreeData.GetSelfDataSource().ToList();

}

// Implementing custom adaptor by extending the DataAdaptor class

public class CustomAdaptor : DataAdaptor

{

// Performs data Read operation

public override object Read(DataManagerRequest dm, string key = null)

{

filterCount = null;

IEnumerable DataSource = TreeGridData;

if (dm.Search != null && dm.Search.Count > 0)

{

// Searching

DataSource = DataOperations.PerformSearching(DataSource, dm.Search);

}

. . .

if (dm.Where != null && dm.Where.Count > 0)

{

if (dm.Where[0].Field == null)

{

DataSource = DataOperations.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);

}

else

{

DataSource = DataOperations.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);

}

}

int count = DataSource.Cast().Count();

if (dm.Skip != 0)

{

//Paging

DataSource = DataOperations.PerformSkip(DataSource, dm.Skip);

}

if (dm.Take != 0)

{

DataSource = DataOperations.PerformTake(DataSource, dm.Take);

}

if (dm.Where != null && dm.Where.Count > 0)

{

if (dm.Where[0].Field == null)

{

FilteredData.Clear();

filterCount = 0;

//For FilterHierarchy mode as "Parent"(default mode), we need to return the parent record of the filtered record along with the actual filtered record

//calling FilterHierarchyData to collect the parent data of filtered data by passing filtered data and complete data source as params

FilteredData = FilterHierarchyData(DataSource);

DataSource = FilteredData; // assigning new list to DataSource and returning

}

}

count = filterCount != null ? (int)filterCount : count;

return dm.RequiresCounts ? new DataResult() { Result = DataSource, Count = count } : (object)DataSource;

}

public List FilterHierarchyData(IEnumerable filterData)

{

foreach(var data in filterData)

{

if(data.ParentId !=null)

{

FilteredData = FilterData(data); // calling FilterData method to add parent to new list

}

FilteredData.Add(data);//adding filtered data to list

}

return FilteredData;

}

public List FilterData(TreeData.BusinessObject data)

{

var parent = TreeGridData.Where(p => p.TaskId == data.ParentId).FirstOrDefault();

if(parent.ParentId != null)

{

FilteredData = FilterData(parent);

FilteredData.Add(parent);

} else

{

var alreadyPresent = FilteredData.FindIndex(d => d.TaskId == parent.TaskId);

if (alreadyPresent == -1)// if parent is not present already, then add parent to list

{

FilteredData.Add(parent);

filterCount++; //adding parent data to list

}

}

return FilteredData;

}

}

}


Find the sample for use search filter with a custom adaptor in TreeGrid from here.


Loader.
Up arrow icon