NicolasCARPi / jquery_jeditable

jQuery edit in place plugin. Extendable via plugin architecture. Plugins for plugin. Really.
https://jeditable.elabftw.net
MIT License
1.74k stars 458 forks source link

Possible bug in display of selected select option on edit #244

Closed sronsiek closed 2 years ago

sronsiek commented 2 years ago

I'm porting an app from jeditable 1.7.3 to 2.0.19

I'm using a number of selects, example options data looks like this:

select_options = JSON.stringify( {
  "":"Not configured",
  "e4ee63964c4711e88296005056014dc0":"Option #1",
  "519dc1904c4711e896db005056014dc0":"Option #2",
  "dcf62004ab8611e39ed40025b3e1c290":"Option #3"
})

Options like this:

         $('td.edit.').editable(url, {
            data: select_options,
            type: 'select',
            method: 'PUT',
            tooltip   : 'Click to edit...',
            indicator : 'Saving...'
}

Initial HTML like this:

<td title="Click to edit...">Option #2</td>

What I've observed: In 1.7.3 everything works as expected. In 2.0.19, when I click on Option #2 to open the select form, the selected option is Not Configured when the expected value is Option #2.

I have done some debugging & found that in 1.7.3 there is a comparison between $(this).text() and $.trim(original.revert) to set selected:

                    $('select', this).children().each(function() {
                        if ($(this).val() == json['selected'] || 
                            $(this).text() == $.trim(original.revert)) {
                                $(this).attr('selected', 'selected');
                        }
                    });

... whereas in 2.0.19 the comparison is between key and String.prototype.trim.call(original.revert == null ? "" : original.revert):

                    if (json.selected === key || value === String.prototype.trim.call(original.revert == null ? "" : original.revert)) {
                         $(option).prop('selected', 'selected');
                    }

ie in the first case, the value from the options hash is used, in the second the key is used.

To test if this is really the issue, I changed the line:

 if (json.selected === key || key === String.prototype.trim.call(original.revert == null ? "" : original.revert)) {

to

 if (json.selected === key || value === String.prototype.trim.call(original.revert == null ? "" : original.revert)) {

Feels like a bug, though I'm not certain. In cases where keys and values are the same in the options, it would not show up ...

jQuery version: 1.11.1 Browser: chrome, firefox OS: SLES15 SP3

NicolasCARPi commented 2 years ago

Hello,

This project is currently transitioning to a new library I wrote: https://github.com/deltablot/malle/

It's still fresh but the core is working, and it will be maintained. At some point I'll officially declare jquery-jeditable as deprecated and focus on maintaining this new library.

It would be great if you could give malle a try and let me know if some important bits are missing.

It is possible that you found a bug (thanks for the detailed report btw), but I'm not sure this will be fixed, for the reasons stated above.