ng2-bs-table
Version:
Boostrap table view for angular 2.
120 lines • 5.6 kB
JavaScript
/*
* Example use
* Basic Array of single type: *ngFor="let todo of todoService.todos | orderBy : '-'"
* Multidimensional Array Sort on single column: *ngFor="let todo of todoService.todos | orderBy : ['-status']"
* Multidimensional Array Sort on multiple columns: *ngFor="let todo of todoService.todos | orderBy : ['status', '-title']"
*/
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
var OrderByPipe = OrderByPipe_1 = (function () {
function OrderByPipe() {
this.value = [];
}
OrderByPipe._orderByComparator = function (a, b) {
if (a === null || typeof a === 'undefined')
a = 0;
if (b === null || typeof b === 'undefined')
b = 0;
if ((isNaN(parseFloat(a)) || !isFinite(a)) || (isNaN(parseFloat(b)) || !isFinite(b))) {
//Isn't a number so lowercase the string to properly compare
if (a.toLowerCase() < b.toLowerCase())
return -1;
if (a.toLowerCase() > b.toLowerCase())
return 1;
}
else {
//Parse strings as numbers to compare properly
if (parseFloat(a) < parseFloat(b))
return -1;
if (parseFloat(a) > parseFloat(b))
return 1;
}
return 0; //equal each other
};
OrderByPipe.prototype.transform = function (input, config) {
if (config === void 0) { config = '+'; }
//invalid input given
if (!input)
return input;
//make a copy of the input's reference
this.value = input.slice();
var value = this.value;
if (!Array.isArray(value))
return value;
if (!Array.isArray(config) || (Array.isArray(config) && config.length == 1)) {
var propertyToCheck = !Array.isArray(config) ? config : config[0];
var desc_1 = propertyToCheck.substr(0, 1) == '-';
//Basic array
if (!propertyToCheck || propertyToCheck == '-' || propertyToCheck == '+') {
return !desc_1 ? value.sort() : value.sort().reverse();
}
else {
var property_1 = propertyToCheck.substr(0, 1) == '+' || propertyToCheck.substr(0, 1) == '-'
? propertyToCheck.substr(1)
: propertyToCheck;
return value.sort(function (a, b) {
var aValue = a[property_1];
var bValue = b[property_1];
var propertySplit = property_1.split('.');
if (typeof aValue === 'undefined' && typeof bValue === 'undefined' && propertySplit.length > 1) {
aValue = a;
bValue = b;
for (var j = 0; j < propertySplit.length; j++) {
aValue = aValue[propertySplit[j]];
bValue = bValue[propertySplit[j]];
}
}
return !desc_1
? OrderByPipe_1._orderByComparator(aValue, bValue)
: -OrderByPipe_1._orderByComparator(aValue, bValue);
});
}
}
else {
//Loop over property of the array in order and sort
return value.sort(function (a, b) {
for (var i = 0; i < config.length; i++) {
var desc = config[i].substr(0, 1) == '-';
var property = config[i].substr(0, 1) == '+' || config[i].substr(0, 1) == '-'
? config[i].substr(1)
: config[i];
var aValue = a[property];
var bValue = b[property];
var propertySplit = property.split('.');
if (typeof aValue === 'undefined' && typeof bValue === 'undefined' && propertySplit.length > 1) {
aValue = a;
bValue = b;
for (var j = 0; j < propertySplit.length; j++) {
aValue = aValue[propertySplit[j]];
bValue = bValue[propertySplit[j]];
}
}
var comparison = !desc
? OrderByPipe_1._orderByComparator(aValue, bValue)
: -OrderByPipe_1._orderByComparator(aValue, bValue);
//Don't return 0 yet in case of needing to sort by next property
if (comparison != 0)
return comparison;
}
return 0; //equal each other
});
}
};
return OrderByPipe;
}());
OrderByPipe = OrderByPipe_1 = __decorate([
core_1.Pipe({ name: 'orderBy', pure: false })
], OrderByPipe);
exports.OrderByPipe = OrderByPipe;
exports.ORDERBY_PROVIDERS = [
OrderByPipe
];
var OrderByPipe_1;
//# sourceMappingURL=order-by.pipe.js.map