Solved

jQuery handsontable auto-complete to auto-fill other fields

Posted on 2013-11-24
6
4,379 Views
1 Endorsement
Last Modified: 2013-11-26
Hi all,
it's about jQuery Excel-like data grid editor. More can be found  here:
http://handsontable.com/

I need help to combine this working jQuery autocomplete autofill :
<!DOCTYPE html>
<html>
	<head>
		<meta charset='utf-8'>
		<title>jQuery UI Autocomplete - Autofill</title>
		<link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/redmond/jquery-ui.css">	
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
		<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
		<script type="text/javascript">
		$(function() { 
			$('#iprice').val("");
			$('#icode').val("");

			$("#name").autocomplete({
				source: [{"name":"BMW","label":"Air Soft Gun","price":"212","abbrev":"BMW"},
						 {"name":"Chrysler","label":"Pepsi Cola Hat","price":"24","abbrev":"CRY"},
						 {"name":"Nissan","label":"Candle Lights Dinner","price":"780","abbrev":"NSS"},
						 {"name":"Suzuki","label":"Pork Meat Ball","price":"178","abbrev":"SZK"},
						 {"name":"Toyota","label":"Granny Health Supplement","price":"24","abbrev":"TYT"}],
				minLength: 2,
				select: function(event, ui) { 
					$('#iprice').val(ui.item.price);
					$('#icode').val(ui.item.abbrev);
				}
			});"BMW", "Chrysler", "Nissan", "Suzuki", "Toyota", "Volvo"

			$["ui"]["autocomplete"].prototype["_renderItem"] = function( ul, item) {
				return $( "<li></li>" ) 
  				.data( "item.autocomplete", item )
  				.append( $( "<a></a>" ).html( item.label ) )
  				.appendTo( ul );
			};

		});
		</script>
	</head>
<body>
<div class="container">
<form action="" method="post">
	<p class="ui-widget">
		<input type="text" id="name" name="name" />
		<input readonly="readonly" type="text" id="iprice" name="iprice" size="5">
		<input readonly="readonly" type="text" id="icode" name="icode" size="3">
	</p>
</form>
</div>
</body>
</html>

Open in new window


...into this working handsontable autocomplete feature:
<!doctype html>
<html>
<head>
  <meta charset='utf-8'>
  <title> Handsontable | Autocomplete OK | Autofill ?!? </title>
  <script data-jsfiddle="common" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  <script data-jsfiddle="common" src="http://handsontable.com/dist/jquery.handsontable.full.js"></script>
  <link data-jsfiddle="common" rel="stylesheet" media="screen" href="http://handsontable.com/dist/jquery.handsontable.full.css">
</head>

<body>

        <div class="pad">
          <div id="mytables"></div>
        </div>

<script type="text/javascript">
var $container	= $("#mytables");
var comsources	= ["BMW", "Chrysler", "Nissan", "Suzuki", "Toyota"];

$container.handsontable({
  startRows: 1,
  startCols: 5,
  rowHeaders: true,
  colHeaders: ['Item Name', 'Price', 'Code'],
  minSpareRows: 1,
  contextMenu: true,
  columns: [
    {
	  data: "name",
      type: 'autocomplete',
      source: comsources,
      strict: false
    },
    {
	  data: "price"
	},
    { 
	  data: "code"
	}
  ]
});
</script>

</body>
</html>

Open in new window


So, what I'm looking for is the autocomplete autofill feature to be applied to "handsontable". On handsontale I can manage to have an autocomplete field... I want this, when a value is selected from the autocomplete, it will also fill 2 other fields with a value.

I was doing lots of trial & errors but seems not able to find a proper solution.

Please help... Thanks.
1
Comment
Question by:tobece
[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
  • 3
  • 3
6 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39673590
Test page : http://jsfiddle.net/wvXvJ/

<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<title> Handsontable | Autocomplete OK | Autofill ?!? </title>
<link data-jsfiddle="common" rel="stylesheet" media="screen" href="http://handsontable.com/dist/jquery.handsontable.full.css">
<script data-jsfiddle="common" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script data-jsfiddle="common" src="http://handsontable.com/dist/jquery.handsontable.full.js"></script>
<script type="text/javascript">

	$(document).ready(function() {

		var $container	= $("#mytables");
		var comsources	= ["Chrysler", "Nissan", "Suzuki", "Toyota"];

		var ac = [
			{"name":"Chrysler","label":"Pepsi Cola Hat","price":"24","abbrev":"CRY"},
			{"name":"Nissan","label":"Candle Lights Dinner","price":"780","abbrev":"NSS"},
			{"name":"Suzuki","label":"Pork Meat Ball","price":"178","abbrev":"SZK"},
			{"name":"Toyota","label":"Granny Health Supplement","price":"24","abbrev":"TYT"}
		];
	
		var ht = $container.handsontable({
			startRows: 1,
			startCols: 5,
			rowHeaders: true,
			colHeaders: ['Item Name', 'Price', 'Code'],
			minSpareRows: 1,
			contextMenu: true,
			columns: [
				{
					data: "name",
					type: 'autocomplete',
					source: comsources,
					strict: false
				},
				{
					data: "price"
				},
				{ 
					data: "code"
				}
			],
			afterChange : function(arr, op) {
				if(op=="edit"&&arr.length==1) {
					var value = arr[0][3];
					for(var i=0;i<ac.length;i++) {
						if(ac[i].name == value) {
							$container.handsontable("setDataAtCell", arr[0][0], 1, ac[i].price);
							$container.handsontable("setDataAtCell", arr[0][0], 2, ac[i].abbrev);
							return false;
						}
					}
				}
			}
		});
	});

</script>
</head>
<body>
<div class="pad">
	<div id="mytables"></div>
</div>
</body>
</html>

Open in new window

1
 

Author Comment

by:tobece
ID: 39675851
Thanks @leakim971 it really saves my time a lot.

Would you help me once more time on the lowercase issue.
For example :
* If I type su then nothing appears as suggestion.
* If I type Su (1st char is uppercase) then Suzuki appears as suggestion.

I think will be better if I type su (all lowercase) then Suzuki would appears as a suggestion.

I've tried to put toLowerCase() on some possible places (that I can think of) but still not getting a proper solution.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39675877
replace :
if(ac[i].name == value) {

Open in new window

by :
if(ac[i].name.toLowerCase() == value.toLowerCase()) {

Open in new window

0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

by:tobece
ID: 39675960
Actually that was where I placed toLowerCase() but seems like nothing changes?

Typed su (all lowercase) : Suggestion NONE
Typed Su : Suggestion Suzuki
Typed SU (all uppercase) : Suggestion NONE
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39676006
Look like the autocomplete type cell don't support that :
http://handsontable.com/demo/autocomplete.html

you should ask the developer to add this in their plugin as I don't see any solution outside the plugin
0
 

Author Closing Comment

by:tobece
ID: 39678416
@leakim971:
OK thanks for the codes.
I'll be contacting them regarding the issue of lowercase letters in autocomplete.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Bootstrap Form 16 41
Debugging Html 8 32
Pass SQL to JSON. Page is in classic ASP and using Jquery 4 40
Keyup listener getting fired on initial page load 1 16
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

733 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