Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #62387

Re: python - jquery datatables with mongodb(mongoengine)

Path csiph.com!usenet.pasdenom.info!news.albasani.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'output': 0.05; 'true,': 0.05; 'error:': 0.07; 'string': 0.09; '[],': 0.09; 'attributes': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rows': 0.09; 'skip:$ 20': 0.09; 'skip:$ 30': 0.09; 'skip:o 60': 0.09; 'spec': 0.09; 'undefined': 0.09; 'api': 0.11; 'def': 0.12; 'translation': 0.12; '""):': 0.16; '-1):': 0.16; '0):': 0.16; 'columns': 0.16; 'concatenated': 0.16; 'missing?': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'sorting': 0.16; 'typeerror:': 0.16; '});': 0.16; 'subject:python': 0.16; 'index': 0.16; 'wrote:': 0.18; 'entered': 0.20; 'import': 0.22; 'header:User-Agent:1': 0.23; 'filtering': 0.24; 'skip:i 40': 0.24; 'skip:{ 20': 0.24; 'sort': 0.25; 'skip:" 30': 0.26; 'code:': 0.26; 'values': 0.27; 'header:X-Complaints- To:1': 0.27; 'skip:( 40': 0.30; 'skip:@ 10': 0.30; 'specified': 0.30; 'code': 0.31; "skip:' 10": 0.31; '"")': 0.31; 'index,': 0.31; 'request,': 0.31; 'class': 0.32; 'skip:c 30': 0.32; 'candidate': 0.34; 'table': 0.34; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'could': 0.34; 'subject:with': 0.35; 'skip:s 30': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'var': 0.36; 'skip:o 20': 0.38; 'filter': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:l 50': 0.60; 'skip:a 30': 0.61; 'skip:a 50': 0.61; 'skip:o 30': 0.61; 'name': 0.63; 'term': 0.63; 'field': 0.63; 'skip:n 10': 0.64; 'total': 0.65; 'here': 0.66; 'between': 0.67; 'skip:r 40': 0.68; 'results': 0.69; 'skip:r 30': 0.69; 'skip:] 10': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: python - jquery datatables with mongodb(mongoengine)
Date Thu, 19 Dec 2013 09:18:42 +0100
Organization None
References <21351ab8-dde9-4e12-bc36-9036de39d9f0@googlegroups.com> <3b660b5f-002f-4304-bcf6-c6eb37e0568e@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084a447.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4405.1387441136.18130.python-list@python.org> (permalink)
Lines 184
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1387441136 news.xs4all.nl 2953 [2001:888:2000:d::a6]:44819
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:62387

Show key headers only | View raw


renier de bruyn wrote:

