Added dynamic data loading in dataTable in search.html

This commit is contained in:
Mokaddem 2016-11-24 13:31:31 +01:00
parent 80ca49f018
commit df8d978088
3 changed files with 50 additions and 19 deletions

View file

@ -347,6 +347,7 @@ def search():
c = [] #preview of the paste content c = [] #preview of the paste content
paste_date = [] paste_date = []
paste_size = [] paste_size = []
num_elem_to_get = 50
# Search filename # Search filename
for path in r_serv_pasteName.smembers(q[0]): for path in r_serv_pasteName.smembers(q[0]):
@ -370,7 +371,7 @@ def search():
from whoosh.qparser import QueryParser from whoosh.qparser import QueryParser
with ix.searcher() as searcher: with ix.searcher() as searcher:
query = QueryParser("content", ix.schema).parse(" ".join(q)) 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: for x in results:
r.append(x.items()[0][1]) r.append(x.items()[0][1])
paste = Paste.Paste(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:] curr_date = curr_date[0:4]+'/'+curr_date[4:6]+'/'+curr_date[6:]
paste_date.append(curr_date) paste_date.append(curr_date)
paste_size.append(paste._get_p_size()) 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']) @app.route("/get_more_search_result", methods=['POST'])
@ -389,7 +393,8 @@ def get_more_search_result():
query = request.form['query'] query = request.form['query']
q = [] q = []
q.append(query) q.append(query)
offset = int(request.form['offset']) page_offset = int(request.form['page_offset'])
num_elem_to_get = 50
path_array = [] path_array = []
preview_array = [] preview_array = []
@ -405,7 +410,7 @@ def get_more_search_result():
from whoosh.qparser import QueryParser from whoosh.qparser import QueryParser
with ix.searcher() as searcher: with ix.searcher() as searcher:
query = QueryParser("content", ix.schema).parse(" ".join(q)) 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: for x in results:
path_array.append(x.items()[0][1]) path_array.append(x.items()[0][1])
paste = Paste.Paste(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["preview_array"] = preview_array
to_return["date_array"] = date_array to_return["date_array"] = date_array
to_return["size_array"] = size_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 to_return["moreData"] = False
else: else:
to_return["moreData"] = True to_return["moreData"] = True

View file

@ -28,7 +28,7 @@
</div> </div>
<div id="div_stil_data"> <div id="div_stil_data">
<button id="load_more_json_button1" type="button" class="btn btn-default" onclick="add_entries(100)" style="display: none">Load 100 entries</button> <button id="load_more_json_button1" type="button" class="btn btn-default" onclick="add_entries(100)" style="display: none">Load 100 entries</button>
<button id="load_more_json_button2" type="button" class="btn btn-warning" onclick="add_entries(500)" style="display: none">Load 500 entries</button> <button id="load_more_json_button2" type="button" class="btn btn-warning" onclick="add_entries(300)" style="display: none">Load 300 entries</button>
<img id="loading_gif_browse" src="{{url_for('static', filename='image/loading.gif') }}" heigt="20" width="20" style="margin: 2px; {{ finished }}"></div> <img id="loading_gif_browse" src="{{url_for('static', filename='image/loading.gif') }}" heigt="20" width="20" style="margin: 2px; {{ finished }}"></div>
</br> </br>

View file

@ -83,7 +83,6 @@
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading"> <div class="panel-heading">
<i class="glyphicon glyphicon-search"></i> <b id="numberOfRes">{{ r|length }}</b> Results for "<strong>{{ query }}</strong> <i class="glyphicon glyphicon-search"></i> <b id="numberOfRes">{{ r|length }}</b> Results for "<strong>{{ query }}</strong>
<img id="loading_gif_search" src="{{url_for('static', filename='image/loading.gif') }}" height="20" width="20" style="margin: 2px;">
<div class="pull-right"> <div class="pull-right">
</div> </div>
@ -114,7 +113,11 @@
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
<div id="div_stil_data">
<button id="load_more_json_button1" type="button" class="btn btn-default" onclick="add_entries();" style="display: none">Load 50 entries</button>
<strong> Totalling {{ num_res }} items </strong>
</div> </div>
</div>
<!-- /.panel-body --> <!-- /.panel-body -->
</div> </div>
</div> </div>
@ -123,6 +126,7 @@
<!-- /#page-wrapper --> <!-- /#page-wrapper -->
</div> </div>
<script src="{{ url_for('static', filename='js/bootstrap.min.js') }}"></script> <script src="{{ url_for('static', filename='js/bootstrap.min.js') }}"></script>
</br>
</body> </body>
<!-- enable tooltip and dataTable --> <!-- enable tooltip and dataTable -->
@ -130,17 +134,29 @@
var search_table; var search_table;
var last_clicked_paste; var last_clicked_paste;
var can_change_modal_content = true; 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(){ $(document).ready(function(){
$('[data-toggle="tooltip"]').tooltip(); $('[data-toggle="tooltip"]').tooltip();
$("#button_show_path").hide(); $("#button_show_path").hide();
var search_table = $('#myTable').DataTable(); search_table = $('#myTable').DataTable();
var prev_query = "{{ query }}"; query = "{{ query }}";
var offset = 2; offset = 0;
var init_num_of_elements_in_table = parseInt("{{ r|length }}"); // Comes from the file search page_offset = 2; //page 1 already loaded
load_search_data(init_num_of_elements_in_table, search_table, prev_query, offset); 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();
}
}); });
</script> </script>
@ -149,27 +165,36 @@
// Loop to recover all the data from get_more_search_results // Loop to recover all the data from get_more_search_results
// And add it dynamically top the dataTable // 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) { function load_search_50_data() {
var options = { query: prev_query, offset: offset }; var options = { query: query, page_offset: page_offset };
$.post( "{{ url_for('get_more_search_result') }}", options).done(function( data ) { $.post( "{{ url_for('get_more_search_result') }}", options).done(function( data ) {
for(i=0; i<data.path_array.length; i++) { for(i=0; i<data.path_array.length; i++) {
var curr_preview = data.preview_array[i].replace(/\"/g, "\'"); var curr_preview = data.preview_array[i].replace(/\"/g, "\'");
search_table.row.add( [ search_table.row.add( [
init_num_of_elements_in_table+((offset-2))+i+1, init_num_of_elements_in_table+((offset))+i+1,
"<a target=\"_blank\" href=\"{{ url_for('showsavedpaste') }}?paste="+data.path_array[i]+"&num="+i+"\"> "+ data.path_array[i] +"</a>", "<a target=\"_blank\" href=\"{{ url_for('showsavedpaste') }}?paste="+data.path_array[i]+"&num="+i+"\"> "+ data.path_array[i] +"</a>",
data.date_array[i], data.date_array[i],
data.size_array[i], data.size_array[i],
"<p><span class=\"glyphicon glyphicon-info-sign\" data-toggle=\"tooltip\" data-placement=\"left\" title=\""+curr_preview+"\"></span> <button type=\"button\" class=\"btn-link\" data-num=\""+i+"\" data-toggle=\"modal\" data-target=\"#mymodal\" data-url=\"{{ url_for('showsavedpaste') }}?paste="+data.path_array[i]+"&num="+i+"\" data-path=\""+data.path_array[i]+"\"><span class=\"fa fa-search-plus\"></span></button></p>" "<p><span class=\"glyphicon glyphicon-info-sign\" data-toggle=\"tooltip\" data-placement=\"left\" title=\""+curr_preview+"\"></span> <button type=\"button\" class=\"btn-link\" data-num=\""+i+"\" data-toggle=\"modal\" data-target=\"#mymodal\" data-url=\"{{ url_for('showsavedpaste') }}?paste="+data.path_array[i]+"&num="+i+"\" data-path=\""+data.path_array[i]+"\"><span class=\"fa fa-search-plus\"></span></button></p>"
] ).draw( false ); ] ).draw( false );
} }
offset = offset + data.path_array.length;
page_offset = page_offset+1;
$("#numberOfRes").text(parseInt($("#numberOfRes").text()) + data.path_array.length); $("#numberOfRes").text(parseInt($("#numberOfRes").text()) + data.path_array.length);
if (data.moreData == true) if (data.moreData == true) {
load_search_data(init_num_of_elements_in_table, search_table, prev_query, offset+10); //continue
else { } else {
$("#loading_gif_search").hide(); all_loaded = true;
$("#load_more_json_button1").hide();
} }
$("#load_more_json_button1").removeAttr('disabled');
return data.path_array.length;
}); });
} }