Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#10581 closed bug (notabug)

autocomplete: item.value skips zero index

Reported by: gadelat Owned by:
Priority: minor Milestone: none
Component: ui.autocomplete Version: 1.11.1
Keywords: Cc:
Blocked by: Blocking:

Description

I got something like this:

$('#form-name').autocomplete({
  source: function(request, response) {
    $.ajax({
      dataType: "json",
      url: "private_url",
      success: function(data) {
        response($.map(data, function(item, index) {
          return {
            label: 'test',
            value: index
          }
        }));
      }
    })
  },
  select: function(event, ui) {
    console.log(ui.item.value);
  }
});

It worked fine in 1.10.4, but in 1.11.1 it outputs label instead of value when selecting first item. When selected other than first item, it works fine. I guess jquery-ui v. 1.11.1 puts label into value if index < 1

Change History (3)

comment:1 Changed 5 years ago by Scott González

Resolution: notabug
Status: newclosed

This isn't a bug, since autocomplete doesn't support numbers. The number 0 fails the truthy check in a || b, the rest pass the test and are implicitly converted to strings.

comment:2 Changed 5 years ago by gadelat

Uhh, can you enlighten me in decision to stop supporting numbers anymore? It's INDEX for gods sake, it's supposed to be zero and pass whatever check there is. Now user can't reliably do this anymore:

var users = {};
$('#form-name').autocomplete({
  source: function(request, response) {
    $.ajax({
      dataType: "json",
      url: "private_url",
      success: function(data) {
        users = data;
        response($.map(data, function(item, index) {
         return {
            label: item.name,
            value: index
          }
        }));
      }
    })
  },
  select: function(event, ui) {
    event.preventDefault();
    var user = users[ui.item.value];
      $('#property1').val(user.property1);
      $('#property2').val(user.property2);
      $('#property3').val(user.property3);
      $('#property4').val(user.property4);
  }
});

I can easily skip this check by doing String(index), but one should not be supposed to do that, indexes are not supposed to be strings just to pass this truth check in library.

Last edited 5 years ago by gadelat (previous) (diff)

comment:3 Changed 5 years ago by Scott González

We didn't decide to stop supporting it. It was never supported in the first place, it just happened to work.

Note: See TracTickets for help on using tickets.