-
Notifications
You must be signed in to change notification settings - Fork 52
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
Updated support for sorting of drop downs #265
Changes from 2 commits
6d6663d
09b0cc2
406dd0d
47367db
0873a72
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,11 +25,9 @@ export default class DialogDataService { | |
if (option[0] === String(field.default_value)) { | ||
field.selected = option; | ||
} | ||
if (field.data_type === 'integer') { | ||
dropDownValues.push([parseInt(option[0], 10), option[1]]); | ||
} else { | ||
dropDownValues.push(option); | ||
} | ||
const value = (field.data_type === 'integer' ? parseInt(option[0], 10) : option[0]); | ||
const description = (parseInt(option[1], 10) ? parseInt(option[1], 10) : option[1]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this Because if not, this breaks on any description which starts with a number. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Description is only used for sorting the drop down list. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh.. and maybe an Otherwise 0 will still get sorted last.. (because EDIT: you can also use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ha, you beat me to it. I just added isNan in just now. |
||
dropDownValues.push([value, description]); | ||
} | ||
field.values = dropDownValues; | ||
field.values = this.updateFieldSortOrder(field); | ||
|
@@ -48,8 +46,25 @@ export default class DialogDataService { | |
* | ||
**/ | ||
private updateFieldSortOrder(data) { | ||
let values = _.sortBy(data.values, data.options.sort_by === 'value' ? 0 : 1); | ||
return data.options.sort_order === 'ascending' ? values : values.reverse(); | ||
const SORT_DESCRIPTION = 1; | ||
const SORT_VALUE = 0; | ||
const FIRST_OPTION = 0; | ||
const VALUE = 0; | ||
const sortBy = (data.options.sort_by === 'value' ? SORT_VALUE : SORT_DESCRIPTION); | ||
let tempValues = [...data.values]; | ||
let defaultDropdownField = []; | ||
// The following if deals with a empty default option if it exists | ||
if (data.data_type === 'integer' && _.isNaN(tempValues[FIRST_OPTION][VALUE]) || | ||
_.isNull(tempValues[FIRST_OPTION][VALUE])) { | ||
defaultDropdownField = tempValues.shift(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we just combine this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about that when i reviewed my own code. I think I will try that but will put the or on a separate line |
||
let values = _.sortBy(tempValues, sortBy); | ||
const sortedValues = data.options.sort_order === 'ascending' ? values : values.reverse(); | ||
if (defaultDropdownField.length) { | ||
sortedValues.unshift(defaultDropdownField); | ||
} | ||
|
||
return sortedValues; | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, can you change the keys and values to something different (from each other)?
It is impossible to tell from this spec if this really works or if it just sorts by value instead :).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, and also without an
11
or something like that, it doesn't actually tell us if those are sorted as numbers or as strings..There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, we need to set the
data.data_type
tointeger
which is part of the original problem that javascript was sorting 1, 11, and 2 as if they were strings.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually that wasn't the problem this PR addresses. This deals with a use case where a user would decide to actually use numbers in the description field and then decide they want to sort the dropdown by the description.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eclarizio , I went ahead and updated one of the test cases to actually specify the data type. It wasn't directly related to this test but I added it in for clarity sake.