Skip to content

Commit

Permalink
feat($browser): Added events to provide the data after it is filtered…
Browse files Browse the repository at this point in the history
… and after it is sorted.

Added new events to the ngTableEventsChannel that fire when the ngTableDefaultGetData filters and

sorts the data. This is usefull when you want to try to export only the filtered data or when you

want to make some real time statistics over the data that is beeing filtered.
  • Loading branch information
dburner committed Nov 24, 2016
1 parent 66f8761 commit 22ed10f
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/core/ngTableDefaultGetData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import * as ng1 from 'angular';
import { IDefaultGetDataProvider, IDefaultGetData, IFilterFunc, INgTableParams } from './public-interfaces';
import { IDefaultGetDataProvider, IDefaultGetData, IFilterFunc, INgTableParams, IEventsChannel } from './public-interfaces';

/**
* Allows for the configuration of the ngTableDefaultGetData service.
Expand All @@ -26,22 +26,24 @@ import { IDefaultGetDataProvider, IDefaultGetData, IFilterFunc, INgTableParams }
export class ngTableDefaultGetDataProvider implements IDefaultGetDataProvider {
filterFilterName = 'filter';
sortingFilterName = 'orderBy';
$get: ($filter: ng1.IFilterService) => IDefaultGetData<any>;
$get: ($filter: ng1.IFilterService, ngTableEventsChannel: IEventsChannel) => IDefaultGetData<any>;
constructor() {
var provider = this;
this.$get = ngTableDefaultGetData;

ngTableDefaultGetData.$inject = ['$filter'];
ngTableDefaultGetData.$inject = ['$filter', 'ngTableEventsChannel'];

/**
* Implementation of the {@link IDefaultGetData IDefaultGetData} interface
*
* @ngdoc service
*/
function ngTableDefaultGetData<T>($filter: ng1.IFilterService): IDefaultGetData<T> {
function ngTableDefaultGetData<T>($filter: ng1.IFilterService, ngTableEventsChannel: IEventsChannel): IDefaultGetData<T> {

var defaultDataOptions = { applyFilter: true, applySort: true, applyPaging: true };

var self = this;

(getData as IDefaultGetData<T>).applyPaging = applyPaging;
(getData as IDefaultGetData<T>).getFilterFn = getFilterFn;
(getData as IDefaultGetData<T>).getOrderByFn = getOrderByFn;
Expand Down Expand Up @@ -96,7 +98,11 @@ export class ngTableDefaultGetDataProvider implements IDefaultGetDataProvider {
var options = ng1.extend({}, defaultDataOptions, params.settings().dataOptions);

var fData = options.applyFilter ? applyFilter(data, params) : data;
ngTableEventsChannel.publishAfterDataFiltered(self, params, fData);

var orderedData = options.applySort ? applySort(fData, params) : fData;
ngTableEventsChannel.publishAfterDataSorted(self, params,orderedData);

return options.applyPaging ? applyPaging(orderedData, params) : orderedData;
}

Expand Down
2 changes: 2 additions & 0 deletions src/core/ngTableEventsChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export function ngTableEventsChannel($rootScope: ng1.IRootScopeService): IEvents
events = addTableParamsEvent('afterReloadData', events);
events = addTableParamsEvent('datasetChanged', events);
events = addTableParamsEvent('pagesChanged', events);
events = addTableParamsEvent('afterDataFiltered', events);
events = addTableParamsEvent('afterDataSorted', events);
return events as IEventsChannel;

//////////
Expand Down
55 changes: 54 additions & 1 deletion src/core/public-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,18 @@ export interface IEventSelectorFunc {
export interface IPagesChangedListener {
(publisher: INgTableParams<any>, newPages: IPageButton[], oldPages: IPageButton[]): any
}
/**
* Signature of the event hander that is registered to receive the *afterDataFiltered* event
*/
export interface IAfterDataFilteredListener<T> {
(publisher: IDefaultGetData<T>, params: INgTableParams<T>, newData: DataResult<T>[] ): any
}
/**
* Signature of the event hander that is registered to receive the *afterDataSorted* event
*/
export interface IAfterDataSortedListener<T> {
(publisher: IDefaultGetData<T>, params: INgTableParams<T>, newData: DataResult<T>[] ): any
}

/**
* Signature of the function used to explicitly unregister an event handler so that it stops
Expand Down Expand Up @@ -647,9 +659,50 @@ export interface IEventsChannel {
* @return a unregistration function that when called will unregister the `listener`
*/
onPagesChanged<T>(listener: IPagesChangedListener, eventFilter?: EventSelector<T>): IUnregistrationFunc;
/**
* Subscribe to receive notification whenever a `ngTableDefaultGetData` instance filters data
* Optionally supply an `eventFilter` to restrict which events that should trigger the `listener` to be called.
*
* @param listener the function that will be called when the event fires
* @param scope the angular `$scope` that will limit the lifetime of the event subscription
* @param eventFilter either the specific `IDefaultGetData` instance you want to receive events for or a predicate function that should return true to receive the event
* @return a unregistration function that when called will unregister the `listener`
*/
onAfterDataFiltered<T>(listener: IAfterDataFilteredListener<T>, scope: IScope, eventFilter?: EventSelector<T> ): IUnregistrationFunc;
/**
* Subscribe to receive notification whenever a `ngTableDefaultGetData` instance filters data
* Optionally supply an `eventFilter` to restrict which events that should trigger the `listener` to be called.
*
* @param listener the function that will be called when the event fires
* @param eventFilter either the specific `IDefaultGetData` instance you want to receive events for or a predicate function that should return true to receive the event
* @return a unregistration function that when called will unregister the `listener`
*/
onAfterDataFiltered<T>(listener: IAfterDataFilteredListener<T>, eventFilter?: EventSelector<T> ): IUnregistrationFunc;
/**
* Subscribe to receive notification whenever a `ngTableDefaultGetData` instance orders data
* Optionally supply an `eventFilter` to restrict which events that should trigger the `listener` to be called.
*
* @param listener the function that will be called when the event fires
* @param scope the angular `$scope` that will limit the lifetime of the event subscription
* @param eventFilter either the specific `IDefaultGetData` instance you want to receive events for or a predicate function that should return true to receive the event
* @return a unregistration function that when called will unregister the `listener`
*/
onAfterDataSorted<T>(listener: IAfterDataSortedListener<T>, scope: IScope, eventFilter?: EventSelector<T> ): IUnregistrationFunc;
/**
* Subscribe to receive notification whenever a `ngTableDefaultGetData` instance orders data
* Optionally supply an `eventFilter` to restrict which events that should trigger the `listener` to be called.
*
* @param listener the function that will be called when the event fires
* @param eventFilter either the specific `IDefaultGetData` instance you want to receive events for or a predicate function that should return true to receive the event
* @return a unregistration function that when called will unregister the `listener`
*/
onAfterDataSorted<T>(listener: IAfterDataSortedListener<T>, eventFilter?: EventSelector<T> ): IUnregistrationFunc;

publishAfterCreated<T>(publisher: INgTableParams<T>): void;
publishAfterReloadData<T>(publisher: INgTableParams<T>, newData: T[], oldData: T[]): void;
publishDatasetChanged<T>(publisher: INgTableParams<T>, newDataset: T[], oldDataset: T[]): void;
publishPagesChanged<T>(publisher: INgTableParams<T>, newPages: IPageButton[], oldPages: IPageButton[]): void;
}
publishAfterDataFiltered<T>(publisher: IDefaultGetData<T>, params: INgTableParams<T>, newData: T[]): void;
publishAfterDataSorted<T>(publisher: IDefaultGetData<T>, params: INgTableParams<T>, newData: T[]): void;
}

82 changes: 82 additions & 0 deletions test/tableParamsSpec.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2159,5 +2159,87 @@ describe('NgTableParams', () => {
expect(events[1]).toEqual('datasetChanged');
});
});

