'use strict';
angular.module('myApp.smart', ['ngRoute'])
.config(['$routeProvider',function($routeProvider) {
$routeProvider.when('/smart', {
templateUrl:'smart-table/index.html',
controller:'mainCtrl'
});
}]);
(function(ng) {
angular.module('myApp', ['smart-table','ui.bootstrap'])
.controller('mainCtrl', ['$scope',function($scope) {
var
nameList= ['Pierre','Pol','Jacques','Robert','Elisa'],
familyName= ['Dupont','Germain','Delcourt','bjip','Menez'],
nationList= ['USA','France','Germany'],
educationList= ['Doctorate','Master','Bachelor','High school'];
functioncreateRandomItem() {
var
firstName=nameList[Math.floor(Math.random() *5)],
lastName=familyName[Math.floor(Math.random() *5)],
nationality=nationList[Math.floor(Math.random() *3)],
education=educationList[Math.floor(Math.random() *4)];
return{
firstName:firstName,
lastName:lastName,
nationality:nationality,
education:education
};
}
$scope.itemsByPage=15;
$scope.collection= [];
$scope.displayed= [].concat($scope.collection);
for(varj=0;j<200;j++) {
$scope.collection.push(createRandomItem());
}
}])
.directive('stSelectDistinct', [function() {
return{
restrict:'E',
require:'^stTable',
scope: {
collection:'=',
predicate:'@',
predicateExpression:'='
},
template:'',
link:function(scope, element, attr, table) {
vargetPredicate=function() {
varpredicate= scope.predicate;
if(!predicate&& scope.predicateExpression) {
predicate= scope.predicateExpression;
}
returnpredicate;
}
scope.$watch('collection',function(newValue) {
varpredicate=getPredicate();
if(newValue) {
vartemp= [];
scope.distinctItems= ['All'];
angular.forEach(scope.collection,function(item) {
varvalue= item[predicate];
if(value&&value.trim().length>0&&temp.indexOf(value) === -1) {
temp.push(value);
}
});
temp.sort();
scope.distinctItems= scope.distinctItems.concat(temp);
scope.selectedOption= scope.distinctItems[0];
scope.optionChanged(scope.selectedOption);
}
},true);
scope.optionChanged=function(selectedOption) {
varpredicate=getPredicate();
varquery= {};
query.distinct= selectedOption;
if(query.distinct==='All') {
query.distinct='';
}
table.search(query,predicate);
};
}
}
}])
.directive('stSelectMultiple', [function() {
return{
restrict:'E',
require:'^stTable',
scope: {
collection:'=',
predicate:'@',
predicateExpression:'='
},
templateUrl:'stSelectMultiple.html',
link:function(scope, element, attr, table) {
scope.dropdownLabel='';
scope.filterChanged=filterChanged;
initialize();
functioninitialize() {
bindCollection(scope.collection);
}
functiongetPredicate() {
varpredicate= scope.predicate;
if(!predicate&& scope.predicateExpression) {
predicate= scope.predicateExpression;
}
returnpredicate;
}
functiongetDropdownLabel() {
varallCount= scope.distinctItems.length;
//获取要查的列表
varselected=getSelectedOptions();
if(allCount===selected.length||selected.length===0) {
return'All';
}
if(selected.length===1) {
returnselected[0];
}
returnselected.length+' items';
}
functiongetSelectedOptions() {
varselectedOptions= [];
angular.forEach(scope.distinctItems,function(item) {
if(item.selected) {
selectedOptions.push(item.value);
}
});
returnselectedOptions;
}
functionbindCollection(collection) {
varpredicate=getPredicate();
vardistinctItems= [];
angular.forEach(collection,function(item) {
varvalue= item[predicate];
fillDistinctItems(value,distinctItems);
});
distinctItems.sort(function(obj, other) {
if(obj.value> other.value) {
return1;
}else if(obj.value< other.value) {
return-1;
}
return0;
});
//获取筛选的列表
scope.distinctItems=distinctItems;
filterChanged();
}
functionfilterChanged() {
scope.dropdownLabel=getDropdownLabel();
varpredicate=getPredicate();
varquery= {
matchAny: {}
};
//获取要筛选的项
query.matchAny.items=getSelectedOptions();
varnumberOfItems=query.matchAny.items.length;
if(numberOfItems===0||numberOfItems=== scope.distinctItems.length) {
query.matchAny.all=true;
}else{
query.matchAny.all=false;
}
table.search(query,predicate);
}
functionfillDistinctItems(value, distinctItems) {
if(value && value.trim().length>0&& !findItemWithValue(distinctItems, value)) {
distinctItems.push({
value: value,
selected:true
});
}
}
functionfindItemWithValue(collection, value) {
varfound=_.find(collection,function(item) {
returnitem.value=== value;
});
returnfound;
}
}
}
}])
.filter('customFilter', ['$filter',function($filter) {
varfilterFilter= $filter('filter');
varstandardComparator=functionstandardComparator(obj, text) {
text = (''+ text).toLowerCase();
return(''+ obj).toLowerCase().indexOf(text) > -1;
};
return functioncustomFilter(array, expression) {
functioncustomComparator(actual, expected) {
varisBeforeActivated= expected.before;
varisAfterActivated= expected.after;
varisLower= expected.lower;
varisHigher= expected.higher;
varhigherLimit;
varlowerLimit;
varitemDate;
varqueryDate;
if(ng.isObject(expected)) {
//exact match
if(expected.distinct) {
if(!actual || actual.toLowerCase() !== expected.distinct.toLowerCase()) {
return false;
}
return true;
}
//matchAny
if(expected.matchAny) {
if(expected.matchAny.all) {
return true;
}
if(!actual) {
return false;
}
for(vari=0;i< expected.matchAny.items.length;i++) {
if(actual.toLowerCase() === expected.matchAny.items[i].toLowerCase()) {
return true;
}
}
return false;
}
//date range
if(expected.before|| expected.after) {
try{
if(isBeforeActivated) {
higherLimit= expected.before;
itemDate=newDate(actual);
queryDate=newDate(higherLimit);
if(itemDate>queryDate) {
return false;
}
}
if(isAfterActivated) {
lowerLimit= expected.after;
itemDate=newDate(actual);
queryDate=newDate(lowerLimit);
if(itemDate
return false;
}
}
return true;
}catch(e) {
return false;
}
}else if(isLower||isHigher) {
//number range
if(isLower) {
higherLimit= expected.lower;
if(actual >higherLimit) {
return false;
}
}
if(isHigher) {
lowerLimit= expected.higher;
if(actual
return false;
}
}
return true;
}
//etc
return true;
}
returnstandardComparator(actual, expected);
}
varoutput=filterFilter(array, expression,customComparator);
returnoutput;
};
}]);
})(angular);