Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Jquery Autocomplete to a specific value and display It's Label using a datasource of JSON objects

Background

So I have a table that is populated by a form. Each row can be edited by hitting a edit button. The Edit button opens the form that is populated. I need to auto fill the autocomplete so that the user can see one of His selected course.

How I Cheated

I'm using PHP and Codeigniter server side and am dynamically creating my form based on database. The labels and values are all produced from the Database and populate my JQuery Auto complete (a.k.a datasource variable below). From my controller I'm passing my value to the model and getting the Label from the DB. From there I'm passing it to my view and to my AutoComplete and setting the input value equal to the found label.

I feel dirty having done it this way. The inefficiency of this burns my eyes.

My Goal

I want to use the value that I've gotten and have the autocomplete select it and display it's label client side.

OR

I need to just display the label in the box so the user knows it's not a blank field.

Both options need to allow the user to modify the autocomplete box.

Existing Code

My code for the input looks like this:

<div class="row-start span-2">
        <label for="course_code">Course Code </label>
    </div>
    <div class="row-end span-2">
        <input id="course_code">
    </div>

My script for the autocomplete looks like this:

    <script>
    function search_course_code(){
      var datasource = [{"value":"1","label":"AAF100 - DL"},{"value":"2","label":"AAF101 - DL+web"},.....]; 
      var searchboxid = "#course_code";
      var searchresultid = "#CRSEINVID";
      $(searchboxid).autocomplete({
        source:datasource,
        focus: function( event, ui ) {
            $( searchboxid ).val( ui.item.label );
            return false;
            },
        select: function(event,ui){
            var UIvalue = ui.item.value;
            var UIlabel = ui.item.label;
            console.log(UIvalue);
            console.log(UIlabel);
            $( searchboxid ).val( ui.item.label );
            use_search("#search1","#CRSEINVID",UIvalue,UIlabel );        return false;
        }
      });
    }; 
    function use_search(show_select,result_id,uivalue,uilabel){
    //loads value to field that takes it's value
    $(result_id).val(uivalue);
    //Display course below search box
    course = "<span>"+uilabel+"</span>";
    $(show_select).html(course );

    //stops the value from being shown in the search box
    return false;
  };

    $( document ).ready(function() {
         search_course_code();
    });
    </script>

I draw the value from a hidden input with a unique ID simply using JQUERY val() function.

What I've tried

Try 1

Setting value using: $(searchboxid).val(hiddenInputValue); Result: Value displayed not the label

Try 2

Using the autocomplete on create method I tried to overwrite the UI object and send it to the select.

ui.item={"value":"","label":""};
ui.item.value=$(hiddenInputValue).val;
this.select(ui);

Result: No observable change, no errors.

Try 3

$(searchboxid).autocomplete("select", hiddenInputValue);

Result:

Uncaught Error: cannot call methods on autocomplete prior to initialization; attempted to call method 'select'

Try 4

Tried changing value using

$(searchboxid).val(hiddenInputValue);

and having change function detect it and set label with

$( searchboxid ).val( ui.item.label );

Result: Value loaded into input not label

Try 5

Tried Triggering the change function with this:

$("#<?php echo $id;?>").autocomplete("option","change").call(searchBox);

and then setting label. Based on the answer to:

jQuery AutoComplete Trigger Change Event

Result: empty UI object for change function,

Try 6

Tried Triggering the select function with this:

$("#<?php echo $id;?>").autocomplete("option","select",{value:hiddenInputValue}).call(searchBox);

and then using my current select function.

Result: Uncaught Error: undefined is not a function,

Ideas

Ideas 1:

I thought of using the value then searching through the datasource object to find associating label and then using:

$(searchboxid).val(label);

would this work? How would I do it?

Idea 2:

If the value of the input field is set to a value using:

$(searchboxid).val(label);

Would the change function detect it? Not detected used console.log function in change function to give feedback,

like image 720
Pastlifeuknow Avatar asked Oct 31 '22 20:10

Pastlifeuknow


1 Answers

So after much research and trying to get this to work I discovered two problems:

  1. that I was using Select2 version 3.5.3 and needed to use text instead of label and :

$myselect.select2("val","somevalue");

  1. The MAJOR source of my problem though was because I was using Web Experience Toolkit tabs and I needed to load the Select 2 after tabs where initialized.
like image 56
Pastlifeuknow Avatar answered Nov 09 '22 10:11

Pastlifeuknow