autocomplete icon indicating copy to clipboard operation
autocomplete copied to clipboard

Fill <input /> with autocomplete:selected

Open patricktargun opened this issue 10 years ago • 10 comments

How would I fill the input with the selected value?

This worked but has a huge lag:

$scope.$on('autocomplete:selected', function(event, suggestion, dataset) {
    console.log(suggestion, dataset);
    $scope.q = suggestion.name
});

patricktargun avatar Aug 11 '16 15:08 patricktargun

Did you try https://github.com/algolia/autocomplete.js#jqueryautocompleteval-val?

kobykorman avatar Aug 12 '16 15:08 kobykorman

Thanks @legshooter - Do you know if there is a way to do this without jQuery? (Using Angular)

Javascript:

console.log(document.getElementById("search-input").autocomplete('val'))

HTML:

<input  id="search-input" ng-model="q" autocomplete aa-datasets="getDatasets()" />

// LOGS: document.getElementById(...).autocomplete is not a function

patricktargun avatar Aug 12 '16 16:08 patricktargun

It makes sense, it is a jQuery plugin, it literally doesn't exist where you're trying to call it. I have no Angular experience atm, so I can't help you with that, though I thought Angular excels at data binding. Maybe someone else has an idea, or one of the maintainers catches up with this.

kobykorman avatar Aug 12 '16 16:08 kobykorman

Adding $scope.$digest() helped speed up the process, but when tabbing/blurring out of the input, the value changes to its pre 'autocomplete:selected' value.

$scope.$on('autocomplete:selected', function(event, suggestion, dataset) {
    console.log(suggestion, dataset);
    $scope.q = suggestion.name
    $scope.$digest()
});

patricktargun avatar Aug 14 '16 01:08 patricktargun

Yes I think calling $digest or $apply is the way to go.

Then, about the value being changed to its previous value; I'm not sure to understand why. Maybe setting the $scope.q is not enough and we should call the underlying setVal method as well.

Maybe try

$scope.$on('autocomplete:selected', function(event, suggestion, dataset) {
    $scope.$apply(function() {
      $scope.q = suggestion.name;
      document.getElementById("search-input").autocomplete('val', suggestion.name);
    });
});

PS: the provided angular binding is just a wrapper on top of the jQuery library, that's why it's not 100% angular-proof.

redox avatar Aug 16 '16 07:08 redox

I had a strange reaction where the ng-model was updated properly (I could log it in a function), but for some reason the input would stay the short version. Seemed impossible...

Thanks for the help on this, but I had to resort to another autocomplete library. Still using Algolia for data retrieval :)

patricktargun avatar Aug 16 '16 11:08 patricktargun

I was having a similar issue, I was able to pull off what I wanted like this...

var searchInput = angular.element("#label");
$scope.q = target.label;
$timeout(function() {
  searchInput.val($scope.q).focus();
}, 0);

afgarcia86 avatar Feb 01 '17 17:02 afgarcia86

I figured that out, but only using jquery directly. Because none of the options above worked for me. Turns out, that this library uses an interval variable to keep the value. So even if you set the input directly with jquery, with $('#selector').val('some-value') it'll return back on blur.

I discovered that this internal variable was set in the onInput event. So what I did was only trigger this function, here's the final code:

$scope.$on('autocomplete:selected', function(event, suggestion, dataset) {
  let element = angular.element('#selector');
  element.val($scope.q).trigger('input').focus();
});

Daymannovaes avatar Jul 05 '17 23:07 Daymannovaes

I would be nice if the directive would return the element in the $emit function, so you can look which input executed the search request.

We have a use case where we have 2 inputs with the autocomplete directive, but each one of them has a different business logic to it. The $scope.$on('autocomplete:selected', function(event, suggestion, dataset) does not allow for dynamic selectors.

It whould be nice to have something like this.

$scope.$on('autocomplete:selected', function(event, suggestion, dataset, element) {
  let element = angular.element(`#${element.name}`);
  element.val($scope.q).trigger('input').focus();
});

UPDATE

I created a simple directive

<input
              type="text"
              id="input-edit"
              name="input-edit"
              ng-model="something"
              autocomplete
              aa-options="{autoselect:true}"
              aa-datasets="Algolia.Search()"
              user-search>

ANGULAR DIRECTIVE

return {
    restrict: 'A',
    controller: 'SearchUserController as user',
    link: function (scope, element, attrs, controller){
      var inputElement = element[0];
      controller.setInputId(inputElement.id);
    }
  }

ANGULAR DIRECTIVE CONTROLLER

self.inputId = '';
  self.setInputId = (value) => self.inputId = value;
  $scope.$on('autocomplete:selected', function(event, suggestion, dataset) {
    let optionSelected = suggestion.username;
    let inputElement = angular.element(`#${self.inputId}`);
    inputElement && inputElement.is(':visible') && inputElement.is(':focus') && inputElement.val(optionSelected).trigger('input').focus();
  });

a-covar avatar Jul 21 '17 04:07 a-covar

@a-covar We accept PR fixing this, that's a nice idea and should be backward compatible.

Quick note, isn't this in the event already bound to the correct element?

vvo avatar Jul 24 '17 08:07 vvo