我需要根据 ColdFusion 查询生成带有类别的自动完成功能。这是我的查询:
<cfquery name = "GetEvents" datasource = "tcc">
SELECT '{ label: "' + cE_Title + '", category: "Tournaments" }' as cE_Title
,'{ label: "' + cE_Location + '", category: "Locations" }' as cE_Location
FROM CourseEvents
然后我把每条记录放到一个列表中,追加列表,然后把列表变成一个数组:
<cfset myList = ValueList(GetEvents.cE_Title)>
<cfset myList2 = ValueList(GetEvents.cE_Location)>
<cfset myListApp = ListAppend(myList,myList2)>
<cfset myArrayList = ListToArray(myListApp)>
这是我的脚本:
<script>
$.widget( "custom.catcomplete", $.ui.autocomplete, {
_enderMenu: function( ul, items ) {
var that = this,
currentCategory = "";
$.each( items, function( index, item ) {
if ( item.category != currentCategory ) {
ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
currentCategory = item.category;
}
that._renderItemData( ul, item );
});
}
});
</script>
<script>
$(function() {
<cfoutput>
var #toScript(myArrayList, "jsVar")#;
</cfoutput>
var availableTags = jsVar;
$( "#EventSearch" ).catcomplete({
delay: 0,
source: availableTags
});
});
</script>
结果应如下所示:
Tournaments
2nd Annual March Madness Scramble
River Run Golf Classic
Locations
Trump International Golf Club
Stoneybrook Golf and Country Club
但是,它们看起来像这样:
{ label: "2nd Annual March Madness Scramble"
category: "Tournaments" }
{ label: "River Run Golf Classic"
category: "Tournaments" }
{ label: "Trump International Golf Club"
category: "Locations" }
{ label: "Stoneybrook Golf and Country Club"
category: "Locations" }
这是一个演示:http ://www.thecoursecaddy.com/c/sandbox/golfevents.cfm
任何帮助,将不胜感激。