Angular dataTable multicolumn filter

angularIn my project i got a requirement that client need to search column wise with the default common search. I checked it in google, found column filter in footer row. But I need it in header itself. Based on that example created one.

 

HTML

<!DOCTYPE html>
<html>

  <head>
    		<link rel="stylesheet" href="style/jquery.dataTables.css" />
  </head>

  <body ng-app="datatablesSampleApp">
<div ng-controller="simpleCtrl" class="code"  style="width: 500px">
<table datatable="" dt-options="dtOptions" dt-columns="dtColumns">
<thead>
<tr>
<th>ID
                <input type="text" ng-model="search.id" ng-change="searchData(search.id,0)" ng-click="blockSorting($event)" /></th>
<th>FirstName
                <input type="text" ng-model="search.firstName" ng-change="searchData(search.firstName,1)"  ng-click="blockSorting($event)"/></th>
<th>LastName</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="person in data">
<td>{{ person.id }}</td>
<td>{{ person.firstName }}</td>
<td>{{ person.lastName }}</td>
</tr>
</tbody>
</table>
</div>
<script src="script/jquery-1.10.1.min.js"></script>
    <script src="script/jquery.dataTables.js"></script>
    <script src="http://code.angularjs.org/1.2.15/angular.js"></script>
    <script src="script/angular-resource.js"></script>
    <script  src="angular-datatables.min.js"></script>
    <script src="script.js"></script>
  </body>

</html>

JS

(function (angular) {
    'use strict';
    angular.module('datatablesSampleApp', ['datatables']).
        controller('simpleCtrl', function ($scope, DTOptionsBuilder, DTColumnBuilder) {
            $scope.data = [{
                "id": 860,
                "firstName": "Superman",
                "lastName": "Yoda"
            }, {
                "id": 870,
                "firstName": "Foo",
                "lastName": "Whateveryournameis"
            }, {
                "id": 590,
                "firstName": "Toto",
                "lastName": "Titi"
            }];
            $scope.dtOptions = DTOptionsBuilder.newOptions().withPaginationType('full_numbers');
            $scope.searchData = function (searchText, index) {
                $scope.table.column(index).search(searchText).draw();
            };
            $scope.$on('event:dataTableLoaded', function (event, loadedDT) {
                $scope.table = loadedDT.DataTable;
            });
            $scope.blockSorting = function($event){
                $event.preventDefault();
                $event.stopPropagation();
            }
        });
})(angular);

Output
angular multi column filter

Advertisements

Bind angular scope to dynamically added html

angularSome times we need to integrate html contents to our angular application. some times we create contents from jQuery and integrate it to our application. like kendo UI in jQuery style, jQuery dataTables etc.
At that time the following snippet will useful.

<!DOCTYPE html>
<html lang="en">

<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.min.js"></script>
</head>

<body ng-app="app">
<div ng-controller="ctrl" id="dvController">
        <test-dir></test-dir>
        <button onclick="addTemplate1()">Add template 1</button>
        <button onclick="addTemplate2()">Add template 2</button>
<div id="dvContainer"></div>
</div>
<script>
                         function addTemplate1() {
                             var template = "<test-dir></test-dir>";
                             $("#dvContainer").html(template);
                             bindAngular();
                         }

                         function addTemplate2() {
                             var template = "FirstName : {{FirstName}}
LastName : {{LastName}}";
                             $("#dvContainer").html(template);
                             bindAngular();
                         }
                         function bindAngular() {
                             var $controllerElement = angular.element('#dvController');
                             var $appElement = angular.element('body');

                             // compiling and applying / digesting the scope.
                             $appElement.injector().invoke(function ($compile) {
                                 var scope = $controllerElement.scope();
                                 $compile($controllerElement)(scope);
                                 scope.$apply();
                             });
                         }
                         angular.module('app', []).controller('ctrl', function ($scope) {
                             $scope.FirstName = "Musthaan";
                             $scope.LastName = "Majeed";
                         }).directive('testDir', function () {
                             return {
                                 template: "
<h1>From dir</h1>
"
                             };
                         })
    </script>
</body>

</html>

Error: [jqLite:nosel] Looking up elements via selectors is not supported by jqLite

One day i am just playing with angular with jQuery. At that time I got an error like

Error: [jqLite:nosel] Looking up elements via selectors is not supported by jqLite
My html looks like,



<head>    

    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>

Because while initializing time angular doesn’t knows that there is jQuery.

Just swap the order and fix this issue.



<head>    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.min.js"></script>
</head>

The JSON request was too large to be deserialized.

I am working on a list and it has a bulk update through api. I am tested with upto 30 records and its working fine on my dev system. whilie update to QA they added a lot of records to update at that time I got an error The JSON request was too large to be deserialized

JSON request was too large
Problem is I am sending data as JSON as MVC’s default maximum Json deserializer members count is 1000. So I changed it in web.config.

<appSettings>
    <add key="aspnet:MaxJsonDeserializerMembers" value="150000" />
</appSettings>

Lazy Load Images with jQuery

Lazy Load is delays loading of images in long web pages. Images outside of viewport are not loaded until user scrolls to them. This is opposite of image preloading.

Using Lazy Load on long web pages will make the page load faster. In some cases it can also help to reduce server load.

Lazy Load Images with jQuery

Here is a basic sample code for lazy load.


