Autocomplete :
$("#parentBranchName").autocomplete({
source : function(request, response) {
$.ajax({
url : projectUrl + "/branchAutoComplete?branch=" + branchName,
type : "GET",
data : {
term : request.term
},
dataType : "json",
success : function(data) {
response($.map(data, function(item) {
console.log(item);
return {
label : item.name,
value : item.id,
};
}));
}
});
},
focus : function(event, ui) {
$("#parentBranchName").val(ui.item.label);
return false;
},
select : function(event, ui) {
event.preventDefault();
this.value = ui.item.label;
// $(this).next().val(ui.item.value);
$("#selectedParentBranchName").val(ui.item.label);
$("#parentBranchId").val(ui.item.value);
return false;
}
});
})
Controller:
@RequestMapping(value = "/stationAutoComplete")
public @ResponseBody List<Station> stationAutoComplete(@RequestParam(value = "stationName") String stationName) {
return stationService.getAllStationIdAndNameByName(stationName);
}
Dao:
public List<Station> getAllStationIdAndNameByName(String stationName){
Query query = entityManager.createQuery("SELECT NEW Station (station.id, station.name) FROM Station station WHERE"
+ " (station.deleteIndicator = false) AND station.name LIKE :searchString");
return (List<Station>) query.setParameter("searchString", "%"+ stationName +"%").getResultList();
}
$("#parentBranchName").autocomplete({
source : function(request, response) {
$.ajax({
url : projectUrl + "/branchAutoComplete?branch=" + branchName,
type : "GET",
data : {
term : request.term
},
dataType : "json",
success : function(data) {
response($.map(data, function(item) {
console.log(item);
return {
label : item.name,
value : item.id,
};
}));
}
});
},
focus : function(event, ui) {
$("#parentBranchName").val(ui.item.label);
return false;
},
select : function(event, ui) {
event.preventDefault();
this.value = ui.item.label;
// $(this).next().val(ui.item.value);
$("#selectedParentBranchName").val(ui.item.label);
$("#parentBranchId").val(ui.item.value);
return false;
}
});
})
Controller:
@RequestMapping(value = "/stationAutoComplete")
public @ResponseBody List<Station> stationAutoComplete(@RequestParam(value = "stationName") String stationName) {
return stationService.getAllStationIdAndNameByName(stationName);
}
Dao:
public List<Station> getAllStationIdAndNameByName(String stationName){
Query query = entityManager.createQuery("SELECT NEW Station (station.id, station.name) FROM Station station WHERE"
+ " (station.deleteIndicator = false) AND station.name LIKE :searchString");
return (List<Station>) query.setParameter("searchString", "%"+ stationName +"%").getResultList();
}
No comments:
Post a Comment