Angular Filter Nested JSON Hierarchical Treeview in Controller

Angular Filter Nested JSON Hierarchical Treeview in Controller

In this Post We Will Explain About is Angular Filter Nested JSON Hierarchical Treeview in Controller With Example and Demo.Welcome on infinityknow.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Angularjs Filter JSON hierarchy tree in controllerExample

In this post we will show you Best way to implement Filter JSON In Hierarchical Way Using AngularJS, hear for filtering – AngularJS displaying hierarchical datawith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Make a HTML file and define markup

<!DOCTYPE html>
<html data-ng-app="liveApp" data-ng-controller="liveCtrl">
<head>
<title>Filter JSON In Nested Using AngularJS Example</title>
<script src="angular.min.js"></script>
<style>
.live_panel {
 margin:auto;
 width:800px;
}
</style>
</head>
<body>
//some code.........
</body>
</html

Make a Body Part define markup

	<div class="live_panel">
		<script type="text/ng-template" id="live_program_list_render.html">
            <li>{{data.name}}</li>
            <li data-ng-if="data.second_chield.length > 0">
               <ul data-ng-repeat="data in data.second_chield" data-ng-include="live_program_list_render.html"></ul>
            </li>
        </script>
		<div>Enter Programing text to filter data</div>
		<div>
			<input data-ng-model="query_str" placeholder="Enter Text here" />
		</div>
		<div>
			<ul data-ng-repeat="data in postData" data-ng-hide="display_filterdata">
				<li>{{data.name}}
					<div data-ng-if="data.second_chield.length > 0">
						<ul data-ng-repeat="data in data.second_chield"
							data-ng-include="'live_program_list_render.html'"></ul>
					</div>
				</li>
			</ul>
			<ul data-ng-repeat="data in all_programdata"
				data-ng-show="display_filterdata">
				<li>{{data.name}}
					<div data-ng-if="data.second_chield.length > 0">
						<ul data-ng-repeat="data in data.second_chield"
							data-ng-include="'live_program_list_render.html'"></ul>
					</div>
				</li>
			</ul>
		</div>
	</div>

Make a js file and define scripting

Create a Module

var liveApp = angular.module('liveApp', []);

Create a Simple Controller using AngularJS

function liveCtrl($scope) {
	$scope.postData = [ {
		"name" : "Java Script",
		"second_chield" : [ {
			"name" : "Angularjs",
			"second_chield" : []
		}, {
			"name" : "Vue js",
			"second_chield" : []
		} ]
	}, {
		"name" : "Server Side",
		"second_chield" : [ {
			"name" : "PHP",
			"second_chield" : []
		}, {
			"name" : "Laravel",
			"second_chield" : []
		} ]
	}, {
		"name" : "Google",
		"second_chield" : [ {
			"name" : "Youtube",
			"second_chield" : []
		}, {
			"name" : "Analytics",
			"second_chield" : []
		} ]
	} ];
	$scope.all_programdata = [];

	$scope.$watch('query_str', function() {
		if ($scope.query_str == null || $scope.query_str.length == 0) {
			$scope.display_filterdata = false;
		} else {
			$scope.all_programdata = [];
			var temp = angular.copy($scope.postData);
			$scope.convert_lower_str = $scope.query_str
					.toLowerCase();
			$scope.filterResources(temp);
			$scope.display_filterdata = true;
			$scope.all_programdata = temp;
		}
	});

	$scope.filterResources = function(program_list) {
		for (var i = 0; i < program_list.length; i++) {
			if (program_list[i].second_chield != null
					&& program_list[i].second_chield.length > 0) {
				$scope.filterResources(program_list[i].second_chield);
				if (program_list[i].second_chield.length == 0) {
					var name = program_list[i].name.toLowerCase();
					if (!(name.indexOf($scope.convert_lower_str) >= 0)) {
						program_list.splice(i, 1);
						i--;
					}
				}
			} else {
				var name = program_list[i].name.toLowerCase();
				if (!(name.indexOf($scope.convert_lower_str) >= 0)) {
					program_list.splice(i, 1);
					i--;
				}
			}
		}
	};

}

