columnDefs.targets by class name not working when columns defined in JS

columnDefs.targets by class name not working when columns defined in JS

GIDustinGIDustin Posts: 1Questions: 1Answers: 0

In a project I am working on, the HTML is simply an empty table. The data and columns are sent from an external ajax call. The columns are all assigned classes using className. When I try to add custom rendering to those columns using columnDefs, I am unable to use the classname of the column as the "targets". I have to specify the columns by index number. Is that working as intended?

I created a demo of the issue at http://live.datatables.net/sisigaca/1/edit The first table has columns defined by JS, and the second table has columns defined in HTML, and you can see that the Age column is formatted "properly" in the second table but not the first. I tried to make the example as close to my production environment as I could using sample data.

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,990Questions: 87Answers: 421
    Answer ✓

    Interesting. I couldn't make it work either. But here is a solution replacing "className" in "columns" with "render". That works!

    $(document).ready( function () {
      var dataReturnedByCustomAjax=
        "data":[{...}]",
        "columns":[
          {"data":"Name","title":"Emp Name","className":"column_Name"},
          {"data":"Age", "title":"Age", "render":function(data,type,row,meta){return Number(data).toFixed(2);}},
        ],
      };
      // This table has no columns in HTML.  They are created by Datatables
      var table = $('#example1').DataTable({
        "deferRender": true,
        "data":dataReturnedByCustomAjax.data,
        "columns":dataReturnedByCustomAjax.columns,
        "columnDefs":[
          // This works, referencing column by number:
           //{"targets":1,"render":function(data,type,row,meta){return Number(data).toFixed(2);}},
        ]
      });
    
      // This table has columns in HTML.  They are not created by Datatables
      var table2 = $('#example2').DataTable({
        "data":dataReturnedByCustomAjax.data,
        "columns":dataReturnedByCustomAjax.columns,
        "columnDefs":[
          // When the columns are defined in HTML, this works with no problem
          {"targets":"column_Age","render":function(data,type,row,meta){return Number(data).toFixed(2);}},
        ]
      });
    });
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    The problem here is that the columnDefs are actually applied before the columns (allowing the more specific columns to override anything set by the columnDefs).

    This is a limitation of the columnDefs.targets, no question. I haven't yet worked out a nice way of resolving it I'm afraid.

    Allan

  • nikhilsheth2049nikhilsheth2049 Posts: 17Questions: 4Answers: 0

    Any updates about columnDef.targets should be column title instead of column index?

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    No - this isn't likely to come until v2 I'm afraid.

    Allan

This discussion has been closed.