@fractastical Visualforce jQuery autocomplete Controller


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  1. public with sharing class EnhancedLookupController extends EnhancedComponentController {
  2.  
  3. String secondDisplayColumn;
  4. public String displayColumn { get; set; }
  5. public String objectToLookup { get; set; }
  6. public String displayName { get; set; }
  7. public String filterString { get; set; }
  8. public transient List<JsonObject> jsonObjs { get; set; }
  9. public Boolean bigset { get; set; }
  10.  
  11. private transient String jsonData;
  12. public List<String> paramValues { get; set; }
  13. public List<String> paramKeys { get; set; }
  14. public Map<String,String> params { get; set; }
  15.  
  16.  
  17. public String getOnLoad(){
  18.  
  19. onLoad();
  20.  
  21. if(bigset == null)
  22. bigset = false;
  23.  
  24. List<String> columns = new List<String>();
  25.  
  26. List<String> displayCols = new List<String>();
  27.  
  28. if (displayColumn == null)
  29. displayColumn = 'name';
  30. else {
  31.  
  32. displayCols = displayColumn.split('[.]');
  33. if(displayCols.size() > 1)
  34. {
  35. displayColumn = displayCols.get(0);
  36. secondDisplayColumn = displayCols.get(1);
  37. }
  38.  
  39. }
  40.  
  41. String initialize;
  42.  
  43. //columns.add('id');
  44. columns.add(displayColumn);
  45.  
  46. if(!bigset)
  47. {
  48. jsonData = JsonUtils.getJsonFromObject(objectToLookup, columns, filterString);
  49. initialize = 'var ' + uid + 'data = ' + jsonData + ';\n' +
  50.  
  51. '$(".L' + uid + '").autocomplete(' + uid + 'data, { \n' +
  52. '\n' +
  53. ' formatItem: function(item) { \n' +
  54. ' return item.' + displayColumn + '; \n' +
  55. ' } \n' +
  56. '\n' +
  57. ' }).result(function(event, item) {\n' +
  58. ' $(".' + uid + '").val(item.id);\n' +
  59. ' }); \n';
  60. }
  61. else
  62. {
  63. uid = 'LL' + uid;
  64. initialize = '' +
  65. ' $(".L' + uid + '").autocomplete({\n' +
  66.  
  67. ' source: function(request, response) {\n' +
  68. ' $.getJSON("' + System.Page.largeLookup.getUrl() + '?otl=' + objectToLookup + '&", {\n' +
  69. // Not sure if we need this
  70. // see: http://www.tgerm.com/2010/02/visualforce-salesforce-jquery-ajax-how.html
  71. // "core.apexpages.devmode.url" :'1',
  72. ' term: request.term\n' +
  73.  
  74. ' }, response);\n' +
  75. ' },\n' +
  76. ' select: function(event, ui) {\n' +
  77.  
  78. ' $(".' + uid +'").val(ui.item.id);\n' +
  79.  
  80. ' },\n' +
  81. ' minLength: 2\n' +
  82.  
  83. ' });\n';
  84. }
  85.  
  86.  
  87.  
  88. if (myValueHolder != null)
  89. initialize += '$(".' + uid + '").val("' + (String) myValueHolder + '");\n';
  90.  
  91.  
  92. addOnLoadJavascriptToParentController(initialize);
  93. return '';
  94.  
  95. }
  96.  
  97. public override Object getCastedValue() {
  98.  
  99. String s = (String) myValueHolder;
  100. if(s == '')
  101. return null;
  102. else
  103. return myValueHolder;
  104. }
  105.  
  106.  
  107. /** invoked on an Ajax request */
  108. public void doSearch() {
  109.  
  110. params = ApexPages.currentPage().getParameters();
  111. paramValues = params.values();
  112. paramKeys = new List <String>();
  113. paramKeys.addAll(params.keySet());
  114.  
  115.  
  116. for(String s: paramKeys)
  117. System.debug('k:' + s);
  118. for(String s: paramValues)
  119. System.debug('v:' + s);
  120.  
  121. if(params.get('otl') != null)
  122. objectToLookup = params.get('otl');
  123. else if (objectToLookup == null)
  124. objectToLookup = 'Account';
  125.  
  126. if(params.get('dc') != null)
  127. displayColumn = params.get('dc');
  128. else if (displayColumn == null)
  129. displayColumn = 'name';
  130.  
  131.  
  132. if(params.get('filterstring') != null)
  133. filterString = params.get('filterstring');
  134. else if (filterString == null)
  135. filterString = '';
  136.  
  137.  
  138. // Do SOQL query to see if there are any records !
  139. List<Sobject> records = getRecords(params.get('term'));
  140.  
  141. if (!records.isEmpty()) {
  142. // Jsonify the results !
  143. jsonObjs = new List<JSONObject>();
  144. for (Sobject o : records) {
  145. JSONObject cjson = new JSONObject();
  146. cjson.putOpt('id', new JSONObject.value((String) o.get('id')));
  147. if(secondDisplayColumn != null)
  148. cjson.putOpt('label', new JSONObject.value((String) o.get(displayColumn) + '(' + ( (String) o.get(secondDisplayColumn) ) + ')' ));
  149. else
  150. cjson.putOpt('label', new JSONObject.value((String) o.get(displayColumn)));
  151.  
  152. cjson.putOpt('value', new JSONObject.value((String) o.get(displayColumn)));
  153. jsonObjs.add(cjson);
  154.  
  155. }
  156.  
  157. }
  158. }
  159.  
  160. // Does the SOQL query, using Ajax request data
  161. public List<SObject> getRecords(String filter) {
  162. List<SObject> records = new List<SObject>();
  163.  
  164. String limitString;
  165. if(filter.length() > 3)
  166. limitString = 'limit 100';
  167. else
  168. limitString = 'limit 8';
  169.  
  170.  
  171. if (filter != null && filter.trim().length() > 0){
  172.  
  173.  
  174. String completeFilter = '%' + String.escapeSingleQuotes(filter) + '%';
  175. if(filterString.trim().startsWith('WHERE'))
  176. filterString = 'AND' + filterString.trim().substring(5, filterString.length());
  177.  
  178. String queryString = 'select id, ' + displayColumn;
  179. if(secondDisplayColumn != null)
  180. queryString += ', ' + secondDisplayColumn;
  181. queryString += ' from ' + objectToLookup + ' where ' + displayColumn + ' like \'' + completeFilter + '\' ';
  182.  
  183. if(filterString.toLowerCase().contains('limit'))
  184. queryString += filterString;
  185. else
  186. queryString += filterString + ' ' + limitString;
  187.  
  188. System.debug('QUERYSTRING: ' + queryString);
  189.  
  190. records = Database.query(queryString);
  191.  
  192. }
  193. else {
  194.  
  195. String queryString = 'select id, ' + displayColumn;
  196. if(secondDisplayColumn != null)
  197. queryString += ', ' + secondDisplayColumn;
  198. queryString += ' from ' + objectToLookup + ' ';
  199.  
  200. if(filterString.toLowerCase().contains('limit'))
  201. queryString += filterString;
  202. else
  203. queryString += filterString + ' ' + limitString;
  204.  
  205.  
  206. System.debug('QUERYSTRING: ' + queryString);
  207. records = Database.query(queryString);
  208.  
  209. }
  210. return records;
  211. }
  212.  
  213. // Returns the JSON result string
  214. public String getResult() {
  215. if(jsonObjs != null)
  216. return JsonUtils.jsonify(jsonObjs);
  217. else return '[ ]';
  218. }
  219.  
  220.  
  221.  
  222. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.