> UPDATE:
> 
> So now I got this code:
> 
> import pymongo
> from pyramid_mongo import get_db
> 
> # translation for sorting between datatables api and mongodb
> order_dict = {'asc': 1, 'desc': -1}
> 
> 
> class DataTables_Handler(object):
>     def __init__(self, request, columns, index, collection):
>         self.columns = columns
>         self.index = index
>         self.db = get_db(request)
>         self.collection = collection
> 
>         # values specified by the datatable for filtering, sorting, paging
>         self.request_values = request.params
> 
>         # results from the db
>         self.result_data = None
> 
>         # total in the table after filtering
>         self.cardinality_filtered = 0
> 
>         # total in the table unfiltered
>         self.cadinality = 0
> 
>         self.run_queries()
> 
>     def output_result(self):
>         output = {}
>         output['sEcho'] = str(int(self.request_values['sEcho']))
>         output['iTotalRecords'] = str(self.cardinality)
>         output['iTotalDisplayRecords'] = str(self.cardinality_filtered)
>         aaData_rows = []
> 
>         for row in self.result_data:
>             aaData_row = []
>             for i in range(len(self.columns)):
> 
>                 aaData_row.append(row[self.columns[i]].replace('"',
>                 '\\"'))
> 
>             # add additional rows here that are not represented in the
>             # database aaData_row.append(('''<input id='%s'
>             # type='checkbox'></input>''' % (str(row[ self.index
>             # ]))).replace('\\', ''))
> 
>             aaData_rows.append(aaData_row)
> 
>         output['aaData'] = aaData_rows
> 
>         return output
> 
>     def run_queries(self):
> 
>         # pages has 'start' and 'length' attributes
>         pages = self.paging()
> 
>         # the term you entered into the datatable search
>         filters = self.filtering()
> 
>         # the document field you chose to sort
>         sorting = self.sorting()
> 
>         # get result from db
>         self.result_data = self.db.self.collection.find(spec=filters,
>                                                       skip=pages.start,
>                                                       limit=pages.length,
>                                                       sort=sorting)
> 
>         total_count =
>         len(list(self.db.self.collection.find(spec=filters)))
> 
>         self.result_data = list(self.result_data)
> 
>         self.cardinality_filtered = total_count
> 
>         self.cardinality = len(list(self.db.self.collection.find()))
> 
>     def filtering(self):
> 
>         # build your filter spec
>         filters = {}
>         if (self.request_values.has_key('sSearch')) and
>         (self.request_values['sSearch'] != ""):
> 
>             # the term put into search is logically concatenated with 'or'
>             # between all columns
>         or_filter_on_all_columns = []
> 
>             for i in range(len(self.columns)):
>                 column_filter = {}
>                 column_filter[self.columns[i]] = {'$regex':
>                 self.request_values['sSearch'], '$options': 'i'}
>                 or_filter_on_all_columns.append(column_filter)
>             filters['$or'] = or_filter_on_all_columns
>         return filters
> 
>     def sorting(self):
>         order = []
>         # mongo translation for sorting order
> 
>         if (self.request_values['iSortCol_0'] != "") and
>         (self.request_values['iSortingCols'] > 0):
>             order = []
>             for i in range(int(self.request_values['iSortingCols'])):
>                 
order.append((self.columns[int(self.request_values['iSortCol_'
>                 + str(i)])], order_dict[self.request_values['sSortDir_' +
>                 str(i)]]))
>         return order
> 
>     def paging(self):
>         pages = namedtuple('pages', ['start', 'length'])
>         if (self.request_values['iDisplayStart'] != "") and
>         (self.request_values['iDisplayLength'] != -1):
>             pages.start = int(self.request_values['iDisplayStart'])
>             pages.length = int(self.request_values['iDisplayLength'])
>         return pages
> 
> with this code in the View:
> 
> @view_config(
> route_name='candidates.list.json',
> renderer='json',
> permission='admin'
> )
> def candidate_list_json(context, request):
>     columns = [ 'id_number', 'first_name', 'email', 'mobile_number']
>     index_column = "id_number"
>     collection = "candidates"
> 
>     results = DataTables_Handler(request, columns, index_column,
>     collection).output_result()
> 
>     # return the results as a string for the datatable
>     return {"results": results}
> 
> and this in the template:
> 
> <a id="btn-addteam" class="btn btn-success"
> href="{{'add_candidate'|route_url}}"><i class="icon-plus"></i> Add
> Candidate</a>
> 
> ID Number Candidate Name Candidate email Mobile Number Health -->
> 
> <script
> src="{{'kivu:static/datatables/jquery.dataTables.js'|
static_url}}"></script>
>     <script
>     src="{{'kivu:static/datatables/dataTables.scroller.js'|
static_url}}"></script>
> <script>
> $(document).ready(function() {
>     url = "{{'candidates.list.json'|route_url}}";
>     var oTable = $('#candidate_search').dataTable( {
>         "bProcessing": true,
>         "bServerSide": true,
>         "sPaginationType": "full_numbers",
>         "bjQueryUI": true,
>         "sAjaxSource": url
> });
> } );</script>
> 
> But its giving me a js error:
> 
> TypeError: aData is undefined
>     for ( var i=0, iLen=aData.length ; i<iLen ; i++ )
> 
> my GET response returns:
> 
> {"results": {"aaData": [], "iTotalRecords": "0", "sEcho": "1",
> {"iTotalDisplayRecords": "0"}}
> 
> there is data in the database. What am I missing?

Could it have to do with the mismatch of 'aData' versus 'aaData'?

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

python - jquery datatables with mongodb(mongoengine) techno206@gmail.com - 2013-12-18 04:20 -0800
  Re: python - jquery datatables with mongodb(mongoengine) techno206@gmail.com - 2013-12-18 04:22 -0800
  Re: python - jquery datatables with mongodb(mongoengine) renier de bruyn <techno206@gmail.com> - 2013-12-18 23:50 -0800
    Re: python - jquery datatables with mongodb(mongoengine) Peter Otten <__peter__@web.de> - 2013-12-19 09:18 +0100

csiph-web