Problem with Sorting

Problem with Sorting

AaronZocktAaronZockt Posts: 2Questions: 1Answers: 0
edited January 10 in Free community support

Hello there :) ,

I have a quick question about the sorting

"order": [
    [2, 'asc']
]

is leaving me with a result like this:

Test A01
Test B01
Test A02
Test B02

and I want the result to look like this:

Test A01
Test A02
Test B01
Test B02

How would I accomplish that?

Answers

  • kthorngrenkthorngren Posts: 21,554Questions: 26Answers: 4,994

    See if the Natural sort plugin works.

    Kevin

  • AaronZocktAaronZockt Posts: 2Questions: 1Answers: 0
    edited January 15

    Here's a code snippet of what I got so far, I want 'Test ABC' to be sorted as described in the original Post.

    <table class="table table-striped dbtable" id="dbtable">
        <thead>
            <tr>
                <th>Test 1</th>
                <th>&nbsp;</th>
                <th>Test ABC</th>
                <th>Test 2</th>
            </tr>
            </thead>
            <tbody>
        </tbody>
    </table>
    
    <script type="text/javascript" src="https://cdn.datatables.net/v/bs/jq-3.7.0/dt-2.2.1/datatables.min.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/plug-ins/2.2.1/sorting/natural.js"></script>
    
    <script type="text/javascript">
    
        $(document).ready(function() {
            $('#dbtable').DataTable({
                "processing": true,
                serverSide: true,
                ajax: {
                    url: "data.php",
                    type: "POST",
                },
                "pageLength": 100,
                columns: [
                    {
                        name: 'test1',
                        data: "test1",
                        sortable: true,
                     
                    },
                    {
                        name: 'flag',
                        data: "flag",
                        sortable: false,
                    },
                    {
                        name: 'testabc',
                        data: 'testabc',
                        sortable: true,
                    },
                    {
                        name: "test2",
                        data: "test2",
                        sortable: true,
                    },
                ],
    
                colReorder: false,
                fnServerParams: function(data) {
                    data['order'].forEach(function(items, index) {
                        data['order'][index]['name'] = data['columns'][items.column]['data'];
                    });
                },
    
                "order": [[2, 'asc']],
    
                columnDefs: [
                    { type: 'natural-nohtml', target: 2 }
                ]
                
            });
        });
    
    </script>
    
Sign In or Register to comment.