passing parameters to column render function

passing parameters to column render function

jasonblewisjasonblewis Posts: 3Questions: 2Answers: 0
edited January 2017 in Free community support

I want to make a function I can pass to DataTables for rendering columns, mainly so I can make a column a clickable link.

How can I pass not only the function name of the renderer but also a parameter, such as the target url?

I made an example showing how not to do it ;)

http://jsfiddle.net/jasonblewis/u9en604m/

See lines 26-27

tia

Answers

  • jasonblewisjasonblewis Posts: 3Questions: 2Answers: 0

    So to answer my own question. I found this example:

    https://datatables.net/forums/discussion/31944/how-to-pass-additional-meta-data-eg-template-to-the-column-render-function

    which helped me sort out a solution, with a friends help.

    function render_urlfn(data,type,row,meta){
      var parameters = meta.settings.oInit.columnDefs[meta.col].parameters;
      var target_url = parameters.url;
      return '<a href="'+target_url+data+'">'+data+'</a>';
    };
    $(document).ready(function () {
      var url = 'http://www.json-generator.com/api/json/get/cbEfqLwFaq?indent=2';
      
      
      var table = $('#example').DataTable({
        'processing': true,
        'serverSide': true,
        'ajax': {
          type: 'POST',
          'url': url,
          'data': function (d) {
            //console.log(d.order);
            return JSON.stringify( d );
          }
        },
        "createdRow" : function(row,data,dataIndex) {
                if (data[0] == "Airi") {
            $(row).addClass('info');
          }
        },
        "columnDefs": [ {
            "targets" : 0,
           "render": render_urlfn,
           "parameters": {
                "url": "http://some_new.url/"
              //, "anotherparameter": true
              //, "andanotherparamter": true
           }
        }
       ]
    
     });
    

    http://jsfiddle.net/jasonblewis/u9en604m/4/

  • kumarsumitkumarsumit Posts: 3Questions: 1Answers: 0
    edited August 2017

    render : function (data, type, row) {
    return '<a href="https://site_url/'+data[0]+'">'+data[0]+'</a>'
    }

This discussion has been closed.