V
V
Valery Vitalievich2020-04-14 19:32:47
JavaScript
Valery Vitalievich, 2020-04-14 19:32:47

How can I show only 5 attributes from an array?

It is necessary that the data from the array is displayed only when a search is performed on it. Not sure if this code is responsible for this. It is not necessary that all data be loaded at once or limit 5.

// Attributes
  $('#mm_attribute_new').autocomplete({
    'delay': 250,
    'source': function(request, response) {
      $.ajax({
        url: 'index.php?route=seller/account-product/jxAutocompleteAttributes&filter_name=' +  encodeURIComponent(request),
        dataType: 'json',
        success: function(json) {
          var existing_attrs = [];
          $.map($(document).find('.mm_attribute').not('.ffSample'), function(attribute_row) {
            existing_attrs.push($(attribute_row).find('input[name^="product_attribute"][name$="[name]"]').val());
          });

          response($.map(json, function(item) {
            if($.inArray(item.name, existing_attrs) == -1) {
              return {
                category: item.attribute_group,
                label: item.name,
                value: item.attribute_id
              }
            }
          }));
        }
      });
    },
    'select': function(item) {
      $(this).parents('#mm_product_attributes').find('.mm_clone').click();
      var newAttr = $(this).parents('#mm_product_attributes').find('.form-group:last');
      newAttr.find('input[name$="[attribute_id]"]').val(item['value']);
      newAttr.find('input[name^="product_attribute"][name$="[name]"]').val(item['label']);
      newAttr.find('label').append(item['label']);
      newAttr.find('textarea').focus();
      $(this).blur();
    }
  });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daria Motorina, 2020-04-14
@valera_91

Try to pinpoint the problem unambiguously, because since you're not sure what the code is that needs to be fixed, it's even harder for the responders to guess where the problem is.
Now you can understand that in the ajax success handler, data arrives with the json argument, and this json is processed in the $.map function. This means that in advance you need to leave the first five elements from json, and then transfer them to map. Ideally, five elements should be given from the backend, and not allocated at the frontend level.
Use js script debugging tools (console.log, debugger)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question