Support the ongoing development of Laravel.io →
Input Forms

This is a noob question but I hope you can help. I have a table of reference data (places, events, people etc) that I want to associate with a table of images. When cataloguing the image I want to use autocomplete to associate the image with pre-existing entries in the "wiki" like table. Here is the autocomplete code that works to populate a list of references. What I want to do is upon save add a reference in a bridge table so that image_id,wiki_id is created. Similarly, upon removal of the tag, the reference is disassociated. I will probably work it out but I am looking for best practice advice here.

Many thanks,

J

$(function() { function split( val ) { return val.split( /,\s*/ ); } function extractLast( term ) { return split( term ).pop(); }

$( "#wikitags" )
  // don't navigate away from the field on tab when selecting an item
  .bind( "keydown", function( event ) {
    if ( event.keyCode === $.ui.keyCode.TAB &&
        $( this ).data( "ui-autocomplete" ).menu.active ) {
      event.preventDefault();
    }
  })
  .autocomplete({
    source: function( request, response ) {
      $.getJSON( "/ajax/wikiSearch", {
        term: extractLast( request.term )
      }, response );
    },
    search: function() {
      // custom minLength
      var term = extractLast( this.value );
      if ( term.length < 2 ) {
        return false;
      }
    },
    focus: function() {
      // prevent value inserted on focus
      return false;
    },
    select: function( event, ui ) {
      var terms = split( this.value );
      // remove the current input
      terms.pop();
      // add the selected item
      terms.push( ui.item.value );
      // add placeholder to get the comma-and-space at the end
      terms.push( "" );
      this.value = terms.join( ", " );
      return false;
    }
  });

});

Last updated 3 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.