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

[RFR] format number for export #577

Merged
merged 1 commit into from
Aug 4, 2015
Merged
Show file tree
Hide file tree
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
17 changes: 15 additions & 2 deletions src/javascripts/ng-admin/Crud/misc/EntryFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ define(function () {
return $filter('date')(date, format);
};
};
this.formatNumber = function (format) {
return function (number) {
return $filter('numeraljs')(number, format);
};
};
}

EntryFormatter.prototype.formatField = function formatField(field) {
Expand All @@ -19,8 +24,6 @@ define(function () {
case 'boolean':
case 'choice':
case 'choices':
case 'number':
case 'float':
case 'string':
case 'text':
case 'wysiwyg':
Expand All @@ -34,6 +37,16 @@ define(function () {
value: entry.values[field.name()]
};
};
case 'number':
case 'float':
var format = field.format();
var formatNumber = this.formatNumber(format);
return function (entry) {
return {
name: label,
value: formatNumber(entry.values[field.name()])
};
};
case 'date':
case 'datetime':
var format = field.format();
Expand Down
16 changes: 10 additions & 6 deletions src/javascripts/test/unit/Crud/misc/EntryFormatterSpec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/*global jasmine,angular,describe,it,expect,beforeEach,spyOn*/
var EntryFormatter = require('../../../../ng-admin/Crud/misc/EntryFormatter');
var $filter = function () {
return function (date, format) {
var $filter = function (filter) {
return function (value, format) {
return {
date: date,
filter: filter,
value: value,
format: format
};
};
Expand Down Expand Up @@ -31,10 +32,12 @@ describe("Service: EntryFormatter.getFormatter formatter", function () {
var formatter = entryFormatter.getFormatter([getField({
type: 'number',
name: 'number',
label: 'Number'
label: 'Number',
format: '0.000%'
})]);
var number = 56;

expect(formatter({values: {number: 5}})).toEqual({Number: 5});
expect(formatter({ values: { number: number } })).toEqual({ Number: { filter: 'numeraljs', value: 56, format: '0.000%' } });
});

it('formatter should format field of type text', function () {
Expand Down Expand Up @@ -77,7 +80,8 @@ describe("Service: EntryFormatter.getFormatter formatter", function () {
var date = new Date();

expect(formatter({values: {date: date}})).toEqual({Date: {
date: date,
filter: 'date',
value: date,
format: 'yyyy/mm/dd'
}});
});
Expand Down