From df8d9780880351984555256787d494ed1077bdc1 Mon Sep 17 00:00:00 2001 From: Mokaddem Date: Thu, 24 Nov 2016 13:31:31 +0100 Subject: [PATCH] Added dynamic data loading in dataTable in search.html --- var/www/Flask_server.py | 16 ++++-- .../templates/important_paste_by_module.html | 2 +- var/www/templates/search.html | 51 ++++++++++++++----- 3 files changed, 50 insertions(+), 19 deletions(-) diff --git a/var/www/Flask_server.py b/var/www/Flask_server.py index 2d3aa196..2b01b6fa 100755 --- a/var/www/Flask_server.py +++ b/var/www/Flask_server.py @@ -347,6 +347,7 @@ def search(): c = [] #preview of the paste content paste_date = [] paste_size = [] + num_elem_to_get = 50 # Search filename for path in r_serv_pasteName.smembers(q[0]): @@ -370,7 +371,7 @@ def search(): from whoosh.qparser import QueryParser with ix.searcher() as searcher: query = QueryParser("content", ix.schema).parse(" ".join(q)) - results = searcher.search_page(query, 1, pagelen=10) + results = searcher.search_page(query, 1, pagelen=num_elem_to_get) for x in results: r.append(x.items()[0][1]) paste = Paste.Paste(x.items()[0][1]) @@ -381,7 +382,10 @@ def search(): curr_date = curr_date[0:4]+'/'+curr_date[4:6]+'/'+curr_date[6:] paste_date.append(curr_date) paste_size.append(paste._get_p_size()) - return render_template("search.html", r=r, c=c, query=request.form['query'], paste_date=paste_date, paste_size=paste_size, char_to_display=max_preview_modal) + results = searcher.search(query) + num_res = len(results) + + return render_template("search.html", r=r, c=c, query=request.form['query'], paste_date=paste_date, paste_size=paste_size, char_to_display=max_preview_modal, num_res=num_res) @app.route("/get_more_search_result", methods=['POST']) @@ -389,7 +393,8 @@ def get_more_search_result(): query = request.form['query'] q = [] q.append(query) - offset = int(request.form['offset']) + page_offset = int(request.form['page_offset']) + num_elem_to_get = 50 path_array = [] preview_array = [] @@ -405,7 +410,7 @@ def get_more_search_result(): from whoosh.qparser import QueryParser with ix.searcher() as searcher: query = QueryParser("content", ix.schema).parse(" ".join(q)) - results = searcher.search_page(query, offset, pagelen=10) + results = searcher.search_page(query, page_offset, num_elem_to_get) for x in results: path_array.append(x.items()[0][1]) paste = Paste.Paste(x.items()[0][1]) @@ -421,7 +426,8 @@ def get_more_search_result(): to_return["preview_array"] = preview_array to_return["date_array"] = date_array to_return["size_array"] = size_array - if len(path_array) < 10: #pagelength + print "len(path_array)="+str(len(path_array)) + if len(path_array) < num_elem_to_get: #pagelength to_return["moreData"] = False else: to_return["moreData"] = True diff --git a/var/www/templates/important_paste_by_module.html b/var/www/templates/important_paste_by_module.html index 535925a1..ca98ce59 100644 --- a/var/www/templates/important_paste_by_module.html +++ b/var/www/templates/important_paste_by_module.html @@ -28,7 +28,7 @@
- +

diff --git a/var/www/templates/search.html b/var/www/templates/search.html index 22bb31d2..e7351ca1 100644 --- a/var/www/templates/search.html +++ b/var/www/templates/search.html @@ -83,7 +83,6 @@
{{ r|length }} Results for "{{ query }} -
@@ -114,7 +113,11 @@ {% endfor %} +
+ + Totalling {{ num_res }} items
+
@@ -123,6 +126,7 @@ +
@@ -130,17 +134,29 @@ var search_table; var last_clicked_paste; var can_change_modal_content = true; + var page_offset; + var offset; + var all_loaded; + var init_num_of_elements_in_table; + var query; + var pagelen = 50; $(document).ready(function(){ $('[data-toggle="tooltip"]').tooltip(); $("#button_show_path").hide(); - var search_table = $('#myTable').DataTable(); + search_table = $('#myTable').DataTable(); - var prev_query = "{{ query }}"; - var offset = 2; - var init_num_of_elements_in_table = parseInt("{{ r|length }}"); // Comes from the file search - load_search_data(init_num_of_elements_in_table, search_table, prev_query, offset); + query = "{{ query }}"; + offset = 0; + page_offset = 2; //page 1 already loaded + all_loaded = false; + init_num_of_elements_in_table = parseInt("{{ r|length }}"); // Comes from the file search + + + if (init_num_of_elements_in_table == pagelen) { + $("#load_more_json_button1").show(); + } }); @@ -149,27 +165,36 @@ // Loop to recover all the data from get_more_search_results // And add it dynamically top the dataTable + function add_entries() { //Used to disable the button before going to the big loop + $("#load_more_json_button1").attr('disabled','disabled'); + setTimeout(function() { load_search_50_data();}, 50); + } - function load_search_data(init_num_of_elements_in_table, search_table, prev_query, offset) { - var options = { query: prev_query, offset: offset }; + function load_search_50_data() { + var options = { query: query, page_offset: page_offset }; $.post( "{{ url_for('get_more_search_result') }}", options).done(function( data ) { for(i=0; i "+ data.path_array[i] +"", data.date_array[i], data.size_array[i], "

" ] ).draw( false ); } + offset = offset + data.path_array.length; + page_offset = page_offset+1; $("#numberOfRes").text(parseInt($("#numberOfRes").text()) + data.path_array.length); - if (data.moreData == true) - load_search_data(init_num_of_elements_in_table, search_table, prev_query, offset+10); - else { - $("#loading_gif_search").hide(); + if (data.moreData == true) { + //continue + } else { + all_loaded = true; + $("#load_more_json_button1").hide(); } + $("#load_more_json_button1").removeAttr('disabled'); + return data.path_array.length; }); }