In jQuery Datatables, how do we define fixed value columns?

Since Jquery.datatables

I've created a data table that dynamically loads data from the server Ajax

into a Json

. this is my code:

$('#MyTable').dataTable({
  'sAjaxSource': '/Accounting/Action/GetData',
  'bStateSave': false,
  'bProcessing': true,
  'bServerSide': true,
  'data': {
  },
  'lengthChange': true,
  'aoColumns': [
    {
      'mData': 'ActionID',
      'title': 'کد',
      'sClass': ''
    },
    {
      'mData': 'ActionTitle',
      'title': 'عنوان',
      'sClass': ''
    }
  ],
  'sPaginationType': 'bootstrap'
});

      

it generates:

ActionID | ActionTitle  
=========+=============
  1      |  Foo         
---------+-------------
  2      |  Bar         
---------+-------------
  3      |  Blah Blah   
---------|------------

      

Ok, but I want to add a fixed value column for all rows, and I don't do that by adding a column to the Json data that the server sends. for example i want to add MyFixedCol

in MyGrid

client side:

ActionID | ActionTitle | MyFixedCol
=========+=============+===========
  1      |  Foo        | FIXEDVAL
---------+-------------+-----------
  2      |  Bar        | FIXEDVAL
---------+-------------+-----------
  3      |  Blah Blah  | FIXEDVAL
---------|-------------+-----------

      

How can i do this?

+3


source to share


1 answer


I found a trick and it worked for me. so i am sharing here maybe help someone :)

datatables

has a callback createdRow

that runs it when the string is created. then I add <td>

to the appropriate line and runs for all lines:

'createdRow': function ( row, data, index ) {
    $(row).append($('<td>').html('FIXEDVAL'));
  }

      



so my code in the question was complete:

$('#MyTable').dataTable({
  'sAjaxSource': '/Accounting/Action/GetData',
  'bStateSave': false,
  'bProcessing': true,
  'bServerSide': true,
  'data': {
  },
  'lengthChange': true,
  'aoColumns': [
    {
      'mData': 'ActionID',
      'title': 'کد',
      'sClass': ''
    },
    {
      'mData': 'ActionTitle',
      'title': 'عنوان',
      'sClass': ''
    }
  ],
  'sPaginationType': 'bootstrap',
  'createdRow': function ( row, data, index ) {
       $(row).append($('<td>').html('FIXEDVAL'));
  }
});

      

0


source







All Articles