Full Code : Filter JSON In Nested Using AngularJS Example

<!DOCTYPE html>
<html data-ng-app="liveApp" data-ng-controller="liveCtrl">
<head>
<title>Filter JSON In Nested Using AngularJS Example</title>
<script src="angular.min.js"></script>
<style>
.live_panel {
 margin:auto;
 width:800px;
}
</style>
</head>
<body>

	<div class="live_panel">
		<script type="text/ng-template" id="live_program_list_render.html">
            <li>{{data.name}}</li>
            <li data-ng-if="data.second_chield.length > 0">
               <ul data-ng-repeat="data in data.second_chield" data-ng-include="live_program_list_render.html"></ul>
            </li>
        </script>
		<div>Enter Programing text to filter data</div>
		<div>
			<input data-ng-model="query_str" placeholder="Enter Text here" />
		</div>
		<div>
			<ul data-ng-repeat="data in postData" data-ng-hide="display_filterdata">
				<li>{{data.name}}
					<div data-ng-if="data.second_chield.length > 0">
						<ul data-ng-repeat="data in data.second_chield"
							data-ng-include="'live_program_list_render.html'"></ul>
					</div>
				</li>
			</ul>
			<ul data-ng-repeat="data in all_programdata"
				data-ng-show="display_filterdata">
				<li>{{data.name}}
					<div data-ng-if="data.second_chield.length > 0">
						<ul data-ng-repeat="data in data.second_chield"
							data-ng-include="'live_program_list_render.html'"></ul>
					</div>
				</li>
			</ul>
		</div>
	</div>
	<script>
var liveApp = angular.module('liveApp', []);

function liveCtrl($scope) {
	$scope.postData = [ {
		"name" : "Java Script",
		"second_chield" : [ {
			"name" : "Angularjs",
			"second_chield" : []
		}, {
			"name" : "Vue js",
			"second_chield" : []
		} ]
	}, {
		"name" : "Server Side",
		"second_chield" : [ {
			"name" : "PHP",
			"second_chield" : []
		}, {
			"name" : "Laravel",
			"second_chield" : []
		} ]
	}, {
		"name" : "Google",
		"second_chield" : [ {
			"name" : "Youtube",
			"second_chield" : []
		}, {
			"name" : "Analytics",
			"second_chield" : []
		} ]
	} ];
	$scope.all_programdata = [];

	$scope.$watch('query_str', function() {
		if ($scope.query_str == null || $scope.query_str.length == 0) {
			$scope.display_filterdata = false;
		} else {
			$scope.all_programdata = [];
			var temp = angular.copy($scope.postData);
			$scope.convert_lower_str = $scope.query_str
					.toLowerCase();
			$scope.filterResources(temp);
			$scope.display_filterdata = true;
			$scope.all_programdata = temp;
		}
	});

	$scope.filterResources = function(program_list) {
		for (var i = 0; i < program_list.length; i++) {
			if (program_list[i].second_chield != null
					&& program_list[i].second_chield.length > 0) {
				$scope.filterResources(program_list[i].second_chield);
				if (program_list[i].second_chield.length == 0) {
					var name = program_list[i].name.toLowerCase();
					if (!(name.indexOf($scope.convert_lower_str) >= 0)) {
						program_list.splice(i, 1);
						i--;
					}
				}
			} else {
				var name = program_list[i].name.toLowerCase();
				if (!(name.indexOf($scope.convert_lower_str) >= 0)) {
					program_list.splice(i, 1);
					i--;
				}
			}
		}
	};

}
	</script>
</body>
</html>

Example

READ :  AngularJS Grid CRUD Example - Angular UI Grid

I hope you have Got What is Angularjs Filter Nested JSON Hierarchical Treeview in Controller And how it works.I would Like to have FeadBack From My Blog(infinityknow.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(infinityknow.com) Are Most Always Welcome.

Add a Comment

Your email address will not be published. Required fields are marked *