Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# encoding: utf-8 | ||
|
||
import json | ||
|
||
from ckan.plugins.toolkit import BaseController, request, get_action | ||
|
||
|
||
class DataTablesController(BaseController): | ||
def ajax(self, resource_id): | ||
draw = int(request.params['draw']) | ||
search_text = unicode(request.params['search[value]']) | ||
offset = int(request.params['start']) | ||
limit = int(request.params['length']) | ||
sort_by_num = int(request.params['order[0][column]']) | ||
sort_order = ( | ||
u'desc' if request.params['order[0][dir]'] == u'desc' | ||
else u'asc') | ||
|
||
datastore_search = get_action(u'datastore_search') | ||
unfiltered_response = datastore_search(None, { | ||
u"resource_id": resource_id, | ||
u"limit": 1, | ||
}) | ||
|
||
cols = [f['id'] for f in unfiltered_response['fields']] | ||
sort_str = cols[sort_by_num] + u' ' + sort_order | ||
|
||
response = datastore_search(None, { | ||
u"q": search_text, | ||
u"resource_id": resource_id, | ||
u"offset": offset, | ||
u"limit": limit, | ||
u"sort": sort_str, | ||
}) | ||
|
||
return json.dumps({ | ||
u'draw': draw, | ||
u'iTotalRecords': unfiltered_response.get(u'total', 0), | ||
u'iTotalDisplayRecords': response.get(u'total', 0), | ||
u'aaData': [ | ||
[unicode(row.get(colname, u'')) for colname in cols] | ||
for row in response['records'] | ||
], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# encoding: utf-8 | ||
|
||
from logging import getLogger | ||
|
||
from ckan.common import json | ||
import ckan.plugins as p | ||
import ckan.plugins.toolkit as toolkit | ||
|
||
|
||
class DataTablesView(p.SingletonPlugin): | ||
''' | ||
DataTables table view plugin | ||
''' | ||
p.implements(p.IConfigurer, inherit=True) | ||
p.implements(p.IResourceView, inherit=True) | ||
p.implements(p.IRoutes, inherit=True) | ||
|
||
def update_config(self, config): | ||
''' | ||
Set up the resource library, public directory and | ||
template directory for the view | ||
''' | ||
toolkit.add_template_directory(config, u'templates') | ||
|
||
def can_view(self, data_dict): | ||
resource = data_dict['resource'] | ||
return resource.get(u'datastore_active') | ||
|
||
def view_template(self, context, data_dict): | ||
return u'datatables/datatables_view.html' | ||
|
||
def info(self): | ||
return {u'name': u'datatables_view', | ||
u'title': u'Table', | ||
u'filterable': True, | ||
u'icon': u'table', | ||
u'requires_datastore': True, | ||
u'default_title': p.toolkit._(u'Table'), | ||
} | ||
|
||
def before_map(self, m): | ||
m.connect( | ||
u'/datatables/ajax/{resource_id}', | ||
controller=u'ckanext.datatablesview.controller' | ||
u':DataTablesController', | ||
action=u'ajax') | ||
return m |
43 changes: 43 additions & 0 deletions
43
ckanext/datatablesview/templates/datatables/datatables_view.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block page %} | ||
<table id="dtprv" | ||
data-role="table" | ||
data-mode="columntoggle" > | ||
<thead> | ||
<tr> | ||
{% for field in h.datastore_dictionary(resource.id) %} | ||
<th scope="col">{{ field.id }}</th> | ||
{% endfor %} | ||
</tr> | ||
</thead> | ||
<tbody> | ||
</tbody> | ||
</table> | ||
{% endblock %} | ||
|
||
{% block scripts %} | ||
<script | ||
src="https://code.jquery.com/jquery-3.1.1.min.js" | ||
crossorigin="anonymous"></script> | ||
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css" type="text/css" /> | ||
<script type="text/javascript" src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script> | ||
<script type="text/javascript"> | ||
$(document).ready(function() { | ||
$('#dtprv').DataTable( | ||
{ | ||
"paging": true, | ||
"serverSide": true, | ||
"processing": true, | ||
"ajax": { | ||
"url": "/datatables/ajax/{{ resource.id }}", | ||
"type": "POST" | ||
} | ||
} | ||
); | ||
}); | ||
</script> | ||
{% endblock %} | ||
|
||
{% block styles %}{% endblock %} | ||
{% block custom_styles %}{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters