所以我有這樣的事情,現在http://codepen.io/anon/pen/bBgVOL使用多個參數篩選數據的最有效方法?
它需要的位置,並將其傳遞給GET AJAX調用,它返回我的緯度/經度然後傳遞,爲我的控制器。 (是的,我知道這是凌亂)
@RequestMapping(value = "/rentals", method = RequestMethod.GET)
public String rentResults(@RequestParam(value = "page", required = false) Integer pageNumber, @RequestParam(value = "lat", required = false) String lat, @RequestParam(value = "lng", required = false) String lng, @RequestParam(value = "rad", required = false) String rad, Map model) {
int total = 25;
if (pageNumber == null) {
pageNumber = 1;
} else {
pageNumber = (pageNumber - 1) * total + 1;
}
List<ForRent> rentals = new ArrayList();
int count;
if (lat == null && lng == null && rad == null) {
rentals = forRentDao.getRentalsByPage(pageNumber, total);
count = forRentDao.getNumOfRentals();
} else {
count = forRentDao.RentalRadiusCount(lat, lng, rad);
rentals = forRentDao.RentalRadius(lat, lng, rad, pageNumber, total);
String latParam = "&lat=" + lat;
String lngParam = "&lng=" + lng;
String radParam = "&rad=" + rad;
model.put("latParam", latParam);
model.put("lngParam", lngParam);
model.put("radParam", radParam);
}
int page;
if (count % total == 0) {
page = (count/total);
} else {
page = 1 + (count/total);
}
List<Integer> pages = new ArrayList();
for (int i = 1; i <= page; i++) {
pages.add(i);
}
model.put("pages", pages);
model.put("rentals", rentals);
boolean rent = true;
model.put("rent", rent);
return "rent";
}
JS
function initMap() {
var input = /** @type {!HTMLInputElement} */(
document.getElementById('address'));
var autocomplete = new google.maps.places.Autocomplete(input);
};
$('#searchRadius').on('click', function (e) {
var lati;
var lng;
var address = $('#address').val();
var rad = $('#radius').val();
var select = document.getElementById('sel');
var choice = select.value;
e.preventDefault();
$.ajax({
type: "GET",
url: "http://www.mapquestapi.com/geocoding/v1/address?key=&location=" + address,
beforeSend: function (xhr) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-type", "application/json");
},
success: function postForm(response) {
lati = response.results[0].locations[0].latLng.lat;
lng = response.results[0].locations[0].latLng.lng;
if (rad == null) {
rad = '10';
}
console.log(lati, lng, rad);
var data = JSON.stringify({
lat: lati,
lng: lng,
rad: rad
});
if (choice == 1) {
$.ajax({
url: contextRoot + "/map/radius",
type: "POST",
data: data,
dataType: 'json',
beforeSend: function (xhr) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-type", "application/json");
},
success: function (data, status) {
console.log(data);
window.location = contextRoot + "/rent/rentals?lat=" + data.lat + "&lng=" + data.lng + "&rad=" + data.rad;
},
error: function (data, status) {
alert("bad api call");
console.log(status);
}
});
}
},
error: function (data, status) {
console.log(data.errors);
}}
);
});
所以現在它要麼返回租金的列表,而緯度/經度/半徑參數,否則將返回與應用的參數列表。
我需要大約5-10多個參數添加到這個(例如http://codepen.io/anon/pen/QGdjoB)
我有我的控制器設置,現在最有可能不會工作,將是一個巨大的頭痛前進,因爲我要的方式需要大量基於參數的if/else語句。我正在使用SQL查詢過濾數據。返回所有數據並將其過濾到內存中還是更好/更有效的方法來設置此控制器響應?
謝謝!