<!DOCTYPE html>
<html lang="en">
<head>
	
	<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
	<script type="text/javascript">
		function elementInViewport(el) {
			var top = el.offsetTop;
			var left = el.offsetLeft;
			var width = el.offsetWidth;
			var height = el.offsetHeight;

			while(el.offsetParent) {
				el = el.offsetParent;
				top += el.offsetTop;
				left += el.offsetLeft;
			}

			return (
				top >= window.pageYOffset &&
				left >= window.pageXOffset &&
				(top + height) <= (window.pageYOffset + window.innerHeight) &&
				(left + width) <= (window.pageXOffset + window.innerWidth)
				);
		}
		 
		function loadimg(){

			$($("img.lazy")).each(function(ind,img){ 
				if (elementInViewport(img)){ 
					var image = new Image();
					image.src = $(img).data("src") ;	
					image.onload=function(){
						$(img).removeClass("lazy")
							.addClass("img")
							.attr("src",image.src);				
					};
				}
			});
		}
		$(document).ready(function(){
			$(document).on( "scroll", function(){
				loadimg();
			} );
		});
	</script>
	<style type="text/css">
		.img{
			 
			border: solid 1px #DDD; 
			width: 150px;
		}
		.lazy{
			display: block;
			float: left;
			border: solid 1px #DDD;
			height: 300px;
			width: 150px;
			background-image: url('spinner.gif');
			background-position: center center;
			background-repeat: no-repeat;
			background-size: 36px 36px;
		}
	</style>
</head>
<body>

	<img src="" data-src="http://example.com/images/4c.jpg" class="lazy" />
	<img src="" data-src="http://example.com/images/6.png" class="lazy" />
	<img src="" data-src="http://example.com/images/5.png" class="lazy" />
	<img src="" data-src="http://example.com/images/7.png" class="lazy" />
	<img src="" data-src="http://example.com/images/4c.jpg" class="lazy" />
	<img src="" data-src="http://example.com/images/6.png" class="lazy" />
	<img src="" data-src="http://example.com/images/5.png" class="lazy" />
	<img src="" data-src="http://example.com/images/7.png" class="lazy" />
	<img src="" data-src="http://example.com/images/4c.jpg" class="lazy" />
	<img src="" data-src="http://example.com/images/6.png" class="lazy" />
	<img src="" data-src="http://example.com/images/5.png" class="lazy" />
	<img src="" data-src="http://example.com/images/7.png" class="lazy" />
	<img src="" data-src="http://example.com/images/4c.jpg" class="lazy" />
	<img src="" data-src="http://example.com/images/6.png" class="lazy" />
	<img src="" data-src="http://example.com/images/5.png" class="lazy" />
	<img src="" data-src="http://example.com/images/7.png" class="lazy" />
	<img src="" data-src="http://example.com/images/4c.jpg" class="lazy" />
	<img src="" data-src="http://example.com/images/6.png" class="lazy" />
	<img src="" data-src="http://example.com/images/5.png" class="lazy" />
	<img src="" data-src="http://example.com/images/7.png" class="lazy" />
	<img src="" data-src="http://example.com/images/4c.jpg" class="lazy" />
	<img src="" data-src="http://example.com/images/6.png" class="lazy" />
	<img src="" data-src="http://example.com/images/5.png" class="lazy" />
	<img src="" data-src="http://example.com/images/7.png" class="lazy" />
	<img src="" data-src="http://example.com/images/4c.jpg" class="lazy" />
	<img src="" data-src="http://example.com/images/6.png" class="lazy" />
	<img src="" data-src="http://example.com/images/5.png" class="lazy" />
	<img src="" data-src="http://example.com/images/7.png" class="lazy" />
	<img src="" data-src="http://example.com/images/4c.jpg" class="lazy" />
	<img src="" data-src="http://example.com/images/6.png" class="lazy" />
	<img src="" data-src="http://example.com/images/5.png" class="lazy" />
	<img src="" data-src="http://example.com/images/7.png" class="lazy" />
	<img src="" data-src="http://example.com/images/4c.jpg" class="lazy" />
	<img src="" data-src="http://example.com/images/6.png" class="lazy" />
	<img src="" data-src="http://example.com/images/5.png" class="lazy" />
	<img src="" data-src="http://example.com/images/7.png" class="lazy" />
	<img src="" data-src="http://example.com/images/4c.jpg" class="lazy" />
	<img src="" data-src="http://example.com/images/6.png" class="lazy" />
	<img src="" data-src="http://example.com/images/5.png" class="lazy" />
	<img src="" data-src="http://example.com/images/7.png" class="lazy" />
</body>
</html>

Attribute Contains Selector in JQuery

This is the most generous of the jQuery attribute selectors that match against a value. It will select an element if the selector’s string appears anywhere within the element’s attribute value. Compare this selector with the Attribute Contains Word selector (e.g. [attr~=”word”]), which is more appropriate in many cases.

<html lang="en">

<head>
    <meta charset="utf-8">
    <title>attributeContains demo</title>
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>

<body>
    <input name="sub1">
    <input name="sub2">
    <input name="sub2">
    <input name="book">
    <script>
        $("input[name*='sub']").val("this box for book!");
    </script>
</body>

</html>

In this codeĀ $( “input[name*=’sub’]” ) select first three textboxes.Becouse both contains “sub” in name attribute.