4
Answers

DataTable Hierarchical Search.

Nimish Saxena

Nimish Saxena

1y
808
1

1) Test
   a) test1
   b) test2
      b.1) Hello
      b.2) Ram
   
2) Test Data
   a) Demo
   b) dummy
      b.1) Cup
      b.2) Plate
      
3) Dummy
   a) test11
   b) test22
      b.1) Hello
      b.2) Ram1

If we search text "Hello" then data should looks like below

1) Test
   b) test2
      b.1) Hello

3) Dummy 
   b) test22
      b.1) Hello
     

Answers (4)
2
Sam Hobbs

Sam Hobbs

55 29.3k 2.1m 1y

Search for articles about self-referencing tables and hierarchical tables. Those are two names of the same thing, there are other names that are sometimes used but I forget the others. The articles will explain how to arrange the data such that each item has a reference to the parent.

You also want an index on the text, such as Hello in your example. Then to do the search you describe, search for items with the text and for each result, just walk up the hierarchy and then show the hierarchy from the top down. No need to do the search yourself and walk every existing hierarchy.

2
Chris Love

Chris Love

Tech Writer 12.1k 428.9k 1y

Hello,

To implement a hierarchical search in a DataTable within an ASP.NET Core application, you could leverage the concept of filtering based on a tree traversal algorithm. Since you're looking to filter the structure and retain the hierarchy, you might consider creating a recursive method that traverses the hierarchy and selectively includes nodes that match the search term.

Here's a high-level example of how you could implement this in C#:

public class Node
{
    public string Key { get; set; }
    public string Value { get; set; }
    public List<Node> Children { get; set; } = new List<Node>();

    // Recursive search function
    public Node Search(string searchTerm)
    {
        Node result = null;
        if (this.Value.Contains(searchTerm))
        {
            result = new Node { Key = this.Key, Value = this.Value };
            // No need to search children if the current node matches
        }
        else
        {
            foreach (var child in Children)
            {
                var searchResult = child.Search(searchTerm);
                if (searchResult != null)
                {
                    if (result == null)
                    {
                        result = new Node { Key = this.Key };
                    }
                    result.Children.Add(searchResult);
                }
            }
        }
        return result;
    }
}

// Usage
var root = /* Your tree structure here */;
var filteredRoot = root.Search("Hello");
// Convert 'filteredRoot' to the desired format for display

This recursive function will return a new Node containing only the hierarchy that includes the search term "Hello". You will need to build your tree structure using the Node class.

The filtering logic is handled within the Search method, which checks the current node and recursively its children for the search term. Child nodes are only included in the result if they or their descendants match the search term.

I hope this helps! Let me know if you have further questions or if you need assistance with a specific part of the implementation.

2
Nimish Saxena

Nimish Saxena

1.1k 597 86.5k 1y

If I seach "Probation" text then data should be show like Criminal Record->Case Name->Disposition Records->Probation.
i am usng datatable.js

2
Vishal Joshi

Vishal Joshi

244 7.8k 135.3k 1y

To search a hierarchical DataTable recursively, you can: 

  • Search for immediate children for a given DataRow and add them to the HashSet
  • Flatten a hierarchical data table into a one-level report

You can also apply searching to a specific column in DataTables through the column().search() method.

 

Please share DB and code details to get clear idea of expected solution.