jquery datatables: adding extra column

前端 未结 3 2000
轮回少年
轮回少年 2020-12-08 07:19

Scenario

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

相关标签:
3条回答
  • 2020-12-08 07:38

    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",
            ""
        ]
    
    0 讨论(0)
  • 2020-12-08 07:38

    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

    0 讨论(0)
  • 2020-12-08 07:44

    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');
        });
    
    } );
    
    0 讨论(0)
提交回复
热议问题