Giter Club home page Giter Club logo

treetable's Introduction

Dynamic Table Builder Directive for Angular

Just pass your json data to the directive and build your table in DOM.

DEMO

Usage

  • Include the follwing directive in your angular application
	app.directive('treeTable', function ($compile) {
		return {
			restrict: 'A',
			scope: {
				data: '=',

			},
		link: function (scope, element, attributes) {
		var data=scope.data;
		scope.table =  '';
			var n=1;

			function buildTable(tableData){
				var row='<table class="table table-bordered">';
				for(i=0;i<tableData.length;i++){
					if(i==0){
						row+='<thead class="table-header">';
						for(x in tableData[i]){
							console.log(x,x.indexOf('_'));
						if(x!="data" && x.indexOf('_') == -1){
							row+='<td><strong>'+ x +'</strong></td>';
						}
						else if(x!="data" && x.indexOf('_') >-1){
							var header=x.split('_'),
								j=0;
							row+='<td><strong>';
							for(j=0;j<header.length;j++){
								row+= header[j] +' ';
							}
							row+='</strong></td>';
						}
					}
					
					row+='</thead><tbody>';
					}
						row+='<tr ng-Mouseover="makeClick()">';
						for(x in tableData[i]){
							if(x!="data"){
								console.log(typeof tableData[i][x]);
								if(typeof tableData[i][x]=='number')
									row+='<td cell-highlighter value='+tableData[i][x]+'>';
								else
									row+='<td>';
								row+=tableData[i][x] +'</td>';
							}
					}
					row+='</tr>';

					scope.table+=row;
					row='';
					if(angular.isDefined(tableData[i].data) && tableData[i].data.length>0){
						console.log("before>>>",scope.table);
						scope.table = scope.table.slice(0,scope.table.length-10);
						console.log("after>>>",scope.table);
						scope.table+='<span class="fa fa-chevron-circle-down pull-right"></span></td></tr>';
						var colspanValue=Object.keys(tableData[i]).length;
						scope.table+='<tr id=row><td colspan='+colspanValue+'>';
						var temp=i;
						n++;
						buildTable(tableData[i].data);
						scope.table+='</td></tr>';
						i=temp;
					}
				}
				scope.table+='</tbody></table>';
			};
				if(data.length>0){
					buildTable(data);
				}
						var elmnt = $compile(scope.table)( scope );
		
		  element.append( elmnt );
		  scope.makeClick=function(value){
			$('tr').on('click',function(event){
				console.log('1111');
				var nextNode = $(this).next();
				if(nextNode.length){
					var text=$(this).next().html();
					if(text.indexOf('colspan')> -1){
						$(this).next().toggle();
						$(this).children().children().toggleClass("fa-chevron-circle-down");
						$(this).children().children().toggleClass("fa-chevron-circle-up");
					}
				}
				//event.stopPropagation();
			});
		  };

		}
	}
});
  • Now include the directive in your DOM to generate the table
<div tree-table data="data">
</div>
  • Remember to pass the JSON in data.
    Eg: $scope.data=[
		{"First_Name": "Root","Last_Name":"More","Phone":4454545454,
		 "data":[
		 {"First_Name": "Root","Last_Name":"More","Phone":4454545454,"data":[]},
		 {"First_Name": "Andy","Last_Name":"Murray","Phone":121212121},
		 {"First_Name": "Rafael","Last_Name":"Nadal","Phone":6565656656,"data":[
		 {"First_Name": "Root","Last_Name":"More","Phone":4454545454,"data":[]},
		 {"First_Name": "Andy","Last_Name":"Murray","Phone":121212121},
		 {"First_Name": "Rafael","Last_Name":"Nadal","Phone":6565656656},
		{"First_Name": "Ranjith","Last_Name":"Prabhu","Phone":98989898989}]},
		{"First_Name": "Ranjith","Last_Name":"Prabhu","Phone":98989898989}]},
		{"First_Name": "Andy","Last_Name":"Murray","Phone":121212121},
		{"First_Name": "Rafael","Last_Name":"Nadal","Phone":6565656656},
		{"First_Name": "Ranjith","Last_Name":"Prabhu","Phone":98989898989},
		];
For the nested tables, pass the values inside the data array in each object.
  • If required use the following directive to highlight the cell value
	app.directive('cellHighlighter', function ($compile) {
		return {
			restrict: 'A',
			scope: {
				value: '=',
			},
			link: function (scope, element, attributes) {
				if(scope.value>4454545454){
					element.addClass('bg-danger');
				}
		}
		}
	});
To hightlight the cell add any css class in the line,
```
element.addClass('bg-danger');
```

Features

  • Easy to build the table
  • Dynamic building of nested tables
  • Works in All Modern Browsers

About Author

Am Ranjithprabhu.K , a passionate in web app development.

License

Released under the MIT license.

treetable's People

Contributors

ranjithprabhuk avatar

Stargazers

ecomarine avatar  avatar cfpperche avatar Balaji avatar

Watchers

James Cloos avatar  avatar

Forkers

murugavele

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.