0

I am trying to implement server side rendering for my jquery datatables with Nodejs and MSSQL database backend.

This is my client side code:

let tblAssignedJobs = $('#tblAssignedJobs').DataTable({
    "oLanguage": {
        "sEmptyTable": "There are no jobs assigned to you at this moment"
    },
    autoWidth  : false
   ,serverSide:true
    ,responsive : true
    ,deferRender    : true
    ,processing : true
    ,paging     : true
    ,pageLength : 25
    ,searching  : true
    ,info       : true
    ,ordering       : true
    ,dom            : "<ipf>"
    ,bPaginate  : false
    ,sDom       :"fptip",
    "aoColumns": [{
        "mData":"studentNumber"
      },{
        "mData": "studentType"
      }
      ,
      {
        "mData":"Description"
      }
      ,
      {
        "mData":"LocationNumber"
      }
      ,
      {
        "mData":"Address"
      },
      { "mData":"StudentNumber",
        "mRender": function(data, type, full) {

            return `<a href = '/editStudent?studentNumber=${data}'><button class='btn btn-primary'>Edit Job</button></a>`
        }
      }
    ],
    ajax: {
        url: "students",
        type: "POST"
    },

    responsive: true
});

On my Nodejs backend server:

let result = await rows.query(My Query)

myJSON = {
 "draw": 1,
 "recordsTotal": 25,
 "recordsFiltered": 25,
 "sEcho":25,
 "data": result.recordset
 }

My goal is to have 25 items per page. But this method is retruning the entire 3000 rows of data without pagination. Can anyone help me on how to setup pagination when using server side rendering in datatables?

5
  • It's the responsibility of the server to populate the "data" section of your response with the correct 25 records, based on the page number requested, any filtering requested, column sorting, etc (see here). Does that server-side logic exist? Commented May 7, 2020 at 20:36
  • To determine what is the correct 25 records I would need some search and sort parameters. I cannot find out a way to send parameters. i.e each time I search something or sort any of my columns I can see that a post request fires. So, I am guessing there is a way to capture what has happened on the server side Commented May 7, 2020 at 22:40
  • All those parameters are sent automatically by DataTables, whenever a user clicks on page navigation, or performs a filter, etc. Basically, anything which causes the data to be re-displayed. See the link in my previous comment - that is the data you need. The server needs to parse this request data from DataTAbles and assemble the data accordingly. So, yes, it's just a request that you have to process - like any other request received by the server. Commented May 7, 2020 at 22:45
  • Since those parameters are sent automatically, I thought I can obtain them on req.params but when I console log it, it is just an empty object {}. Commented May 7, 2020 at 22:52
  • I don't use node - but when I use DataTables server-side processing, and I use type: 'POST' (same as you) in my ajax call, I can see the request details in my Firefox browser: Network tab > the POST record > Params. If you cannot see that, then that may be a new question you can research. Commented May 7, 2020 at 23:34

1 Answer 1

-1

Please Check answer of pankaj kumar jQuery Datatable with NodeJS & MySql

you can import this short library code to use server-side functionality of datatable in node with mysql and sequelize

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.