Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!---   quick and dirty example
  2.         if the url term key exists (which comes from jquery autocomplete by default)
  3.         run whats in the cfif block, filter the query using querytoarrayofstructures for simplicity
  4.         and because client may not have "keep original case" turned on in the Railo admin, lower case everything.
  5.         exit out and return the json results
  6. --->
  7. <cfscript>
  8. /**
  9.  * http://www.cflib.org/udf/querytoarrayofstructures
  10.  * Converts a query object into an array of structures.
  11.  *
  12.  * @param query      The query to be transformed
  13.  * @return This function returns a structure.
  14.  * @author Nathan Dintenfass (nathan@changemedia.com)
  15.  * @version 1, September 27, 2001
  16.  */
  17. function QueryToArrayOfStructures(theQuery){
  18.     var theArray = arraynew(1);
  19.     var cols = ListtoArray(theQuery.columnlist);
  20.     var row = 1;
  21.     var thisRow = "";
  22.     var col = 1;
  23.     for(row = 1; row LTE theQuery.recordcount; row = row + 1){
  24.         thisRow = structnew();
  25.         for(col = 1; col LTE arraylen(cols); col = col + 1){
  26.             thisRow[cols[col]] = theQuery[cols[col]][row];
  27.         }
  28.         arrayAppend(theArray,duplicate(thisRow));
  29.     }
  30.     return(theArray);
  31. }
  32. </cfscript>
  33. <cfif structKeyExists(url,'term')>
  34.     <cfset dbase = query(
  35.         id      :[1,2,3,4,5,6],
  36.         emp_name:["George","John","Thomas","James","Andrew","Martin"],
  37.         active  :[true,true,true,true,true,false]
  38.     ) />
  39.     <cfquery name="results" dbtype="query">
  40.         SELECT
  41.             emp_name as label,
  42.             id
  43.         FROM dbase
  44.         WHERE active = true
  45.         AND emp_name LIKE <cfqueryparam value="%#url.term#%" cfsqltype="cf_sql_varchar" />
  46.     </cfquery>
  47.     <cfset resultsArray = QueryToArrayOfStructures(results) />
  48.     <cfset resultsJson = lCase( serializeJson(resultsArray) ) />
  49.     <cfset WriteOutput( resultsJson ) />
  50.     <cfabort>
  51. </cfif>
  52.  
  53. <!doctype html>
  54. <html lang="en">
  55. <head>
  56.     <meta charset="utf-8">
  57.     <title>jQuery UI Autocomplete - Multiple, remote</title>
  58.     <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
  59.     <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  60.     <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
  61.     <link rel="stylesheet" href="/resources/demos/style.css">
  62.     <style>
  63.         .ui-autocomplete-loading {
  64.             background: white url("images/ui-anim_basic_16x16.gif") right center no-repeat;
  65.     }
  66.     </style>
  67.     <script>
  68.         $(function() {
  69.             function log( message ) {
  70.                 $( "<div>" ).text( message ).prependTo( "#log" );
  71.                 $( "#log" ).scrollTop( 0 );
  72.             }
  73.  
  74.             $( "#employees" ).autocomplete({
  75.                 source: "autocompleteexample.cfm",
  76.                 minLength: 2,
  77.                 select: function( event, ui ) {
  78.                     log( ui.item ?
  79.                     "Selected: " + ui.item.value + " aka " + ui.item.id :
  80.                     "Nothing selected, input was " + this.value );
  81.                 }
  82.             });
  83.         });
  84.     </script>
  85. </head>
  86. <body>
  87.     <div class="ui-widget">
  88.         <label for="employees">Employees: </label>
  89.         <input id="employees" size="50">
  90.     </div>
  91.     <div class="ui-widget" style="margin-top:2em; font-family:Arial">
  92.         Result:
  93.         <div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div>
  94.     </div>
  95. </body>
  96. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement