SlideShare ist ein Scribd-Unternehmen logo
1 von 9
Dynamic Data Display With jQuery,
   DataTables and MongoDB
  June 2012 NY MongoDB User Group
            June 19, 2012
              Jim O’Hara
http://datatables.net
<table cellpadding="0" cellspacing="0" border="0"
Setting up the html               class="display" id="example">
                                  <thead>
Add the html elements
                                   <tr>
                                  <th width="10">Last Name</th>
Add headers in a <thead>
                                  <th width="10">First Name</th>
Include an id tag for the table   <th width="10">Age</th>
                                  <th width="25">Location</th>
                                  <th width="10">Date</th>
                                  <th width="5">Fatal</th>
                                  </tr>
                                  </thead>
                                  <tbody>
                                  </tbody>
                                  </table>
$(document).ready(function() {
                                                  var oTable = $('#example').dataTable( {
Setting up the html                               "bProcessing": true,
This script is added in a script tag              "bPaginate": true,
following references to jQuery and                "bProcessing": true,
Datables.                                         "bDeferRender":true,
                                                  "iDisplayLength":250,
These are the table cell elements                 "sAjaxSource": "MongoServlet",
                                                  "sScrollY": "400px",
Script will run on document load.                 "aoColumns": [
                                                              { "mDataProp": "lname"},
Datatables will read each value with                          { "mDataProp": "fname"},
the mDataProp, and populate the                               { "mDataProp": "age", "sClass": "center"},
JSON in a table cell.                                         { "mDataProp": "location"},
                                                              { "mDataProp": "date"},
MongoServlet is the name of the Java                          { "mDataProp": "fatal", "sClass": "center"}
class that will run.                                          ]

Servlet will reference a collection in              }
MongDB and return a JSON object.         );
db.miners.findOne()
{
"_id" : "ObjectID(“4fde364a……….."),
             "lname" : "ABAHAZY",
             "fname" : "JOHN",
             "location" :
"WOODWARD",
             “age" : "37",
             "fatal" : "Y",
             "date" : "1905/09/30"
}
m = new Mongo("localhost");
MongoServlet
(snippets)     protected void doPost(()

               PrintWriter out = response.getWriter();

               DB db = m.getDB("test");

               DBCollection coll = db.getCollection("miners");

               DBCursor cursor = coll.find();

               JSONObject miners = new JSONObject();

               Map map = new LinkedHashMap();

               List items = new ArrayList();
while(cursor.hasNext()) {
MongoServlet    Miner miner = new Miner(); // a simple POJO
(snippets)      DBObject dbObject = cursor.next();
                String lname = String.valueOf(dbObject.get("lname"));
                String fname = String.valueOf(dbObject.get("fname"));
                String age = String.valueOf(dbObject.get("age"));
                String date = String.valueOf(dbObject.get("date"));
                String fatal = String.valueOf(dbObject.get("fatal"));
                String location = String.valueOf(dbObject.get("location")).toUpperCase();

                   miner.setLname(lname);
                   miner.setFname(fname);
                   miner.setAge(age);
                   miner.setFatal(fatal);
                   miner.setLocation(location);
                   miner.setDate(date);

                   items.add(miner);
               }
MongoServlet   map.put("sEcho", "1");
(snippets)     map.put("iTotalRecords", items.size());
               map.put("iTotalDisplayRecords", items.size());
               map.put("aaData", items);

               ObjectMapper mapper = new ObjectMapper();
               String jsonString = mapper.writeValueAsString(map);
               System.out.println(jsonString);

               out.print(jsonString); // JSON will be returned to the calling page
http://localhost:8080/tables/datatables.jsp

Weitere ähnliche Inhalte

Was ist angesagt?

San Francisco Java User Group
San Francisco Java User GroupSan Francisco Java User Group
San Francisco Java User Group
kchodorow
 
Aggregation in MongoDB
Aggregation in MongoDBAggregation in MongoDB
Aggregation in MongoDB
Kishor Parkhe
 
JavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovyJavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovy
Yasuharu Nakano
 
Let's Build A Gutenberg Block | WordCamp Europe 2018
Let's Build A Gutenberg Block | WordCamp Europe 2018Let's Build A Gutenberg Block | WordCamp Europe 2018
Let's Build A Gutenberg Block | WordCamp Europe 2018
Lara Schenck
 
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
MongoDB
 
MongoDB Aggregation Framework
MongoDB Aggregation FrameworkMongoDB Aggregation Framework
MongoDB Aggregation Framework
Tyler Brock
 
Is2215 lecture8 relational_databases
Is2215 lecture8 relational_databasesIs2215 lecture8 relational_databases
Is2215 lecture8 relational_databases
dannygriff1
 

Was ist angesagt? (19)

Mongoskin - Guilin
Mongoskin - GuilinMongoskin - Guilin
Mongoskin - Guilin
 
MySQL flexible schema and JSON for Internet of Things
MySQL flexible schema and JSON for Internet of ThingsMySQL flexible schema and JSON for Internet of Things
MySQL flexible schema and JSON for Internet of Things
 
MongoDB - Aggregation Pipeline
MongoDB - Aggregation PipelineMongoDB - Aggregation Pipeline
MongoDB - Aggregation Pipeline
 
MongoDB Aggregation
MongoDB Aggregation MongoDB Aggregation
MongoDB Aggregation
 
How to calculate the optimal undo retention in Oracle
How to calculate the optimal undo retention in OracleHow to calculate the optimal undo retention in Oracle
How to calculate the optimal undo retention in Oracle
 
San Francisco Java User Group
San Francisco Java User GroupSan Francisco Java User Group
San Francisco Java User Group
 
Aggregation in MongoDB
Aggregation in MongoDBAggregation in MongoDB
Aggregation in MongoDB
 
JavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovyJavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovy
 
Python queue solution with asyncio and kafka
Python queue solution with asyncio and kafkaPython queue solution with asyncio and kafka
Python queue solution with asyncio and kafka
 
Let's Build A Gutenberg Block | WordCamp Europe 2018
Let's Build A Gutenberg Block | WordCamp Europe 2018Let's Build A Gutenberg Block | WordCamp Europe 2018
Let's Build A Gutenberg Block | WordCamp Europe 2018
 
Mongo db modifiers
Mongo db modifiersMongo db modifiers
Mongo db modifiers
 
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDB
 
Splitapp coding
Splitapp codingSplitapp coding
Splitapp coding
 
Nodejs mongoose
Nodejs mongooseNodejs mongoose
Nodejs mongoose
 
Mobile Web 5.0
Mobile Web 5.0Mobile Web 5.0
Mobile Web 5.0
 
MongoDB Aggregation Framework
MongoDB Aggregation FrameworkMongoDB Aggregation Framework
MongoDB Aggregation Framework
 
Is2215 lecture8 relational_databases
Is2215 lecture8 relational_databasesIs2215 lecture8 relational_databases
Is2215 lecture8 relational_databases
 
Talk MongoDB - Amil
Talk MongoDB - AmilTalk MongoDB - Amil
Talk MongoDB - Amil
 

Ähnlich wie jQuery Datatables With MongDb

WebSocket JSON Hackday
WebSocket JSON HackdayWebSocket JSON Hackday
WebSocket JSON Hackday
Somay Nakhal
 
CouchDB on Android
CouchDB on AndroidCouchDB on Android
CouchDB on Android
Sven Haiges
 

Ähnlich wie jQuery Datatables With MongDb (20)

Data access 2.0? Please welcome: Spring Data!
Data access 2.0? Please welcome: Spring Data!Data access 2.0? Please welcome: Spring Data!
Data access 2.0? Please welcome: Spring Data!
 
An introduction into Spring Data
An introduction into Spring DataAn introduction into Spring Data
An introduction into Spring Data
 
JSON & AJAX.pptx
JSON & AJAX.pptxJSON & AJAX.pptx
JSON & AJAX.pptx
 
Embedding a language into string interpolator
Embedding a language into string interpolatorEmbedding a language into string interpolator
Embedding a language into string interpolator
 
Drupal Mobile
Drupal MobileDrupal Mobile
Drupal Mobile
 
NoSQL meets Microservices - Michael Hackstein
NoSQL meets Microservices - Michael HacksteinNoSQL meets Microservices - Michael Hackstein
NoSQL meets Microservices - Michael Hackstein
 
Json
JsonJson
Json
 
NoSQL meets Microservices
NoSQL meets MicroservicesNoSQL meets Microservices
NoSQL meets Microservices
 
Michael Hackstein - NoSQL meets Microservices - NoSQL matters Dublin 2015
Michael Hackstein - NoSQL meets Microservices - NoSQL matters Dublin 2015Michael Hackstein - NoSQL meets Microservices - NoSQL matters Dublin 2015
Michael Hackstein - NoSQL meets Microservices - NoSQL matters Dublin 2015
 
Spray Json and MongoDB Queries: Insights and Simple Tricks.
Spray Json and MongoDB Queries: Insights and Simple Tricks.Spray Json and MongoDB Queries: Insights and Simple Tricks.
Spray Json and MongoDB Queries: Insights and Simple Tricks.
 
WebSocket JSON Hackday
WebSocket JSON HackdayWebSocket JSON Hackday
WebSocket JSON Hackday
 
CouchDB on Android
CouchDB on AndroidCouchDB on Android
CouchDB on Android
 
NoSQL and JavaScript: a Love Story
NoSQL and JavaScript: a Love StoryNoSQL and JavaScript: a Love Story
NoSQL and JavaScript: a Love Story
 
WOTC_Import
WOTC_ImportWOTC_Import
WOTC_Import
 
MongoDB and RDBMS
MongoDB and RDBMSMongoDB and RDBMS
MongoDB and RDBMS
 
Using Arbor/ RGraph JS libaries for Data Visualisation
Using Arbor/ RGraph JS libaries for Data VisualisationUsing Arbor/ RGraph JS libaries for Data Visualisation
Using Arbor/ RGraph JS libaries for Data Visualisation
 
d3sparql.js demo at SWAT4LS 2014 in Berlin
d3sparql.js demo at SWAT4LS 2014 in Berlind3sparql.js demo at SWAT4LS 2014 in Berlin
d3sparql.js demo at SWAT4LS 2014 in Berlin
 
Http4s, Doobie and Circe: The Functional Web Stack
Http4s, Doobie and Circe: The Functional Web StackHttp4s, Doobie and Circe: The Functional Web Stack
Http4s, Doobie and Circe: The Functional Web Stack
 
Validating JSON -- Percona Live 2021 presentation
Validating JSON -- Percona Live 2021 presentationValidating JSON -- Percona Live 2021 presentation
Validating JSON -- Percona Live 2021 presentation
 
Hands On Spring Data
Hands On Spring DataHands On Spring Data
Hands On Spring Data
 

Kürzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Kürzlich hochgeladen (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 

jQuery Datatables With MongDb

  • 1. Dynamic Data Display With jQuery, DataTables and MongoDB June 2012 NY MongoDB User Group June 19, 2012 Jim O’Hara
  • 3. <table cellpadding="0" cellspacing="0" border="0" Setting up the html class="display" id="example"> <thead> Add the html elements <tr> <th width="10">Last Name</th> Add headers in a <thead> <th width="10">First Name</th> Include an id tag for the table <th width="10">Age</th> <th width="25">Location</th> <th width="10">Date</th> <th width="5">Fatal</th> </tr> </thead> <tbody> </tbody> </table>
  • 4. $(document).ready(function() { var oTable = $('#example').dataTable( { Setting up the html "bProcessing": true, This script is added in a script tag "bPaginate": true, following references to jQuery and "bProcessing": true, Datables. "bDeferRender":true, "iDisplayLength":250, These are the table cell elements "sAjaxSource": "MongoServlet", "sScrollY": "400px", Script will run on document load. "aoColumns": [ { "mDataProp": "lname"}, Datatables will read each value with { "mDataProp": "fname"}, the mDataProp, and populate the { "mDataProp": "age", "sClass": "center"}, JSON in a table cell. { "mDataProp": "location"}, { "mDataProp": "date"}, MongoServlet is the name of the Java { "mDataProp": "fatal", "sClass": "center"} class that will run. ] Servlet will reference a collection in } MongDB and return a JSON object. );
  • 5. db.miners.findOne() { "_id" : "ObjectID(“4fde364a……….."), "lname" : "ABAHAZY", "fname" : "JOHN", "location" : "WOODWARD", “age" : "37", "fatal" : "Y", "date" : "1905/09/30" }
  • 6. m = new Mongo("localhost"); MongoServlet (snippets) protected void doPost(() PrintWriter out = response.getWriter(); DB db = m.getDB("test"); DBCollection coll = db.getCollection("miners"); DBCursor cursor = coll.find(); JSONObject miners = new JSONObject(); Map map = new LinkedHashMap(); List items = new ArrayList();
  • 7. while(cursor.hasNext()) { MongoServlet Miner miner = new Miner(); // a simple POJO (snippets) DBObject dbObject = cursor.next(); String lname = String.valueOf(dbObject.get("lname")); String fname = String.valueOf(dbObject.get("fname")); String age = String.valueOf(dbObject.get("age")); String date = String.valueOf(dbObject.get("date")); String fatal = String.valueOf(dbObject.get("fatal")); String location = String.valueOf(dbObject.get("location")).toUpperCase(); miner.setLname(lname); miner.setFname(fname); miner.setAge(age); miner.setFatal(fatal); miner.setLocation(location); miner.setDate(date); items.add(miner); }
  • 8. MongoServlet map.put("sEcho", "1"); (snippets) map.put("iTotalRecords", items.size()); map.put("iTotalDisplayRecords", items.size()); map.put("aaData", items); ObjectMapper mapper = new ObjectMapper(); String jsonString = mapper.writeValueAsString(map); System.out.println(jsonString); out.print(jsonString); // JSON will be returned to the calling page