describe('afterDataFiltered', () => {
it('should fire when a reload completes - no filter', () => {
// given
ngTableEventsChannel.onAfterDataFiltered((getData, params, newVal) => {
actualPublisher = params;
actualEventArgs = [newVal];
});
var data = [1,2,3];
var params = createNgTableParams({ count: 5 }, { counts: [5, 10], dataset: data });

// when
params.reload();
scope.$digest();

// then
expect(actualPublisher).toBe(params);
expect(actualEventArgs).toEqual([data]);
});

it('should fire when a reload completes and the data is filtered', () => {
// given
ngTableEventsChannel.onAfterDataFiltered((getData, params, newVal) => {
actualPublisher = params;
actualEventArgs = [newVal];
});

var initialDs = [10, 10, 101, 5];
var expectedDs = [10, 10, 101]; // when filtered with "10"
var params = createNgTableParams({ dataset: initialDs });
params.filter({ $: "10" });

// when
params.reload();
scope.$digest();

// then
expect(actualPublisher).toBe(params);
expect(actualEventArgs).toEqual([expectedDs]);
});
});

describe('afterDataSorted', () => {
it('should fire when a reload completes - no order', () => {
// given
ngTableEventsChannel.onAfterDataSorted((getData, params, newVal) => {
actualPublisher = params;
actualEventArgs = [newVal];
});
var data = [1,2,3];
var params = createNgTableParams({ count: 5 }, { counts: [5, 10], dataset: data });

// when
params.reload();
scope.$digest();

// then
expect(actualPublisher).toBe(params);
expect(actualEventArgs).toEqual([data]);
});

it('should fire when a reload completes and the data is filtered', () => {
// given
ngTableEventsChannel.onAfterDataSorted((getData, params, newVal) => {
actualPublisher = params;
actualEventArgs = [newVal];
});

var initialDs = [10, 10, 101, 5];
var expectedDs = [10, 10, 101]; // when filtered with "10"
var params = createNgTableParams({ dataset: initialDs });
params.filter({ $: "10" });

// when
params.reload();
scope.$digest();

// then
expect(actualPublisher).toBe(params);
expect(actualEventArgs).toEqual([expectedDs]);
});
});
})
});

0 comments on commit 22ed10f

Please sign in to comment.