locked
How can find the column number using column name datatable , Please help RRS feed

  • Question

  • User-1355965324 posted

    I am using the  datatable using the following ,   is it  possible to find target  column number using the column name .  For example column Target 1  should be DepotName and 

    Column 2  =  EmployeeName for  the order     . So how to find the column number using the column name in JS Datatable. It would be very greate if you can help

    order: [[2, 'asc'], [1, 'asc'], [16, 'asc']],  

     $('#tblAttendance').DataTable({
                "ajax": {
                    "url": "/Report/AttendanceReportData",
                    "data": report,
                    "dataSrc": function (json) {
                        return JSON.parse(json);
                    }
                },
    
                "columns": [
                    { "data": "FromDate" },
                    { "data": "DepotName" },
                    { "data": "EmployeeName" },
                    { "data": "ClockedIn" },
                    { "data": "ClockedOut" },
                    { "data": "IsNormalAttendance" },
                    { "data": "IsDayOffMarked" },
                    { "data": "IsSickMarked" },
                    { "data": "IsHolidayMarked" },
                    { "data": "IsMaternity" },
                    { "data": "IsTimeOff" },
                    { "data": "TimeOffHrs" },
                    { "data": "IsDeducted" },
                    { "data": "DueHrs" },
                    { "data": "ExtraTimeHrs" },
                    { "data": "Description" },
                    { "data": "SortDate" }
    
                ],
                order: [[2, 'asc'], [1, 'asc'], [16, 'asc']],
                rowGroup: {
                    dataSrc: ["EmployeeName", "DepotName"],
                    startRender: function (rows, group) {
                        return $('<tr/>')
                            .append('<td class="td-left" >' + group + '</td>');
                    }
                },
    
    
                "columnDefs": [
                    {
                        "targets": [1],
                        "visible": false
                    },
                    {
                        "targets": [2],
                        "visible": false
                    },
                    {
                        "targets": [16],
                        "visible": false
                    },
                    {
                        "className": "dt-center", "targets": "_all"
                    },
                    { "width": "10%", "targets": 0 },
                    { "width": "10%", "targets": 4 },
                ],
                "pageLength": 40,
                scrollY: "500px",
                scrollX: true,
                 paging: true,
                 dom: 'Bfrtip',
                buttons: [
                    'copy',
                    'csv',
                    'excel',
                    {
                        extend: 'pdfHtml5',
                        orientation: 'landscape',
                        pageSize: 'LEGAL' }
    
                ]
    
            });
        });
    </script>

    Wednesday, October 9, 2019 9:38 AM

Answers

  • User-719153870 posted

    Hi polachan,

     is it  possible to find target  column number using the column name

    I think what you are looking for is column-selector.

    Please refer to below code:

    <script>
            $(document).ready(function () {
                var table = $('#example').DataTable({
                    columns: [
                        { name: 'Name' },
                        { name: 'Position' },
                        { name: 'Office' },
                        { name: 'Age' },
                        { name: 'Start date' },
                        { name: 'Salary' }
                    ]
                });
                alert(table.column('Name:name').index());
            });
        </script>

    This will get you the number of the column whose name is "Name", and the number is 0.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 10, 2019 2:39 AM

All replies

  • User-474980206 posted

    try the docs:

      https://datatables.net/reference/api/columns()

    Wednesday, October 9, 2019 3:52 PM
  • User-719153870 posted

    Hi polachan,

     is it  possible to find target  column number using the column name

    I think what you are looking for is column-selector.

    Please refer to below code:

    <script>
            $(document).ready(function () {
                var table = $('#example').DataTable({
                    columns: [
                        { name: 'Name' },
                        { name: 'Position' },
                        { name: 'Office' },
                        { name: 'Age' },
                        { name: 'Start date' },
                        { name: 'Salary' }
                    ]
                });
                alert(table.column('Name:name').index());
            });
        </script>

    This will get you the number of the column whose name is "Name", and the number is 0.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 10, 2019 2:39 AM