Simple data-source service with filtering, sorting and paginating. Designed to work with angular-resource models.
bower install vasvitaly/angular-data-source- Include the
data-source.jsscript into your app. - Add
vasvitaly.angular-data-sourceas a module dependency to your app.
angular.module('myApp', ['vasvitaly.angular-data-source']);// Item - is ngResource based model
controllers.controller("ItemsController",
['$scope', 'vvvDataSource', 'Item', function($scope, vvvDataSource, Item) {
var dsOptions = {
newItemDefaults: {name: '', colors: [{name: '', name_lat: '', show_in_list: 0}]},
filter: {name: 'Abr', surName: 'Bar'},
sorting: {fieldId: 'createdAt', desc: true},
clearFilter: {name: '', surName: ''},
perPage: 20,
page: 1
}
$scope.dataSource = new vvvDataSource(Item, dsOptions);
$scope.dataSource.query();
}]);<table>
<tr ng-repeat="row in dataSource.rows">
</tr>
</table>or
<table>
<tr ng-repeat="row in dataSource.filteredRows()">
</tr>
</table>$scope.dataSource = new vvvDataSource(Model, [options]);- Model - ngResource based object
- options - options object
- newItemDefaults - object, options which will be sent for creating new item of Model
- filter - object, filter will be applied to rows in
filteredRows()using angular filter() or sent to the server. You can use your own registered filters using their name double underscored as key in filters. Example: Let have own filter registered as 'startingWith'. filters
{
__startingWith: anyTypeValue
}
will use startingWith filter and send to it anyTypeValue as second argument.
- sorting - object,
{fieldId: 'string' desc: boolean}, used for ordering rows in filteredRows() and in server request. - clearFilter - object, which will be used as base in
clearFilterAPI method. - perPage - rows limit to show, also sent to the server.
- page - page to show, also sent to the server.
Makes first request to the server to get rows.
dataSource.query([options], [callBackFunc])- options - object, base options used for quering server by Model, will be populated with filters, paginating and sorting.
- callBackFunc - function, callback function to call on success response from Server. Will be called with one argument - results json.
This service checks last item of server response to have .pagination key. If found, last element pops from results and .pagination is used to populate pagination info.
Change page in paginated data
dataSource.paginate(page)page - page to show
Change data sorting field or direction. If not all data from the server shown, server request will be used.
dataSource.sortBy(fieldId)fieldId - field name to sort by. If same fieldId sent, sorting direction will be changed.
Returns current pagination info
dataSource.paginationInfo()Returns object
{
totalCount: number,
perPage: number,
page: number,
locally: boolean
}Could be empty in case no pagination used for data.
Returns current sorting info object
dataSource.sortingInfo()Returns object
{
fieldId: string,
desc: boolean,
}Returns true if current sorting field is equal to argument
dataSource.isOrderedByField(fieldId)Arg fieldId string
Return boolean
Used to send actual filtering state to the server when filtered on server
dataSource.applyFilter()Returns boolean - true when sent to the server, false - otherwise.
Applying filters also sets page to 1.
Clears current filters. Uses clearFilter initialization option or empty object.
Applies cleared filters when filtered on the server.
dataSource.clearFilter()Returns boolean - true when sent to the server, false - otherwise.
Sets page to 1.
Returns rows, filtered using angular.filter() with current filters object. Then results are sorted and paginated. If server returned more data than set in perPage local paginating will be used.
dataSource.filteredRows()Returns [<Model {}>...<Model {}>]
Builds new item of Model using newItemDefaults populated with sent args.
dataSource.newRecord(attrs)Returns new Model(newItemDefaults << attrs)
Adds arg to the top of .rows array.
dataSource.add(arg)Removes from list object having id equal to sent id
dataSource.remove(id)Returns array with deleted item or false when not found.
just untouched rows got from the server
Filters. You can use them in filter form on the page.