I have implemented a searchable drop-down using jquery and visual force. Whenever I start typing a string it will search the database and bring all the matched users.
I am using a jquery to call a method in my controller and search the database using this string, then returning the results back to jquery where I'm displaying the results in a drop-down bar.
But this is showing all the users including users who are not visible to me.
VisualForce:
<span class='slds-grid_align-center slds-size_1-of-4' >
<apex:repeat value="{!conList2}" var="con" id="conRepeatId">
<apex:inputText label="Input" value="{!con.Name}" id="conName" onfocus="autocompleteonfocus('{!1}');" styleClass="conName{!1}"/>
</apex:repeat>
</span>
Jquery Script:
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js"></script>
<script>
$j = jQuery.noConflict();
function autocompleteonfocus(row){
var selectedId = null;
$j('input[id$=conName],input[id$=conId]').autocomplete({
minLength: 2,
delay: 500,
source: function( request, response ) {
CalendarController.checkThisMethod(request.term, function(res, evt){
console.log(res);
var result = [];
res.forEach(function(record) {
result.push( {
label: record.Name,
Name : record.Name,
Id : record.Id
});
});
response(result);
});
},
select: function(event, ui) {
console.log(ui);
event.preventDefault();
$j(".conName"+row).val(ui.item.Name);
selectedId = ui.item.value;
selectedUserChanged(ui.item.Id);
},
});
}
</script>
Controller:
global with sharing class CalendarController {
public ApexPages.StandardController controller;
public list<User> conList2 {get;set;}
public List<User> conListNew {get;set;}
public static list<User> checkName {get;set;}
public CalendarController(ApexPages.StandardController controller){
conList2 = new list<user>(); // do i need to do this step
User c = new User();// do i need to do this step
conList2.add(c);// do i need to do this step
}
@RemoteAction
public static list<User> checkThisMethod(String checkName){
String appendPercentile = '%' + checkName + '%';
List<user> conListNew = new List<user>();
conListNew.addall([select id,name from user where name like :appendPercentile]);
system.debug('conListNew Value is: '+conListNew);
return conListNew;
}
}