From fb014196dfc0caad2680be41d375a0120cb64db7 Mon Sep 17 00:00:00 2001 From: ValantineSuh Date: Wed, 14 Feb 2024 10:42:40 +0100 Subject: [PATCH] Search bar functionality --- app.py | 22 +++++++++++++++++++++- static/index.js | 21 +++++++++++++++++++++ templates/locationList.html | 31 +++++++++++++++++++++++++++++-- 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index 662c820..af9252c 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,4 @@ -from flask import Flask, render_template, request, redirect +from flask import Flask, render_template, request, redirect, jsonify, Response import sqlite3 from flask_sqlalchemy import SQLAlchemy @@ -151,5 +151,25 @@ def employee(): return render_template('add_employee.html') +@app.route('/search', methods=['GET']) +def search(): + res = None + + entity = request.args.get('entity') + query = request.args.get('query') + + if entity == 'location': + data = Location.query.filter(Location.location_name.contains(query)).all() + res = jsonify([{ + "location_name": r.location_name, "number_of_offices": r.number_of_offices, "head_quater_contact": r.number_of_offices + } for r in data]) + # elif entity == 'location': + # data = Location.query.filter(Location.location_name.contains(query)).all() + # res = jsonify(data) + else: + res = Response("Invalid entity", status=400) + + return res + if __name__ == '__main__': app.run(host='localhost', port=8080, debug=True) \ No newline at end of file diff --git a/static/index.js b/static/index.js index e69de29..d9e8703 100644 --- a/static/index.js +++ b/static/index.js @@ -0,0 +1,21 @@ +$(document).ready(function() { + $('#search-button').click(function() { + var query = $('#query').val(); + $.ajax({ + url: '/search', + type: 'GET', + data: { query: query }, + success: function(response) { + var results = response; + var resultsHtml = ''; + for (var i = 0; i < results.length; i++) { + resultsHtml += '

' + results[i] + '

'; + } + $('#search-results').html(resultsHtml); + }, + error: function(error) { + console.log('Search request failed:', error); + } + }); + }); + }); \ No newline at end of file diff --git a/templates/locationList.html b/templates/locationList.html index e259b3a..5b2f334 100644 --- a/templates/locationList.html +++ b/templates/locationList.html @@ -4,16 +4,42 @@ Online Inventory System + + + +
- +
@@ -37,6 +63,7 @@

Welcome To Our Online Inventory System

-->

Welcome To Our Online Inventory System

+
Add Location @@ -49,7 +76,7 @@

Welcome To Our Online Inventory System

Actions - + {% for location in locations %} {{ location.location_name }}