language.info: how to change message when there's only one entry (feature request?)

language.info: how to change message when there's only one entry (feature request?)

Bob RodesBob Rodes Posts: 17Questions: 1Answers: 0
edited May 2016 in Free community support

I see language.info options for zero entries showing, zero records found, etc. I might be missing something, but what I do not see is the ability to directly vary the info when there is one and only one entry. Since variation between singular and plural is common to most languages, it might be a useful feature to add. A case in point: I changed a scrollable table to say "Showing END Entries". When there is only one entry, I'd like it to say "Showing 1 Entry" instead of "Showing 1 Entries". Is there a way to do this that I'm missing?

Replies

  • Bob RodesBob Rodes Posts: 17Questions: 1Answers: 0
    edited May 2016

    My question now is how do I make this say "Showing 1 entry" when I filter down to one record? I've read up on the 'search.dt' event, but I don't see how to inject a new string into the info value without making a new call to dataTable() every time the user changes the search criteria. That seems wrong.

  • allanallan Posts: 61,934Questions: 1Answers: 10,154 Site admin

    Unfortunately there isn't a trivial way to do this in DataTables at the moment. You could using infoCallback, but there isn't a nice simple way to handle the singular case.

    Allan

  • Bob RodesBob Rodes Posts: 17Questions: 1Answers: 0

    That seems to be what I'm looking for. I'll experiment with it when I get around to it and post the code when I come up with something. Thanks Allan.

  • allanallan Posts: 61,934Questions: 1Answers: 10,154 Site admin

    That would be great - thanks.

    The language options are something I'd like to improve more, but its exceptionally difficult to offer the required options for the various languages. If there was just a single number being displayed I've got a function for that, but with multiple numbers being displayed its a bit of a nightmare.

    There was a GNU page that describe the issues with internationalisation which I now can't find, but their general overview of getttext describes some of the issues nicely (should you be interested...).

    Allan

  • Bob RodesBob Rodes Posts: 17Questions: 1Answers: 0
    edited June 2016

    As it turns out, using infoCallback was very straightforward.

    I got into this because the default info is better suited to a paged table than a scrolling one. In a scrolling table the start and end parameters are redundant, as they will always equal 1 and total, respectively. The info would say something like "Showing 1 to 40 of 40 entries" when it could just as well say "Showing 40 entries". So, I wanted this (I also wanted to say "record" instead of "entry"):

    1. If there are zero records in the table, say "Showing 0 records".
    2. If there is one record in the table, say "Showing 1 record".
    3. If there are x records in the table, where x>1 and we aren't applying a filter, say "Showing x records".
    4. Otherwise, say "Showing 'y of x records", where x is the total records in the table, and y is the total records shown.

    In the options list for the DataTable() call, I started with this (although I changed _END_ to _TOTAL_ after I got clearer on the meanings of the parameters):

            language: {
                info: 'Showing _TOTAL_ ' + (1 == _MAX_ ? 'record' : 'of _MAX_ records'),
                infoEmpty: 'Showing 0 records',
                infoFiltered: ''        
            }
    

    Which works ok, but misses requirement 3 above. As I write this, I see I could nest another ternary operator into the info parameter:

            info: 'Showing _TOTAL_ ' + (1 == _MAX_ ? 'record' : 
                (_MAX_ == _TOTAL_ ? 'records' : 'of _MAX_ records')),
    

    But that's rather messy. Using infoCallback is clearer and more manageable. I got rid of language altogether and substituted this:

            'infoCallback': function( settings, start, end, max, total, pre ) {
                if (0 == max) { return 'Showing 0 records'; }
                if (1 == max) { return 'Showing 1 record'; }
                if (total == max) { return 'Showing ' + max + ' records'; }
                return 'Showing ' + total + ' of ' + max + ' records'; 
            }  
    
  • allanallan Posts: 61,934Questions: 1Answers: 10,154 Site admin

    Awesome - thanks for the feedback! I'll look into how to improve that options in future.

    Allan

This discussion has been closed.