0

i want to search for a string in my array. My Problem ist that my array is really big and has many nested arrays.

It look like this:

[  
   {  
      "Id":null,
      "Text":"Marketing",
      "Gruppen":[  
         {  
            "Id":null,
            "Text":"Werbeartikel",
            "Gruppen":[  
               {  
                  "Id":null,
                  "Text":"Werbegeschenk Hobby, Freizeit",
                  "Gruppen":[  
                     {  
                        "Id":"51004839",
                        "Text":"Taschenmesser (Werbeartikel)",
                        "Gruppen":null
                     },
                     {  
                        "Id":"51004843",
                        "Text":"Schirm (Werbeartikel)",
                        "Gruppen":null
                     },
                     {  
                        "Id":"51004845",
                        "Text":"Sportartikel (Werbeartikel)",
                        "Gruppen":null
                     }
                  ]
               }
            ]
         }
      ]
   }
]

Now i want to search for (Taschenmesser). i only need to look up all "Text" fields.

I dont know how to do this. I want to get the result as:,

{  
 "Id":"51004839",
 "Text":"Taschenmesser (Werbeartikel)",
 "Gruppen":null
}

Here is the Method i search with. But it dont search for nested nested nested arrays:

$scope.SearchForGroup = function (pSearchText) {
        var lGruppe = $filter('filter')($scope.WarenGruppenResponse.WarenGruppe, "Schirm")

    };

here is plunker demonstrating the problem http://plnkr.co/edit/l0cnZQPAh2HXLX1yTnLl?p=info

Ertan
  • 11
  • 4
  • 2
    It *does* search in nested nodes of your tree. Enter "Schirm" in the text box, and you'll see that the ng-repeat still displays the unique element in your array. Enter "Foo", and you'll see it doesn't anymore. What do you really want to achieve? If you want to display all the nodes of the tree that contain the entered text, why don't you just transform the tree into a flat array of nodes, and then filter that flat array? – JB Nizet Aug 17 '17 at 07:45
  • i want when i enter "Schirm" in the textbox that "Schirm" appear and not the parent array which contains the nested array "Schirm" – Ertan Aug 17 '17 at 08:06
  • So, just do what I advised. Transform your tree into a flat array of nodes, and filter these nodes by their Text attribute. It's 10 lines of code. http://plnkr.co/edit/MUxMg4Pua9r1he1K6NJm?p=preview – JB Nizet Aug 17 '17 at 08:08
  • I understand, But its only a small snippet of my big array. So i can be 4-5 nested areas. What will i do then? Groups in Groups in Groups in Groups..... – Ertan Aug 17 '17 at 08:12
  • The depth doesn't matter. Just read the code in the plunkr I gave you. It's simple recursion. You need to understand recursion if you deal with tree structures. – JB Nizet Aug 17 '17 at 08:15
  • i have tested it with my big array and it works perfect. WOW Thank you very much for youre answer bro. – Ertan Aug 17 '17 at 08:23

1 Answers1

0

Since you have nested list, you can write search algorithm as posted here: LINK

I would use watcher on input and generate new array:

 $scope.search = '';

  $scope.foundItems = [];

    function searchTree(element, matchingTitle){
     if(element.Text.indexOf(matchingTitle) > -1){
          return element;
     }else if (element.Gruppen != null){
          var i;
          var result = null;
          for(i=0; result == null && i < element.Gruppen.length; i++){
               result = searchTree(element.Gruppen[i], matchingTitle);
          }
          return result;
     }
     return null;
}

    $scope.$watch(function () {
        return $scope.search;
    },
            function (newVal, oldVal) {

                $scope.foundItems = [];

                if (newVal !== undefined && newVal !== oldVal && newVal !== '') {


                    var result = searchTree($scope.data[0], newVal);

                   console.log(result);

                   $scope.foundItems.push(result);

                }
   });

DEMO

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225