jquery datatables: adding extra column

76,716

Solution 1

You can define your columns in a different way like this

"aoColumns": [
        null,
        null,
        null,
        null,
        null,
        { "mData": null }
    ]

or this

"aoColumnDefs":[
    {
        "aTargets":[5],
        "mData": null
    }
]

Here some docs Columns

Take a look at this DataTables AJAX source example - null data source for a column

Note that prior to DataTables 1.9.2 mData was called mDataProp. The name change reflects the flexibility of this property and is consistent with the naming of mRender. If 'mDataProp' is given, then it will still be used by DataTables, as it automatically maps the old name to the new if required.

Another solution/workaround could be adding that '5' parameter...

For example adding extra "" to each row

like this:

    [
        "IT",
        "10030",
        "VILLAREGGIA",
        "TO",
        "Torino",
        ""
    ],
    [
        "IT",
        "10030",
        "VISCHE",
        "TO",
        "Torino",
        ""
    ]

Solution 2

Just in case anyone using a newer version of DataTables (1.10+) is looking for an answer to this question, I followed the directions on this page:

https://datatables.net/examples/ajax/null_data_source.html

Solution 3

Posting this answer here, just to show that where the aoColumnDefs needs to be defined. I got help from this question it self, but I struggled for a while for where to put the aoColumnDefs. Further more also added the functionality for onclick event.

$(document).ready(function() {
  var table = $('#userTable').DataTable( {
        "sAjaxSource": "/MyApp/proctoring/user/getAll",
        "sAjaxDataProp": "users",
        "columns": [
                    { "data": "username" },
                    { "data": "name" },
                    { "data": "surname" },
                    { "data": "status" },
                    { "data": "emailAddress" },
                    { "data" : "userId" }
                  ],
        "aoColumnDefs": [
           {
                "aTargets": [5],
                "mData": "userId",
                "mRender": function (data, type, full) {
                    return '<button href="#"' + 'id="'+ data + '">Edit</button>';
                }
            }
         ]
    } );

    $('#userTable tbody').on( 'click', 'button', function () {
        var data = table.row( $(this).parents('tr') ).data();
        console.log(data);
        $('#userEditModal').modal('show');
    });

} );
Share:
76,716
Alberto De Caro
Author by

Alberto De Caro

Software analyst and developer with 10 years of experience in web-based applications, multi-tier architectures, data integration. More then 3 years experience in web analytics, eCommerce, web tagging and implementation projects. Strong analytical and relationship attitudes. Good communication skills, always curious and learning oriented. Core technologies: Javascript, JSON, JQuery, AJAX HTML, CSS .NET technologies Web analytics: Adobe Analytics Adobe Dynamic Tag Manager (DTM) Systems and data integration: Sql Server SSIS XML Web Services

Updated on April 26, 2020

Comments

  • Alberto De Caro
    Alberto De Caro about 4 years

    Scenario

    I am using (@version 1.9.4) for the first time to display data to the user. I succeed in retrieving the data via ajax and in binding them to the datatable.

    Now I want to add extra columns to let the user process the records. For semplicity sake, the aim is to add a button with an onclick handler that retrieve the data of the 'clicked' record.

    In my plan I would bind the json item corresponding to the 'clicked' record to the onclick handler.

    Till now, if I add an additional TH for the action column to the DOM, an error occurs from datatables code:

    Requested unknown parameter '5' from data source for row 0
    

    Question

    How to set custom columns? How to fill their content with HTML?


    Here is my actual config.

    HTML

    <table id="tableCities">
        <thead>
            <tr>
                <th>country</th>
                <th>zip</th>
                <th>city</th>
                <th>district code</th>
                <th>district description</th>
                <th>actions</th>
            </tr>
        </thead>
        <tbody></tbody>
    </table>
    

    Javascript

    $('#tableCities').dataTable({
        "bPaginate": true,
        "bLengthChange": false,
        "bFilter": true,
        "bSort": true,
        "bInfo": false,
        "bAutoWidth": true
        , "bJQueryUI": true
        , "bProcessing": true
        , "bServerSide": true
        , "sAjaxSource": "../biz/GetCitiesByZip.asp?t=" + t
    });
    

    Sample Json result

    {
        "aaData":
        [
            [
                "IT",
                "10030",
                "VILLAREGGIA",
                "TO",
                "Torino"
            ],
            [
                "IT",
                "10030",
                "VISCHE",
                "TO",
                "Torino"
            ]
        ],
        "iTotalRecords": 2,
        "iTotalDisplayRecords": 2,
        "iDisplayStart": 0,
        "iDisplayLength": 2
    }
    

    Edit

    Daniel is right. The solution is to set up the custom column in the aoColumnDefs, specifying the mData and the mRender properties. In particular mRender lets to define custom html and javascript.

    /* inside datatable initialization */
    , "aoColumnDefs": [
       {
            "aTargets": [5],
            "mData": null,
            "mRender": function (data, type, full) {
                return '<a href="#" onclick="alert(\''+ full[0] +'\');">Process</a>';
            }
        }
     ]
    
  • Alberto De Caro
    Alberto De Caro over 11 years
    Straightforward, the 5th missing parameter just corresponds to the action column. To add an empty field to the data source seems to be a workaround rather then an answer.
  • PartialOrder
    PartialOrder almost 6 years
    Yup. Use "defaultContent"