Pass columns.render Via Ajax

Pass columns.render Via Ajax

lunarcrmlunarcrm Posts: 2Questions: 1Answers: 0
edited April 2018 in Free community support

I'm generating my datatables on the fly, writing the columns and data using a single function. I'm wanting to pass in the columns.render values directly from my ajax data but I'm getting parser errors when I try to do this. Here is my code...

    var data, 
    jqxhr = $.ajax($url).done(function () {
        data = JSON.parse(jqxhr.responseText);
        $('#json').text(JSON.stringify(data));

        $(tableName).dataTable({
            "data": data.data,
            "columns": data.columns[0]
        });
    })

And this is the data I'm passing in...

{
  "data": [
    [
      "Column",
      "250.00"
    ]
  ],
  "columns": [
    [
      {
        "name": null,
        "className": "sml",
        "total": null
      },
      {
        "name": "Leads",
        "className": "smlr",
        "orderable": true,
        "render": function ( data, type, row, meta ) { return 'Link'; }
      }
    ]
  ]
}

I've also tried enclosing the render function in double quotes but this returns as requested unknown parameter error.

Thanks

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,300Questions: 26Answers: 4,945
    Answer ✓

    I think you will find this thread interesting:
    https://datatables.net/forums/discussion/comment/128096/#Comment_128096

    Kevin

  • lunarcrmlunarcrm Posts: 2Questions: 1Answers: 0

    The tread was useful thanks. I adapted it and used the following is anyone is interested.

    {
    "data": [
    [
    "Column",
    "250.00"
    ]
    ],
    "columns": [
    [
    {
    "name": null,
    "className": "sml",
    "total": null
    },
    {
    "name": "Leads",
    "className": "smlr",
    "orderable": true,
    "function": "myfunction(data, type, row, meta)"
    }
    ]
    ]
    }

    var columns = data.columns[0];
    $(data.columns[0]).each(function(i, item) {
        columns[i].render = function ( data, type, row, meta ) {
            return eval(item.function)
        }
    });
    
    data.columns[0] = columns;
    
    $(tableName).dataTable({
        "data": data.data,
        "columns": data.columns[0]
    });
    

    function myFunction(data, type, row, meta) {
    return data + " " + type + " " +row + " " + meta;
    }

This discussion has been closed.