Commit a46e40a0 authored by Renán Sosa Guillen's avatar Renán Sosa Guillen

polygon info retrieval

parent 09251807
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
// Get the <datalist> and <input> elements. // // Get the <datalist> and <input> elements.
var input = document.getElementById('ajax-input'); // var input = document.getElementById('ajax-input');
var value = input; // var value = input.value;
var dataList = document.getElementById('polygon-list'); // var dataList = document.getElementById('polygon-list');
console.log(input); // console.log("VALUE:", value);
// Create a new XMLHttpRequest. // // Create a new XMLHttpRequest.
var request = new XMLHttpRequest(); // var request = new XMLHttpRequest();
//
// // Handle state changes for the request.
// request.onreadystatechange = function(response) {
// if (request.readyState === 4) {
// if (request.status === 200) {
// // Parse the JSON
// var jsonOptions = JSON.parse(request.responseText);
//
// // Loop over the JSON array.
// jsonOptions.forEach(function(item) {
// // Create a new <option> element.
// var option = document.createElement('option');
// // Set the value using the item in the JSON array.
// option.value = item;
// // Add the <option> element to the <datalist>.
// dataList.appendChild(option);
// });
//
// // Update the placeholder text.
// input.placeholder = "Search...";
// } else {
// // An error occured :(
// input.placeholder = "Couldn't load datalist options :(";
// }
// }
// };
//
// // Update the placeholder text.
// input.placeholder = "Loading options...";
//
// // Set up and make the request.
// request.open('POST', 'http://localhost:8080/catalog/searchsubmit/', true);
// request.send({
// value: value
// });
// Handle state changes for the request. $(document).ready(function () {
request.onreadystatechange = function(response) { var input = document.getElementById('ajax-input');
if (request.readyState === 4) { var dataList = document.getElementById('polygon-list');
if (request.status === 200) {
// Parse the JSON
var jsonOptions = JSON.parse(request.responseText);
// Loop over the JSON array.
jsonOptions.forEach(function(item) {
// Create a new <option> element.
var option = document.createElement('option');
// Set the value using the item in the JSON array.
option.value = item;
// Add the <option> element to the <datalist>.
dataList.appendChild(option);
});
// Update the placeholder text.
input.placeholder = "Search...";
} else {
// An error occured :(
input.placeholder = "Couldn't load datalist options :(";
}
}
};
// Update the placeholder text. $('#ajax-input').on('input', function(e) {
input.placeholder = "Loading options...";
// Set up and make the request. var value = $(this).val();
request.open('POST', 'http://localhost:8080/catalog/searchsubmit/', true); if (value === '') return;
request.send({ console.log(value);
value: value // console.log("TOKEN: ", document.getElementsByName('csrfmiddlewaretoken')[0].value);
$.ajax({
type: "POST",
url: "http://localhost:8080/catalog/searchsubmit/",
data: {
'csrfmiddlewaretoken': document.getElementsByName('csrfmiddlewaretoken')[0].value,
'value': value
},
dataType: 'json',
success: function(data) {
// console.log("DATA: ", data);
// data.forEach(function(polygon) {
// console.log(polygon);
// // Create a new <option> element
// var option = document.createElement('option');
// // Set the value using the item in the JSON array
// option.value = polygon.city + " " + polygon.state;
// // Add the <option> element to the <datalist>.
// dataList.appendChild(option);
// });
$('#ajax-input').autocomplete({
source: data
});
}
});
});
}); });
\ No newline at end of file
...@@ -125,8 +125,8 @@ ...@@ -125,8 +125,8 @@
<div class="input-group"> <div class="input-group">
<input type="text" id="ajax-input" list="polygon-list" name="value" class="form-control" placeholder="Search..."> <input type="text" id="ajax-input" list="polygon-list" name="value" class="form-control" placeholder="Search...">
<datalist id="polygon-list"> <datalist id="polygon-list">
<option value="merida"> {# <option value="merida">#}
<option value="otro"> {# <option value="otro">#}
</datalist> </datalist>
<span class="input-group-btn"> <span class="input-group-btn">
<button type="submit" name="search" id="search-btn" class="btn btn-flat"><i class="fa fa-search"></i> <button type="submit" name="search" id="search-btn" class="btn btn-flat"><i class="fa fa-search"></i>
......
No preview for this file type
...@@ -67,20 +67,26 @@ class SearchSubmitView(View): ...@@ -67,20 +67,26 @@ class SearchSubmitView(View):
for polygon in polygonList: for polygon in polygonList:
polygonInfo = json.loads(polygon.json_info) polygonInfo = json.loads(polygon.json_info)
# data.append({
# 'city' : polygon.name,
# 'state' : self.state_parser[polygonInfo['properties']['CVE_ENT']],
# 'geojson': polygon.json_info
# })
data.append({ data.append({
'city' : polygon.name, 'city': polygonInfo['properties']['NOMGEO'],
'state' : self.state_parser[polygonInfo['properties']['CVE_ENT']], 'state': self.state_parser[polygonInfo['properties']['CVE_ENT']],
'geojson': polygon.json_info 'geojson': polygonInfo
}) })
context = { # context = {
'searchValue': searchValue, # 'searchValue': searchValue,
'data': json.dumps(data, ensure_ascii=True) # 'data': json.dumps(data, ensure_ascii=True)
} # }
rendered_template = template.render(context, request) # rendered_template = template.render(context, request)
print("DATA:", data) print("DATA:", data)
return HttpResponse(rendered_template, content_type='application/json') # return HttpResponse(rendered_template, content_type='application/json')
return HttpResponse(json.dumps(data, ensure_ascii=True), content_type='application/json')
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
......
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment