Solved

passing multidimensional array in jquery

Posted on 2014-11-18
9
718 Views
Last Modified: 2015-01-02
hi,

How can i pass multidimensional array in jquery autocomplete.
say var availableTags = [[1,"ActionScript"],[2,Groovy]];

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery UI Autocomplete - Multiple values</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>
  $(function() {
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
    ];
    function split( val ) {
      return val.split( /,\s*/ );
    }
    function extractLast( term ) {
      return split( term ).pop();
    }
 
    $( "#tags" )
      // don't navigate away from the field on tab when selecting an item
      .bind( "keydown", function( event ) {
        if ( event.keyCode === $.ui.keyCode.TAB &&
            $( this ).autocomplete( "instance" ).menu.active ) {
          event.preventDefault();
        }
      })
      .autocomplete({
        minLength: 0,
        source: function( request, response ) {
          // delegate back to autocomplete, but extract the last term
          response( $.ui.autocomplete.filter(
            availableTags, extractLast( request.term ) ) );
        },
        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;
        }
      });
  });
  </script>
</head>
<body>
 
<div class="ui-widget">
  <label for="tags">Tag programming languages: </label>
  <input id="tags" size="50">
</div>
 
 
</body>
</html>

Open in new window

0
Comment
Question by:Rocking
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
9 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40451185
test page : http://jsfiddle.net/2ojLszux/1/

  $(function() {
    var availableTags =  [[1,"ActionScript"],[2,"Groovy"]];
 
    $( "#tags" )
      .autocomplete({
        minLength: 0,
        source: function( request, response ) {
          // delegate back to autocomplete, but extract the last term
            var r = [];
            for(var i=0;i<availableTags.length;i++) {
                if(availableTags[i][1].indexOf(request.term)>=0) {
                    r.push({label:availableTags[i][1],value:availableTags[i][0]});
                }
                response(r);
            }
        },
        focus: function() {
          // prevent value inserted on focus
          return false;
        },
        select: function( event, ui ) {
          this.value = ui.item.value + ", " + ui.item.label;
          return false;
        }
      });
  });

Open in new window

0
 

Author Comment

by:Rocking
ID: 40451247
text box should display
this.value =  ui.item.label;
how to get the id of the value from the text box?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40451253
text box should display
this.value =  ui.item.label;

do you mean you want to display the label only and not the id?

how to get the id of the value from the text box?

to do what?
alert( ui.item.value ); // alert the id

new example : http://jsfiddle.net/2ojLszux/2/
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Rocking
ID: 40451273
http://jsfiddle.net/2ojLszux/2/
It's not working for multiple values
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40451367
It's not working for multiple values

I'm not following you
0
 

Author Comment

by:Rocking
ID: 40452742
What i mean to say when u type A it gives the options,now select Action Script,Output is as per below
1, ActionScript

Working fine

Now the text box has value (1, ActionScript) and i try to type G it should display Groovy it did when i typed A
In this case it is not working
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40453666
jquery autocomplete doesn't work like this
your initial question talk about multi array and not putting multi result in a single textbox
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question