Ho to avoid sorting but keep the search field, "Show nn entries", etc?

Ho to avoid sorting but keep the search field, "Show nn entries", etc?

UlfDunkelUlfDunkel Posts: 3Questions: 1Answers: 0

Hi there.

I am using a very basic implementation of DataTables in one of my password-protected websites. Thus I cannot show a URL here.

In my PHP script, I include a local copy of dataTables.dataTables.css, dataTables.bootstrap4.min.css, jquery.dataTables.min.js, and dataTables.bootstrap4.min.js.

Then I create the properly sorted table using PHP. Afterwards, I simply call this JavaScript:

// DataTable
echo "<script>\n";
echo "$('#TP_clients_vwd').DataTable();\n";
echo "</script>\n";

I recognized that my table is badly sorted after this, listing Names with German Umlauts behind the regular letters, so everything's fine except that the DataTables script itself resorts my already sorted table data, damaging my German phonebook alphabetical order (which means, that e.g. "ä" is sorted like "ae", "ö" like "oe", "ü" like "ue", "ß" like "ss" etc.).

I then tried and switched off the DataTables sorting, using this change:

// DataTable
echo "<script>\n";
// fade DataTable sorting, because the table is sorted already
echo "new DataTable('#TP_clients_vwd', { order: false });\n";
echo "$('#TP_clients_vwd').DataTable();\n";
echo "</script>\n";

But then the Search field and all additional DataTables UI gimmicks like "Show nn entries", "[Previous | 1 | 2 | Next]" are gone, too.

How can I use DataTables properly but without it having the table data resorted on its own?

Any help or hint is really appreciated.

Best, Ulf

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 22,159Questions: 26Answers: 5,100
    edited July 7

    You can use order: [] to have Datatables order the data as supplied by the data source. See the first example in the order docs.

    Use ordering to disable the end users ability to click and sort the entire table or columns.orderable to disable sorting per column.

    Kevin

  • kthorngrenkthorngren Posts: 22,159Questions: 26Answers: 5,100

    Another option to allow Datatables to sort the column is to try the diacritic sorting plugin.

    Kevin

  • allanallan Posts: 64,743Questions: 1Answers: 10,713 Site admin

    The diacritic sorting plugin shouldn't actually be needed any more with DataTables 2. Are you able to PM me a login to your page so I can take a look at what is going wrong?

    That said, as Kevin says, you can also disable DataTable's sorting if you prefer.

    Allan

  • UlfDunkelUlfDunkel Posts: 3Questions: 1Answers: 0

    Thank you all for your comments and recommendations. "order: []" doesn't change the behavior, compared with "order: false". DataTable doesn't sort in both cases, but also removes the navigation elements mentioned in my initial posting.

  • allanallan Posts: 64,743Questions: 1Answers: 10,713 Site admin
    Answer ✓

    That suggests to me that you've actually got a Javascript error on the page. If you look at the browser's console, is there anything shown there? Can you link to the page or show us the full JS initialisation for the DataTable?

    Allan

  • rf1234rf1234 Posts: 3,143Questions: 92Answers: 433

    Hi Ulf,

    I use the international order plug in
    https://datatables.net/plug-ins/sorting/intl

    I have two languages on my pages, English and German. This is how I handle it. Works fine. I run this code prior to data table initialization.

    //sorting:
    //Use the phonebook variant of the German sort order, 
    //which expands umlauted vowels to character pairs: ä → ae, ö → oe, ü → ue.
    if (lang === 'de') {
        $.fn.dataTable.ext.order.intl("de-DE-u-co-phonebk");
    } else {
        $.fn.dataTable.ext.order.intl("en-GB");
    }
    

    With the current version of the Plug-In you might have to use it this way:

    DataTable.intlOrder('de-DE-u-co-phonebk');
    

    Roland

  • allanallan Posts: 64,743Questions: 1Answers: 10,713 Site admin

    I knew there was a plugin that provided locale specific options but I couldn't remember its name! Many thanks @rf1234!

    Allan

  • UlfDunkelUlfDunkel Posts: 3Questions: 1Answers: 0

    @Allan: Thank you for pointing me to the Browser console. That did the trick. Now that I tidied up my code this simple version does all that I wanted:

    // DataTable
    echo "<script>\n";
    echo "$('#TP_clients_vwd').DataTable( {order:[]} );\n";
    echo "</script>\n";

    Thank all of you for this extraordinary forum and your helpful comments. This kind of support has become very rare. I really appreciate it.

    Best, Ulf

Sign In or Register to comment.