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

optionsSorting not working for numeric values #2277

Open
goldfoot opened this issue Feb 20, 2023 · 1 comment
Open

optionsSorting not working for numeric values #2277

goldfoot opened this issue Feb 20, 2023 · 1 comment

Comments

@goldfoot
Copy link

goldfoot commented Feb 20, 2023

jTable: 2.5.0

I have a field with options like to:

{
    "title": "My field",
    "options": {
        "1": "Some Label",
        "5": "Some Label",
        "7": "Some Label",
        "8": "Some Label",
        "11": "Some Label",
        "13": "Some Label",
        "15": "Some Label",
        "16": "Some Label",
        "96": "Some Label",
        "97": "Some Label",
        "98": "Some Label",
        "99": "Some Label",
        "100": "Some Label",
        "101": "Some Label",
        "102": "Some Label",
        "103": "Some Label"
    },
    "optionsSorting": "value-desc",
}

The object is parsed from JSON, so it is not possible for me to use numeric keys directly.

I expected that the options would be sorted from highest numeric values to lowest numeric values in the dropdown of the create.and edit forms. However, this is not the case. The options get sorted by value (so 1, 5, 7, etc.), but the values are treated as string values. Instead of a numerically descending order, the options are sorted in the lexicographic order, resulting in:
99, 98, 97, 96, 16, 15, 13, 103, 102, ...

After some research of the jTable source code, i found that the type check of the value done in https://github.com/volosoft/jtable/blob/master/jquery.jtable.js#L955 is not respecting numeric values in quotes as such. So I changed

if ($.type(dataSelector(options[0])) == 'string') {
    // lexicographic sorting
} else {
   // numeric sorting
}

to

if (!$.isNumeric(dataSelector(options[0]))) {
    // lexicographic sorting
} else {
   // numeric sorting
}

This seems to work, even if the keys of the object are numeric values in quotes.

@liedekef
Copy link
Contributor

This solution has been added to my fork, I agree :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants