Sending custom column attributes to the server on POST

Sending custom column attributes to the server on POST

kmd1970kmd1970 Posts: 36Questions: 8Answers: 1
edited January 2015 in Free community support

I am using Datatables version 1.10.4

I would like to send a custom column attribute to the server on POST for every column. (I can't use the "Name" attribute.)

Example :

For each column I would like to send an "Alias" value to the server.

$('#example').dataTable( {
  "columns": [
    { "data": "engine", "alias" : "engine_type" },
    { "data": "browser", "alias" : "browser_type" },
    { "data": "platform", "alias" : "platform_version" },
    { "data": "version", "alias" : "version_number" },
    { "data": "grade", "alias" : "grade_level" }
  ]
} );

I assume the array sent would look like this if successful :

columns[0][data]    engine
columns[0][name]    
columns[0][alias]   engine_type
columns[0][orderable]   false
columns[0][search][regex]   false
columns[0][search][value]   
columns[0][searchable]  false

columns[1][data]    browser
columns[1][name]    
columns[1][alias]   browser_type
columns[1][orderable]   false
columns[1][search][regex]   false
columns[1][search][value]   
columns[1][searchable]  false

... etc, etc, etc

Any help would be appreciated

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,805Questions: 1Answers: 10,119 Site admin

    Use either ajax.data or preXhr to add additional information to the data sent to the server.

    Allan

  • kmd1970kmd1970 Posts: 36Questions: 8Answers: 1
    edited May 2015

    Hi Allan, Yes, I am using ajax.data to send additional information to server but I want to set the custom attributes at the column level. Custom per column attributes like data, name, search, etc.

    I suppose I could use ajax.data and the column index for every column but that
    is going to look a bit sloppy.

    "data": function(d) {
                 d.columns[0]['alias'] = 'engine_type';
                 d.columns[1]['alias']  = 'browser_type';
                 d.columns[2]['alias']  = 'platform_version';
                 ... etc, etc
                 d.deleted = 0;  
                },
    
  • allanallan Posts: 61,805Questions: 1Answers: 10,119 Site admin
    Answer ✓

    Why wouldn't you just use a for loop? Certainly, if you want to add a property to each element in the array you either need to do it in a loop or statically, I don't see what other option there is?

    Allan

  • kmd1970kmd1970 Posts: 36Questions: 8Answers: 1

    Not using for loop since the custom attribute values are different for each column.

    Actually, your first suggestion should work, although I am no longer able to mark it as the answer. Thanks again.

This discussion has been closed.