How do I add paging and search to my express web app

How do I add paging and search to my express web app

n403729w735708n403729w735708 Posts: 26Questions: 6Answers: 4

I'm struggling to figure out how to use this plugin with my Express web app. The server side examples here use php and I'm using javascript. I need to load data from my database (postgresql), and display it to the user (ejs template). They will need the ability to filter results (I planned on using a form for this) and I need the ability to customize some of the columns: some will have buttons along with returned data etc... Any help is much appreciated. Thank you!

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,696Questions: 1Answers: 10,500 Site admin

    Per the forum rules, please link to a page showing the issues that you are having so we can help to debug them.

    Allan

  • n403729w735708n403729w735708 Posts: 26Questions: 6Answers: 4

    Hi,

    Sorry for not following the rules here. Let me explain:
    Error: Uncaught TypeError: Cannot read property 'length' of undefined

    What I'm trying to do: use datatables plugin for pagination and search functionality. I'll have to add some buttons to certain columns/cells but that will come after.

    I'm currently using express 4 (ejs) / node js. Most of the examples I see use php so it's a bit difficult since I'm new with express / ajax.

    Code: http://pastebin.com/10KdDu1a

    Thank you

  • allanallan Posts: 63,696Questions: 1Answers: 10,500 Site admin

    You are using server-side processing in that code. Have you implemented server-side processing in the server-side script? Without a test case that I can load run and check, I can't say.

    Allan

  • n403729w735708n403729w735708 Posts: 26Questions: 6Answers: 4

    I was actually able to get it going yesterday. I'll put up my example when I'm done for others to reference later.

  • n403729w735708n403729w735708 Posts: 26Questions: 6Answers: 4

    Sample server side script for everyone (filename.js):

    var express = require('express');
    var router = express.Router();
    var pgp = require('pg-promise')(/*options*/);
    var conString = "postgres://dbname:password@localhost:5432/db";
    var db = pgp(conString);
    
    /* GET users listing. */
    router.get('/', function(req, res, next) {
        console.log(req.query);
        var sco; // shared connection object;
        var recordsTotal = 0;
        db.connect()
            .then(function (obj) {
                sco = obj; // save the connection object;
                return sco.query("select count(*) from provider"
                );
            })
            .then(function (obj) {
                //console.log('obj ' + obj[0].count);
                recordsTotal =  obj[0].count;
                return sco.query("select id, last_name as lastname, first_name as firstname from provider order by timestamp desc limit $1 offset $2 ",
                    [req.query.length, req.query.start]);
            })
            .then(function (providers) {
                if (providers !== undefined) {
                    res.send(JSON.stringify({
                        "draw": parseInt(req.query.draw),
                        "recordsFiltered": recordsTotal,
                        "recordsTotal": recordsTotal,
                        "data": providers
                    }));
                }
            }
            , function (reason) {
                console.log(reason); // display reason why the call failed;
            })
            .done(function () {
                if (sco) {
                    sco.done();
                }
            });
    });
    module.exports = router;
    
  • allanallan Posts: 63,696Questions: 1Answers: 10,500 Site admin
    Answer ✓

    Great - thanks for posting that!

    Regards,
    Allan

This discussion has been closed.