Sort datatable by letter 'Y' when search function used

Sort datatable by letter 'Y' when search function used

tj26tj26 Posts: 11Questions: 6Answers: 0

Hello,

I have a table (currently sorted by the 1st column alphabetically). The table has a search box so user can enter a name to search on, this returns the matches correctly... but I need the results to be sorted by the 2nd column so that any result that starts with the letter Y will be returned to the top of the table (all other letters can follow in any order)

Currently when I start typing the the search box the correct values are displayed but the airport-first-asc to order the 2nd column by 'Y' is not called,

Hope that makes sense.
Thank you for you help!

//Search sorting
        $.extend($.fn.dataTable.ext.type.order, {
            "airport-first-asc": function (ac) {
                console.log("ac is " + ac);
                if (ac.startsWith("Y")) {
                    return 1;
                }
                return 0;
            }
        });

        //Setup the Location Table. Can search on Common Name, Location ID and Sectors (Not Lat/Long/QNH)
            function setupTable() {
                var table = $('#locTable').DataTable({
                    ajax: '/locations/data',
                    "lengthChange": false,
                    "processing": true,
                    "scrollY": "400px",
                    "paging": true,
                    language: {
                        searchPlaceholder: "Enter Location",
                        search: "",
                    },
                    "columnDefs": [
                        { "searchable": false, "targets": [2, 3, 5] },
                        {
                            "render": function (data, type, row) {
                                return (Math.round(data * 100) / 100).toFixed(2);
                            }, "targets": [2, 3]
                        },
                        { "type": "airport-first-asc", "targets": 1 } //1 = LocationID
                    ],
                    searchDelay: 350,
                    "pageLength": 100,
                    "dom": '<fi<tr><"bottom"p>>',
                    "language": {
                        "processing": "<i class='fa fa-cog fa-spin fa-3x fa-fw'></i><span class='sr-only'>Loading...</span>",
                    }
                });
                return table;
            }

Data to look as below (cut it down to 2 columns, Filter on column 1 (name) and order on Column 2 codes starting with Y to go to the top of the filtered list). so search on sunshine should result in:

SUNSHINEA YBSS
SUNSHINEA SWQQ
SUNSHINEA WSQQ
SUNSHINEA TTWW

but doesn't, it shows
SUNSHINEA SWQQ
SUNSHINEA WSQQ
SUNSHINEA TTWW
SUNSHINEA YBSS

Html:

<input id="searchInput" value="Type To Filter">
<br/>
<table>
    <thead>
       
    </thead>
    <tbody id="fbody">
        <tr>
            <td>ABERDEEN</td>
            <td>ADSS</td>
        </tr>
        <tr>
            <td>ABERDEEN</td>
            <td>AWAS</td>
        </tr>
        <tr>
            <td>ABERDEEN</td>
            <td>TEEE</td>
        </tr>
        <tr>
            <td>ABERDEEN</td>
            <td>YABN</td>
        </tr>
        <tr>
            <td>ABERDEEN</td>
            <td>ZXSS</td>
        </tr>
         <tr>
            <td>SUNSHINEA</td>
            <td>SWQQ</td>
        </tr>
        <tr>
            <td>SUNSHINEA</td>
            <td>WSQQ</td>
        </tr>
        <tr>
            <td>SUNSHINEA</td>
            <td>TTWW</td>
        </tr>
        <tr>
            <td>SUNSHINEA</td>
            <td>YBSS</td>
        </tr>
    </tbody>
</table>

This question has an accepted answers - jump to answer

Answers

Sign In or Register to comment.