datatablesQuery is a module for making the integration between front-end tables using datatables and a REST API, node.js, express, MongoDB and Mongoose backed Servers, easier.
The main purpose is dealing with server side processing available in datatables, making it easy to integrate client and server.
Install the module.
npm install datatables-query
In your front-end, configure your DataTable to use serverSide processing and Ajax. The request type MUST be 'POST'.
// jQuery way;
// Angular way - @see for full examplevmdtOptions = DTOptionsBuilder// all other options
In your server, you MUST use
var app = ;app
In the route handler, import the module and pass a reference to the mongoose model you wish to use as data source.
The DataTables params will get caught in the request body. It should be passed to the run method, which will return a promise.
That's all folks. Your table should be working just fine.
As noted above, it is assumed that the server parses the request using extended urlencoded and that the data object is a Mongoose object.
Datatables with serverSide processing enabled makes POST requests with content-type
and express' module body parser makes it easiear to work with this data, transforming it to JSON and parsing arrays and
Using Without Datatables
One could use this module without datatables in the front-end making requests. For this to work, the POST body must be a configuration object equivalent to the one shown below:
// req.body should be equivalent to:"draw": "3" // datatable stuff, but is mandatory nonetheless"start": "0""length": "10""search":"value": """columns":"data": "name" // field name in the MongoDB Schema"searchable": "true" // mandatory"orderable": "true" // mandatory// .. same structure as above for each field"order":"column": "1" // index of the column used for sorting"dir": "asc" // direction of sorting ('asc' | 'desc')
- Add examples to this repo
- Implement filter by column
- Add tests to the 'run' method
Feel free to fork and mess with this code. But, before opening PRs, be sure that you adhere to the Code Style and Conventions
grunt lint) and add/correct as many tests as needed to ensure your code is working as expected.
The MIT License (MIT)
Copyright (c) 2015 Vinicius Teixeira email@example.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.