Skip to content

Commit

Permalink
[#3444] datatables resource view
Browse files Browse the repository at this point in the history
  • Loading branch information
wardi committed Feb 17, 2017
1 parent bd03c7b commit 8c50390
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 0 deletions.
Empty file.
44 changes: 44 additions & 0 deletions ckanext/datatablesview/controller.py
@@ -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']
],
})
47 changes: 47 additions & 0 deletions ckanext/datatablesview/plugin.py
@@ -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 ckanext/datatablesview/templates/datatables/datatables_view.html
@@ -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 %}
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -87,6 +87,7 @@
'recline_grid_view = ckanext.reclineview.plugin:ReclineGridView',
'recline_graph_view = ckanext.reclineview.plugin:ReclineGraphView',
'recline_map_view = ckanext.reclineview.plugin:ReclineMapView',
'datatables_view = ckanext.datatablesview.plugin:DataTablesView',
'image_view = ckanext.imageview.plugin:ImageView',
'webpage_view = ckanext.webpageview.plugin:WebPageView',
# FIXME: Remove deprecated resource previews below. You should use the
Expand Down

0 comments on commit 8c50390

Please sign in to comment.