Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #740 - fixed colgroup vs thead bug #752

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 41 additions & 32 deletions src/scripts/ngTableController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @license New BSD License <http://creativecommons.org/licenses/BSD/>
*/

(function(){
(function () {
/**
* @ngdoc object
* @name ngTableController
Expand All @@ -16,7 +16,7 @@
*/
angular.module('ngTable').controller('ngTableController', ['$scope', 'NgTableParams', '$timeout', '$parse', '$compile', '$attrs', '$element',
'ngTableColumn', 'ngTableEventsChannel',
function($scope, NgTableParams, $timeout, $parse, $compile, $attrs, $element, ngTableColumn, ngTableEventsChannel) {
function ($scope, NgTableParams, $timeout, $parse, $compile, $attrs, $element, ngTableColumn, ngTableEventsChannel) {
var isFirstTimeLoad = true;
$scope.$filterRow = {};
$scope.$loading = false;
Expand All @@ -29,15 +29,15 @@
}
$scope.params.settings().$scope = $scope;

var delayFilter = (function() {
var delayFilter = (function () {
var timer = 0;
return function(callback, ms) {
return function (callback, ms) {
$timeout.cancel(timer);
timer = $timeout(callback, ms);
};
})();

function onDataReloadStatusChange (newStatus/*, oldStatus*/) {
function onDataReloadStatusChange(newStatus /*, oldStatus*/ ) {
if (!newStatus || $scope.params.hasErrorState()) {
return;
}
Expand Down Expand Up @@ -66,7 +66,7 @@
// CRITICAL: the watch must be for reference and NOT value equality; this is because NgTableParams maintains
// the current data page as a field. Checking this for value equality would be terrible for performance
// and potentially cause an error if the items in that array has circular references
$scope.$watch('params', function(newParams, oldParams){
$scope.$watch('params', function (newParams, oldParams) {
if (newParams === oldParams || !newParams) {
return;
}
Expand All @@ -87,14 +87,23 @@

// $element.find('> thead').length === 0 doesn't work on jqlite
var theadFound = false;
angular.forEach($element.children(), function(e) {
var colgroupFound = false;
angular.forEach($element.children(), function (e, i) {
if (e.tagName === 'THEAD') {
theadFound = true;
}
if (e.tagName === 'COLGROUP') {
colgroupFound = true;
}
});
if (!theadFound) {
headerTemplate = angular.element(document.createElement('thead')).attr('ng-include', 'templates.header');
$element.prepend(headerTemplate);
if (!colgroupFound) {
$element.prepend(headerTemplate);
} else {
console.log(headerTemplate);
$element.find('colgroup').after(headerTemplate);
}
}
var paginationTemplate = angular.element(document.createElement('div')).attr({
'ng-table-pagination': 'params',
Expand Down Expand Up @@ -122,7 +131,7 @@
if ((angular.isObject(result) && (angular.isObject(result.promise) || angular.isFunction(result.then)))) {
var pData = angular.isFunction(result.then) ? result : result.promise;
delete $column.filterData;
return pData.then(function(data) {
return pData.then(function (data) {
// our deferred can eventually return arrays, functions and objects
if (!angular.isArray(data) && !angular.isFunction(data) && !angular.isObject(data)) {
// if none of the above was found - we just want an empty array
Expand All @@ -140,7 +149,7 @@

this.buildColumns = function (columns) {
var result = [];
(columns || []).forEach(function(col){
(columns || []).forEach(function (col) {
result.push(ngTableColumn.buildColumn(col, $scope, result));
});
return result
Expand All @@ -158,7 +167,7 @@
}
};

this.setupBindingsToInternalScope = function(tableParamsExpr){
this.setupBindingsToInternalScope = function (tableParamsExpr) {

// note: this we're setting up watches to simulate angular's isolated scope bindings

Expand All @@ -178,75 +187,75 @@
setupGroupRowBindingsToInternalScope();
};

function setupFilterRowBindingsToInternalScope(){
function setupFilterRowBindingsToInternalScope() {
if ($attrs.showFilter) {
$scope.$parent.$watch($attrs.showFilter, function(value) {
$scope.$parent.$watch($attrs.showFilter, function (value) {
$scope.show_filter = value;
});
} else {
$scope.$watch(hasVisibleFilterColumn, function(value){
$scope.$watch(hasVisibleFilterColumn, function (value) {
$scope.show_filter = value;
})
}

if ($attrs.disableFilter) {
$scope.$parent.$watch($attrs.disableFilter, function(value) {
$scope.$parent.$watch($attrs.disableFilter, function (value) {
$scope.$filterRow.disabled = value;
});
}
}

function setupGroupRowBindingsToInternalScope(){
function setupGroupRowBindingsToInternalScope() {
$scope.$groupRow = {};
if ($attrs.showGroup) {
var showGroupGetter = $parse($attrs.showGroup);
$scope.$parent.$watch(showGroupGetter, function(value) {
$scope.$parent.$watch(showGroupGetter, function (value) {
$scope.$groupRow.show = value;
});
if (showGroupGetter.assign){
if (showGroupGetter.assign) {
// setup two-way databinding thus allowing ngTableGrowRow to assign to the showGroup expression
$scope.$watch('$groupRow.show', function(value) {
$scope.$watch('$groupRow.show', function (value) {
showGroupGetter.assign($scope.$parent, value);
});
}
} else{
$scope.$watch('params.hasGroup()', function(newValue) {
} else {
$scope.$watch('params.hasGroup()', function (newValue) {
$scope.$groupRow.show = newValue;
});
}
}

function getVisibleColumns(){
return ($scope.$columns || []).filter(function(c){
function getVisibleColumns() {
return ($scope.$columns || []).filter(function (c) {
return c.show($scope);
});
}

function hasVisibleFilterColumn(){
function hasVisibleFilterColumn() {
if (!$scope.$columns) return false;

return some($scope.$columns, function($column){
return some($scope.$columns, function ($column) {
return $column.show($scope) && $column.filter($scope);
});
}

function some(array, predicate){
function some(array, predicate) {
var found = false;
for (var i = 0; i < array.length; i++) {
var obj = array[i];
if (predicate(obj)){
if (predicate(obj)) {
found = true;
break;
}
}
return found;
}

function commonInit(){
function commonInit() {
ngTableEventsChannel.onAfterReloadData(bindDataToScope, $scope, isMyPublisher);
ngTableEventsChannel.onPagesChanged(bindPagesToScope, $scope, isMyPublisher);

function bindDataToScope(params, newDatapage){
function bindDataToScope(params, newDatapage) {
var visibleColumns = getVisibleColumns();
if (params.hasGroup()) {
$scope.$groups = newDatapage || [];
Expand All @@ -257,15 +266,15 @@
}
}

function bindPagesToScope(params, newPages){
function bindPagesToScope(params, newPages) {
$scope.pages = newPages
}

function isMyPublisher(publisher){
function isMyPublisher(publisher) {
return $scope.params === publisher;
}
}

commonInit();
}]);
})();
})();