argerim / select2-rails

Integrate Select2 javascript library with Rails asset pipeline
https://github.com/argerim/select2-rails
965 stars 374 forks source link

Stopped working for list of entries with the same id #206

Open mhenrixon opened 1 year ago

mhenrixon commented 1 year ago

The following code used to work:

$('[data-select-products]').each(function() {
  var $el;
  $el = $(this);
  return $el.select2({
    theme: 'bootstrap',
    width: null,
    containerCssClass: ':all:',
    minimumInputLength: 2,
    ajax: {
      url: '/admin/shop/products',
      delay: 600,
      dataType: 'json',
      data: function(params) {
        return {
          search: params.term,
          page_limit: 10,
          reject_ids: $el.data('reject-ids')
        };
      },
      processResults: function(data) {
        var i, len, p, ref, results, text;
        results = [];
        ref = data.products;
        for (i = 0, len = ref.length; i < len; i++) {
          p = ref[i];
          text = p.reference;
          results.push({
            id: p.id,
            text: text
          });
        }
        return {
          results: results
        };
      }
    }
  });
});

For the following HTML:

= form_for [:admin, :shop, @product, product_relation], html: {class: '', data: {remote: true}} do |f|
    .form-group.row
      .col-md-6
        - if product_relation.new_record?
          = f.hidden_field :relation_id
          = f.select :related_product_id, [], {}, id: "product_relation_related_product_id", data: { select_products: true, reject_ids: product_ids}, class: 'form-control'

As it is now after upgrading select2-rails and jQuery dependencies it only marks the last one for select2. Any ideas on what I need to change to make it work for the first x records and not only the last? It used to work at some point :)

Haven't used jQuery in years so baffled, that said I am in the process of upgrading all our dependencies so figured something in the usage changed.