5

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,

Community
  • 1
  • 1
Pastlifeuknow
  • 91
  • 1
  • 1
  • 9
  • What you need your script to do is clear, but what you're asking form us is not. You will get better responses if you tell us what you've already tried to do to accomplish your task and what the results of those attempts were. For example, you already have one attempted answer and your comment said that you already tried that. If you include that information in your question, you can save everyone time. – skrrgwasme Jul 23 '14 at 17:52
  • Thanks For the advice hopefully what I added more to help. – Pastlifeuknow Jul 23 '14 at 18:12
  • It's looking much better, but I've made an edit: I switched the order of your background information and your specific request. Your request is pretty specific, so it makes more sense to describe your overall project before the specific task at hand. Also, it's great that you've added your attempted solutions, but try to be more specific with the first result you've listed. What does "nothing" mean? Did you get a segfault? A blank screen? Did it execute but still give you incorrect results? What did they look like? – skrrgwasme Jul 23 '14 at 18:30
  • Hopefully that edit answers questions. Not familiar with segfault. – Pastlifeuknow Jul 23 '14 at 18:33
  • Perhaps segfault was a poor example of a potential error since you're working with Javascript. It's short for [Segmentation Fault](http://en.wikipedia.org/wiki/Segmentation_fault) and is more common in languages like C/C++ where you manually manage memory. You shouldn't encounter them often in JS web development, although it's still possible. I think you have a much stronger question now, and if I knew enough about JS I would try to answer it for you. – skrrgwasme Jul 23 '14 at 18:38
  • Thanks a lot for the assist and the education. – Pastlifeuknow Jul 23 '14 at 18:43

2 Answers2

2

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.
Pastlifeuknow
  • 91
  • 1
  • 1
  • 9
0

assign the value to the auto complete input element by using

$('#YourAutoCompletBox').val(yourValuefromHiddenControl);

html:

Topic: <input type="text" id="topics" /><input type="hidden" id="topicID" />

<br/><br/><br/><br/>

<p>You selected <span id="results"></span></p>

jQuery:

var topics= [
    {
        value: "cooking",
        label: "Cooking",
        id: "1"
    },
    {
        value: "C++",
        label: "C++",
        id: "2"
    },
    {
        value: "craftsmanship",
        label: "Software Craftsmanship",
        id: "3"
    }
];

$(document).ready(function() {
    $( "#topics" ).autocomplete({
        minLength: 0,
        source: topics,
        focus: function( event, ui ) {
            $( "#topics" ).val( ui.item.label );
            return false;
        },
        select: function( event, ui ) {
            $( "#topics" ).val( ui.item.label );
            $("#topicID").val(ui.item.id);
            $( "#results").text($("#topicID").val());    
            return false;
    }
})

});

Playground : jsfiddle

SitecoreNoob
  • 301
  • 1
  • 3
  • 12
  • please check my Updated post , hope this helps , please mark as answer if it works for you – SitecoreNoob Jul 23 '14 at 17:55
  • That's pretty much what I have now. What I need to do now is essentially to take the value in your and pass it into the input then show the label instead of the input. – Pastlifeuknow Jul 23 '14 at 18:11
  • U have to use $( "#topics" ).val( ui.item.label ); $("#topicID").val(ui.item.id); to access your label property, please let me know if it works for you – SitecoreNoob Jul 23 '14 at 18:16
  • Need to this when page loads. So i would us the create method. When I do the ui object is null. – Pastlifeuknow Jul 23 '14 at 18:30