Autocomplete UI bring images

1

Colleagues.

I'm using the autocomplete of the jquery UI which is working properly, but I would like the results to also bring this code:

<i class="fa fa-user" aria-hidden="true"></i>

When I put it directly into PHP, it brings it as text, I believe I have to do this in Jquery. How would I do that? The code I'm using is:

<script>
  $( function() {
    function log( message ) {
      $( "<div>" ).text( message ).prependTo( "#log" );
      $( "#log" ).scrollTop( 0 );
    }

    $( "#txtUsuarios" ).autocomplete({
      source: function( request, response ) {
        $.ajax( {
          url: "buscar-usuarios.php",
          dataType: "json",
          data: {
            term: request.term
          },
          success: function( data ) {
                      response( data );
            //response( data );

          }
        } );
      },
      minLength: 2,
      select: function( event, ui ) {
        log( "Selected: " + ui.item.value + " aka " + ui.item.id );
      }
    } );
  } );
  </script>
    
asked by anonymous 21.01.2017 / 21:47

1 answer

0

There is a method called _renderItem

It's a bit complex, and it's used with images and not icon fonts, but I think you'd enjoy using it:

(function($){
  
  var $project = $('#project');

  var projects = [
    {
      value: "jquery",
      label: "jQuery",
      desc: "the write less, do more, JavaScript library",
      icon: "jquery_32x32.png"
    },
    {
      value: "jquery-ui",
      label: "jQuery UI",
      desc: "the official user interface library for jQuery",
      icon: "jqueryui_32x32.png"
    },
    {
      value: "sizzlejs",
      label: "Sizzle JS",
      desc: "a pure-JavaScript CSS selector engine",
      icon: "sizzlejs_32x32.png"
    }
  ];
  
  $project.autocomplete({
    minLength: 0,
    source: projects,
    focus: function( event, ui ) {
      $project.val( ui.item.label );
      return false;
    }
  });
  
  $project.data( "ui-autocomplete" )._renderItem = function( ul, item ) {
    
    var $li = $('<li>'),
        $img = $('<img>');


    $img.attr({
      src: 'https://jqueryui.com/resources/demos/autocomplete/images/' + item.icon,
      alt: item.label
    });

    $li.attr('data-value', item.label);
    $li.append('<a href="#">');
    $li.find('a').append($img).append(item.label);    

    return $li.appendTo(ul);
  };
  

})(jQuery);
<!DOCTYPE html>
<html>
<head>
<link href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script><scriptsrc="http://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div id="project-label">Select a project (type "j" for a start):</div>
  <input id="project">
</body>
</html>
  

This example is not my own, but @BillCriswell member of Stackoverflow in English

Source: Stackoverflow in English

    
21.01.2017 / 22:29