Commit 94bd0ac2 authored by Renán Sosa Guillen's avatar Renán Sosa Guillen

option list updated

parent 8a9af8f3
......@@ -40,57 +40,113 @@
// request.send({
// value: value
// });
var OPT_LIST_IS_HIDDEN = true;
var makeRequest = function (value) {
var pol_element = document.getElementById('polygon-list');
$.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.polygonList);
while (pol_element.firstChild) {
pol_element.removeChild(pol_element.firstChild);
}
data.polygonList.forEach(function (polygon) {
$('#polygon-list').append('<a href="#"><li class="pol">' + polygon.city + ", " + polygon.state + '</li></a>');
});
if (OPT_LIST_IS_HIDDEN) {
$('#option-list').show();
OPT_LIST_IS_HIDDEN = false;
}
}
});
}
$('#ajax-input').focus(function () {
console.log('in');
if (OPT_LIST_IS_HIDDEN && $(this).val() != '') {
// console.log('makeRequest');
makeRequest($(this).val());
}
});
$(document).click(function (event) {
if (!OPT_LIST_IS_HIDDEN) {
//if click is triggered outside '#option-list' element remove the list
if(!$(event.target).closest('#option-list').length > 0 ) {
$('#option-list').hide();
var pol_element = document.getElementById('polygon-list');
while (pol_element.firstChild) {
pol_element.removeChild(pol_element.firstChild);
}
OPT_LIST_IS_HIDDEN = true;
}
}
});
$(document).ready(function () {
var input = document.getElementById('ajax-input');
var dataList = document.getElementById('polygon-list');
// var input = document.getElementById('ajax-input');
// var dataList = document.getElementById('polygon-list');
var pol_element = document.getElementById('polygon-list');
$('#ajax-input').on('input', function(e) {
var value = $(this).val();
if (value === '') {
$('#option-list').hide();
var nodeList = document.getElementById('polygon-list')
while (nodeList.firstChild) {
nodeList.removeChild(nodeList.firstChild);
}
return
}
console.log(value);
// console.log("TOKEN: ", document.getElementsByName('csrfmiddlewaretoken')[0].value);
var value = $(this).val();
if (value === '') {
$('#option-list').hide();
while (pol_element.firstChild) {
pol_element.removeChild(pol_element.firstChild);
}
OPT_LIST_IS_HIDDEN = true;
return
}
console.log(value);
// console.log("TOKEN: ", document.getElementsByName('csrfmiddlewaretoken')[0].value);
makeRequest(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.polygonList);
// while (pol_element.firstChild) {
// pol_element.removeChild(pol_element.firstChild);
// }
$.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.polygonList);
// 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);
// });
data.polygonList.forEach(function (polygon) {
$('#polygon-list').append('<li class="pol">' + polygon.city + " " + polygon.state + '</li>');
});
$('#option-list').show();
// $('#ajax-input').autocomplete({
// source: data.polygonList,
// minLength: 3
// });
}
});
});
// data.polygonList.forEach(function (polygon) {
// $('#polygon-list').append('<a href="#"><li class="pol">' + polygon.city + ", " + polygon.state + '</li></a>');
// });
$('#option-list').focusout(function () {
$(this).hide();
// if (OPT_LIST_IS_HIDDEN) {
// $('#option-list').show();
// OPT_LIST_IS_HIDDEN = false;
// }
// }
// });
});
// $('#option-list').focusout(function () {
// $(this).hide();
// });
});
\ No newline at end of file
......@@ -9,7 +9,7 @@ from django.urls import reverse
from django.views.generic.base import View
from django.template import loader
from urllib import urlencode
import requests, json
import requests, json, unicodedata
# Create your views here.
......@@ -54,46 +54,29 @@ class SearchSubmitView(View):
template = 'map.html'
# template = 'search_submit.html'
state_parser = {
'01': "Aguascalientes",
'02': "Baja California",
'03': "Baja California Sur",
'04': "Campeche",
'05': "Chiapas",
'06': "Chihuahua",
'07': "Ciudad de México",
'08': "Coahuila",
'09': "Colima",
'10': "Durango",
'11': "Guanajuato",
'12': "Guerrero",
'13': "Hidalgo",
'14': "Jalisco",
'15': "México",
'16': "Michoacán",
'17': "Morelos",
'18': "Nayarit",
'19': "Nuevo León",
'20': "Oaxaca",
'21': "Puebla",
'22': "Querétaro",
'23': "Quintana Roo",
'24': "San Luis Potosí",
'25': "Sinaloa",
'26': "Sonora",
'27': "Tabasco",
'28': "Tamaulipas",
'29': "Tlaxcala",
'30': "Veracruz",
'31': "Yucatán",
'32': "Zacatecas"
'01': "Aguascalientes", '02': "Baja California", '03': "Baja California Sur", '04': "Campeche",
'05': "Chiapas", '06': "Chihuahua", '07': "Ciudad de México", '08': "Coahuila",
'09': "Colima", '10': "Durango", '11': "Guanajuato", '12': "Guerrero",
'13': "Hidalgo", '14': "Jalisco", '15': "México", '16': "Michoacán",
'17': "Morelos", '18': "Nayarit", '19': "Nuevo León", '20': "Oaxaca",
'21': "Puebla", '22': "Querétaro", '23': "Quintana Roo", '24': "San Luis Potosí",
'25': "Sinaloa", '26': "Sonora", '27': "Tabasco", '28': "Tamaulipas",
'29': "Tlaxcala", '30': "Veracruz", '31': "Yucatán", '32': "Zacatecas"
}
def strip_accents(self, s):
return ''.join((c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn'))
def post(self, request):
template = loader.get_template(self.template)
searchValue = request.POST.get('value', '')
searchValue = self.strip_accents(searchValue).lower()
## A simple query for Polygon objects whose name is 'searchValue'
polygonList = Polygon.objects.filter(name=searchValue)
# polygonList = Polygon.objects.filter(name=searchValue)
polygonList = Polygon.objects.filter(name__startswith=searchValue)
# data = []
list = []
......
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