How to render res.json(editor.data()) on ejs template

How to render res.json(editor.data()) on ejs template

ucinucin Posts: 13Questions: 4Answers: 0
edited June 2021 in DataTables

hi all,

I'm having a bit of a problem with rendering data populated from datatables nodejs library on ejs templated page.

If I run the same config over "generator" populated settings (which uses .html) everything works fine. When I try to send data directly to the same .html page using my express server (with current settings) all I get is the json data .

Either way, the populated data is the same:

{
  cancelled: [],
  data: [
{
      DT_RowId: 'row_13',
      accountcode: '905051258615',
      src: 'unavailable',
      dst: 'start',
      clid: '""Gelen Arama"" <unavailable>',
      uid: '1003',
      uid_name: 'holigan-ilayda',
      calldate: '2021-05-23 04:27:53',
      end: '2021-05-23 04:29:39',
      billsec: 106,
      disposition: 'ANSWERED'
}
],
  fieldErrors: [],
  draw: undefined,
  files: {},
  options: {},
  recordsTotal: undefined,
  recordsFiltered: undefined,
  searchPanes: undefined
}

and my ejs template is

<% for(var i=0; i< data.length; i++) { %>
                            
    <tr>
      <td>
      <%= data[i].accountcode%>
    </td>
    <td>
    <%= data[i].src%>
    </td>
    <td>
    <%= data[i].dst%>
    </td>
    <td>
    <%= data[i].clid%>
    </td>
...etc 

I keep getting invalid JSON response

this is how I'm sending the data

router.get('/cdr1', async function(req, res) {

    let editor = new Editor(db, 'cdr', 'id').fields(
        new Field("accountcode"),
        new Field("src"),
        new Field("dst"),
        new Field("clid"),
        new Field("uid"),
        new Field("uid_name"),
        //new Field("lastapp"),
        new Field("calldate")
                .validator(Validate.dateFormat("YYYY-MM-DD HH:mm:ss"))
                .getFormatter(Format.dateTime("YYYY-MM-DD HH:mm:ss", "YYYY-MM-DD HH:mm:ss"))
                .setFormatter(Format.dateTime("YYYY-MM-DD HH:mm:ss", "YYYY-MM-DD HH:mm:ss")),
        new Field("end")
                .validator(Validate.dateFormat("YYYY-MM-DD HH:mm:ss"))
                .getFormatter(Format.dateTime("YYYY-MM-DD HH:mm:ss", "YYYY-MM-DD HH:mm:ss"))
                .setFormatter(Format.dateTime("YYYY-MM-DD HH:mm:ss", "YYYY-MM-DD HH:mm:ss")),
        new Field("billsec"),
        new Field("disposition"),
        
    );
    editor.where( function () {
        this
        .where('calldate', '>=', '2021-05-23 00:00:00')
        .where('calldate', '<', '2021-05-23 04:30:00')
        .where('lastapp', '<>', "playback" )
        .where( 'lastapp','<>', "hangup" )
        })

    await editor.process(req.body);
    //res.json(editor.data());
    let result = editor.data()
    console.log(result)
    res.render('pbx/cdr1', {data: result });

thanks,
Ucin

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    edited June 2021

    Hi Ucin,

    You need two routes:

    1. The HTML (and Javascript) for the page - say /cdr1
    2. The Ajax data - /cdr1/data perhaps?

    It looks like you might be attempting to do both in the same controller above. DataTables won't make use of your ejs template I'm afraid - you could possibly populate a <table> using it and then have DataTables work on that, but I think you'd probably be better served just having a <table> with a header defined it in, and then let DataTables populate it.

    Allan

Sign In or Register to comment.