webgui/www/extras/yui/examples/autocomplete/ac_fn_multfields_clean.html
JT Smith 20f8df1291 upgrading to YUI 2.6
data tables are going to need some work yet, but the other stuff seems to be working 100%
2008-10-22 23:53:29 +00:00

125 lines
4.4 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Custom Function to Search Different Fields at Runtime</title>
<style type="text/css">
/*margin and padding on body element
can introduce errors in determining
element position and are not recommended;
we turn them off as a foundation for YUI
CSS treatments. */
body {
margin:0;
padding:0;
}
</style>
<link rel="stylesheet" type="text/css" href="../../build/fonts/fonts-min.css" />
<link rel="stylesheet" type="text/css" href="../../build/autocomplete/assets/skins/sam/autocomplete.css" />
<script type="text/javascript" src="../../build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="../../build/animation/animation-min.js"></script>
<script type="text/javascript" src="../../build/datasource/datasource-min.js"></script>
<script type="text/javascript" src="../../build/autocomplete/autocomplete-min.js"></script>
<!--begin custom header content for this example-->
<style type="text/css">
#myAutoComplete {
width:15em; /* set width here or else widget will expand to fit its container */
padding-bottom:2em;
}
</style>
<!--end custom header content for this example-->
</head>
<body class=" yui-skin-sam">
<h1>Custom Function to Search Different Fields at Runtime</h1>
<div class="exampleIntro">
<p>This example uses a FunctionDataSource that performs string matching against different fields of a two-dimensional array at runtime, depending on whether the input is a letter or a number. Since the data for this example is already loaded into memory, queries should be quite fast to return data, but use of the custom function allows a more complex search algorithm. When the searched field is determined, the DataSource schema also needs to be updated on the fly. A custom formatter allows users to see both state and area code values for each result.</p>
</div>
<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== -->
<h3>Enter a state or an area code:</h3>
<div id="myAutoComplete">
<input id="myInput" type="text">
<div id="myContainer"></div>
</div>
<script type="text/javascript" src="assets/js/data.js"></script>
<script type="text/javascript">
YAHOO.example.FnMultipleFields = function(){
var allData = YAHOO.example.Data.arrayAreaCodesStates;
// Track each interaction if it is against a state or an area code
var nSearchField;
// Define a custom search function
var searchAreaCodesAndStates = function(sQuery) {
var allMatches = [],
item, i, l;
// 0 for area code, 1 for state
nSearchField = (YAHOO.lang.isNumber(sQuery*1)) ? 0 : 1;
for(i=0, l=allData.length; i<l; i++) {
item = allData[i];
// State must be made case-insensitve and make the state return as index 0
if(nSearchField) {
if(item[nSearchField].toLowerCase().indexOf(sQuery.toLowerCase()) === 0) {
allMatches[allMatches.length] = [item[1], item[0]];
}
}
// Area codes are simpler
else {
if(item[nSearchField].indexOf(sQuery) === 0) {
allMatches[allMatches.length] = item;
}
}
}
// States should be sorted alphabetically
// Define schema on the fly (since the return order changes)
if(nSearchField) {
allMatches.sort();
this.responseSchema = {fields: ["state", "areacode"]};
}
else {
this.responseSchema = {fields: ["areacode", "state"]};
}
return allMatches;
};
// Use a FunctionDataSource
var oDS = new YAHOO.util.FunctionDataSource(searchAreaCodesAndStates);
// Instantiate AutoComplete
var oAC = new YAHOO.widget.AutoComplete("myInput", "myContainer", oDS);
oAC.useShadow = true;
oAC.resultTypeList = false;
oAC.formatResult = function(oResultData, sQuery, sResultMatch) {
return (sResultMatch + " (" + ((nSearchField) ? oResultData.areacode : oResultData.state) + ")");
};
return {
fnSearch: searchAreaCodesAndStates,
oDS: oDS,
oAC: oAC
};
}();
</script>
<!--END SOURCE CODE FOR EXAMPLE =============================== -->
</body>
</html>