Get tableID from within render function

Get tableID from within render function

DocNCDocNC Posts: 24Questions: 6Answers: 0

hello all
is it a "standard" way to access tableID from within a render function ?
I use this way, but there is a big warning about it.....
thanks
Michel

function render_xx(data,type,row,meta)
{
ID = meta.settings.sTableId;
}

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    That is probably as good as it gets at the moment. What do you need it for?

    Allan

  • DocNCDocNC Posts: 24Questions: 6Answers: 0

    I have complex data from vue
    Original tables are one to many
    COMPANY --> Contacts (name,surname,email)--> numbers (kind, number)
    the vue make a JSON Array of objects.

    [
        {
            "n":"xxxxx",
            "p": "Marc",
            "id": 2378,
            "num": {
                "mobile": "",
                "numero": ""
            },
            "mail": "**@xxx.nc; "
        },
        {
            "n":"xxxxx",
            "p": "Sandra ",
            "id": 2379,
            "num": {
                "mobile": "51 51 65",
                "numero": " 51 51 51 ; "
            },
            "mail": "**@xxx.nc"
        },
        {
            "n":"xxxxx",
            "p": "Frédérique",
            "id": 2380,
            "num": {
                "mobile": "51 51 33"
            },
            "mail": "**@xxx.nc"
        },
        {
            "n":"xxxxx",
            "p": "Henri",
            "id": 2381,
            "num": {
                "mobile": "51 51 06"
            },
            "mail": "**@xxx.nc"
        },
        {
            "n":"xxxxx",
            "p": "Yohan",
            "id": 2382,
            "num": null,
            "mail": "**@xxx.nc "
        },
        {
            "n":"xxxxx",
            "p": "José",
            "id": 2383,
            "num": null,
            "mail": "**@xxx.nc "
        },
        {
            "n":"xxxxx",
            "p": "Varinka",
            "id": 3458,
            "num": {
                "": "51 51 81"
            },
            "mail": "**@xxx.nc "
        },
        {
            "n":"xxxxx",
            "p": "Varinka COGULET",
            "id": 3459,
            "num": {
                "": "792181"
            },
            "mail": "**@xxx.nc"
        },
        {
            "n":"xxxxx",
            "p": "Anne-Laure",
            "id": 3460,
            "num": {
                "": "826523"
            },
            "mail": "**@xxx.nc"
        },
        {
            "n":"xxxxx",
            "p": "Jessika ",
            "id": 3461,
            "num": {
                "": "51 51 16"
            },
            "mail": "**@xxx.nc"
        },
        {
            "n":"xxxxx",
            "p": "Fanny",
            "id": 3462,
            "num": null,
            "mail": ""
        },
        {
            "n":"xxxxx",
            "p": "Natanaëlle",
            "id": 3747,
            "num": {
                "": "51 51 75"
            },
            "mail": "**@xxx.NC"
        },
        {
            "n":"xxxxx",
            "p": "Franck",
            "id": 3868,
            "num": null,
            "mail": ""
        },
        {
            "n":"xxxxx",
            "p": "Alice ",
            "id": 3884,
            "num": null,
            "mail": ""
        },
        {
            "n":"xxxxx",
            "p": "Denis ",
            "id": 3885,
            "num": null,
            "mail": ""
        },
        {
            "n":"xxxxx",
            "p": "Chloé",
            "id": 4355,
            "num": null,
            "mail": "**@xxx.nc"
        }
    ]
    

    data a list of contacts with numbers....
    several tables send me the same kind of data.
    When the data is in a "client" table, I have to show a cart (the client may buy) but not if from "contact" or "providers" table.
    It seems easier to check the table id than to write several renders who will look the same only for that.
    here is the render

    function render_Contact(data, type, full, meta) {
        if (data == undefined || data == null || data == "") return "";
        if (type !== "display") return data;
        try {
            var obj = JSON.parse(data);
        } catch (e) { return ""; }
        inclient = meta.settings.sTableId.toLowerCase().includes("client");
        IDclient = -1;
    
        var HTMLanswer = '<div class="case contact"><ul class="contact">';
        obj.forEach((item, n) => {
            HTMLanswer += "<li class='contact'>";
            for (let [key, value] of Object.entries(item)) {
                // console.log('n' +n+ 'key '+key+' value '+value);
                switch (key) {
                    case 'num':
                        HTMLanswer += HTMLnum(value);
                        break;
                    case 'mail':
                        HTMLanswer += HTMLmail(value);
                        break;
    
                    case 'id':
                        IDclient = value;
                        break;
                    default:
                        HTMLanswer += HTMLitem(value, key, 'p');
                        break;
                }
    
            }
            if (inclient) {
                HTMLanswer += '<a class="caddy" href="' + window.hrefBuy + IDclient + '"></a>';
                HTMLanswer += "</li>";
            } else
                HTMLanswer += "</li>";
    
        });
    
        HTMLanswer += '</ul class="contact"></div>';
    
        return HTMLanswer;
    }
    

    thanks for your support.
    Michel

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Answer ✓

    Hi Michel,

    Fair enough! Thanks for the explanation. Normally I will have a function that returns a configured rendering function - for example $.fn.dataTable.render.moment( 'Do MMM YYYY' ) (from our date / time rendering plug-in), whereby you configure it when initialising the table, but this would be a valid option as well.

    Allan

This discussion has been closed.