How can I limit string length?

How can I limit string length?

GameForGrantsGameForGrants Posts: 12Questions: 2Answers: 0
edited September 2019 in Free community support

I am not even sure how to phrase this question, so I was not sure how to search this.
My datatable is a leaderboard in which some of the usernames are emails. I need to limit the string length of the usernames so that the entire email is not being displayed to the world. Could anyone point me in the right direction?

Here is the code that my datatable is currently using to form the table (the 'name' value is the one I need to truncate, ideally this would be done by removing anything after the @ symbol, but I would be OK with at least limiting the number of characters):

$(function () {
            t1 = $('#table').DataTable({
                responsive: true,
                searching: false,
                "paging": false,
                lengthChange: false,
                "info": false,
                "order": [[0, "asc"]],
                
            
                
            });
            t2 = $('#table2').DataTable({
                responsive: true,
                searching: false,
                "paging": false,
                "info": false,

                lengthChange: false,
                "order": [[0, "asc"]]
            });

            t3 = $('#table3').DataTable({
                responsive: true,
                searching: false,
                "paging": false,
                "info": false,

                lengthChange: false,
                "order": [[0, "asc"]]
            });
            var sr = 20,sr2=20,sr3=20;
            firebase.database().ref("/players").orderByChild("score1").limitToLast(20).on("child_added", function (snp) {
                
                console.log(snp.val());
                t1.row.add([
                sr,
                snp.val().name,
                snp.val().score1
                ]).draw(false);
                
              

                        sr--;           
            })
            firebase.database().ref("/players").orderByChild("score1").limitToLast(20).on("child_added", function (snp) {

                console.log(snp.val());
                t3.row.add([
                sr3,
                snp.val().name,(
                parseInt(snp.val().score1) + parseInt(snp.val().score2))/2
                ]).draw(false);


                sr3--;
            })
            firebase.database().ref("/players").orderByChild("score2").limitToLast(20).on("child_added", function (snp) {
                console.log(snp);
                var row = new t2.row();
                row[0] = sr2;
                row[1] = snp.val().name;
                row[2] = snp.val().score2;
                t2.row.add(row).draw(false);                
                sr2--;
            })            
        })
        

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has an accepted answers - jump to answer

Answers

  • GameForGrantsGameForGrants Posts: 12Questions: 2Answers: 0

    Ok, so I actually did find the Ellipsis renderer after some searching and am able to get it to limit values to 5 digits. This is very nice, but since it is Javascript, would there be some way to just stop the characters after the @ symbol instead?

    This is the link that I followed: https://datatables.net/blog/2016-02-26

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    Hi @GameForGrants ,

    You could use columns.render for that and modify the string. The penultimate example is changing the string for ellipsis - you could do something similar and cut off at the ampersand.

    Cheers,

    Colin

  • GameForGrantsGameForGrants Posts: 12Questions: 2Answers: 0

    @colin

    Thanks for the reply. Could you give me an example of how I would cut it off at the ampersand? Since email length will always be a variable, I am not sure how to make that work.

    Thanks,
    Jason

  • kthorngrenkthorngren Posts: 21,160Questions: 26Answers: 4,921
    Answer ✓

    Using columns.render you could simply parse the string. Assuming the email format is something like name@place.xyz you could do something like this:

            "render": function ( data, type, row, meta ) {
              return data.split('@')[0];
            }
    

    Look at Asthon Cox in this example:
    http://live.datatables.net/kuworeda/1/edit

    That is the simplest form. You may need to adjust based on your data in whether you need orthogonal data to search for the email domains, etc.

    Kevin

  • GameForGrantsGameForGrants Posts: 12Questions: 2Answers: 0

    Bingo!
    Thank you so much!

This discussion has been closed.