Change some words in cells

Change some words in cells

MickManMickMan Posts: 33Questions: 5Answers: 0

Hi everyone,

first of all: sorry for my english.

I made a table that retrieve data from a json.
I have 2 version of the table for 2 languages and the translation of headers and other elements wasn't a problem.

However i can't find a way to translate some words taken by the json.

For example i need to translate "Scad: 02-11-2021 | Pot.Rend.: 34.14 %" to "Expiry: 02-11-2021 | Pot.Return: 34.14 %", so only the words "Scad" and "Pot.Rend" needs to be translated.

I tried renderer, celldata, jquery and javascript solutions but i was unable to perform the task.

My code looks like this:

    "ajaxSource": "SOURCE",
    "columnDefs": [
        { "visible": false, "targets": 0 }
    ],
            "displayLength": 9999,
            "columns": [
                { "data": "KeyID" },
                { "data": "ISIN" },
                { "data": "Sottostante", "width": "10%" },
                { "data": "Strike" },
                { "data": "Last" },
                { "data": "Performance" },
                { "data": "Capitale" },
                { "data": "AltreInfo", "width": "30%" },
                { "data": "Scheda" }
              ],

In this example "AltreInfo" is the column to target.

Anyone willing to write a piece of code that i can use? I'm not an expert at all, so please be kind.

Thank you very much.

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,949Questions: 87Answers: 416
    edited July 2020 Answer ✓

    You can do this on the server with a getFormatter (search the docs for getFormatters). Or you can do it client side as well.

    Client side you can just use "replace": https://www.w3schools.com/jsref/jsref_replace.asp

    // you need to have the user language available 
    // in your code somewhere
    var lang = 'en';
    
    { data: "AltreInfo", width: "30%" ,
        render: function ( data, type, row ) {
            if (lang === 'en') {     
               return data
                        .replace("Scad:", "Expiry:")
                        .replace("Pot.Rend.:", "Pot.Return:");        
            }
            return data; //return the Italian original value
        }
    },
    

    Please take note of this:

    So if you need to replace multiple Scads and Pot.Rends in one string you'll need a regular expression.

  • MickManMickMan Posts: 33Questions: 5Answers: 0

    Thank You so much for this!
    I looked for the replace function but i did some syntax error and didn't work.

    I spent hours trying to figure it out and then you came and saved me.

    Just for completeness, this is the piece of code that did the trick (i removed the lang var and added "" to data.

                    { "data": "AltreInfo", width: "30%" ,
    render: function ( data, type, row ) {
           return data
                    .replace("Scad:", "Expiry:")
                    .replace("Pot.Rend.:", "Pot.Return:");
        }
    

    Again big, big thank you.
    May i donate something for your help? Let me know. :)

  • rf1234rf1234 Posts: 2,949Questions: 87Answers: 416

    I am glad that you got it working! I am just a user of Editor and Data Tables, but I think the author of Data Tables and Editor, Allan Jardine, and his team deserve additional support. Please use this page for your donation by purchasing one of the supporter packages. Many thanks!
    https://datatables.net/purchase/index

  • MickManMickMan Posts: 33Questions: 5Answers: 0

    Done!

    <3

  • MickManMickMan Posts: 33Questions: 5Answers: 0

    Sorry to bother again...

    I have problems with special characters.

    I'm unable to change "Periodicità" in "Periodicity".
    Tried html and unicode, too, but both didn't work.

    Is there a solution for this?

    Thanks

  • rf1234rf1234 Posts: 2,949Questions: 87Answers: 416

    Interesting. It works no problem using Javascript. I did it with the W3 schools tool. You have the link above and "Periodicità" was replaced without any problem:

    You can try escaping special characters.
    https://www.freeformatter.com/html-escape.html#ad-output

    If I try the replacement with an escaped value it doesn't work though:

    I would try to find a solution by googling or searching Stack Overflow. Good luck.

  • MickManMickMan Posts: 33Questions: 5Answers: 0
    edited July 2020

    Yes i tried with &agrave; but didn't work.

    I'll try to find a solution and i'll be report it here if found.

    Thanks again!

  • MickManMickMan Posts: 33Questions: 5Answers: 0

    Ok I found the solution!
    The page encoding was missing. :#

    <meta charset="utf-8">

    This fix the problem.

This discussion has been closed.