0

I'm using DataTables to fill a table and need some data from a different source. So I started off with render and made a second ajax request by the ID's of what I need the name of. Here is my code, that will explain it more clearly.

$('#table').DataTable({
    "ajax": "/api/url",
    "language": {
        "url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/German.json"
    },
    "order": [[0, "asc"]],
    "pagingType": "simple",
    "responsive": {details: false},
    "bAutoWidth": true,
    "lengthChange": true,
    "scrollX": false,
    "aoColumnDefs": [{}],
    "columns": [
        {
            "data": "typ_name"
        },
        {
            "data": "mietpreise",
            render: function (data, type, row, meta) {
                let tmp = [],
                    currentCell = $("#table").DataTable().cells({
                        "row": meta.row,
                        "column": meta.col
                    }).nodes(0);

                data.forEach(function (item) {
                    $.get("/api/url2/" + item.preis_kundengruppe_id, function (row, status) {
                        tmp.push(row.data.kundengruppe_name);
                    });
                });
                console.log(tmp);

                return $(currentCell).text(tmp);
            }
        },

I'm expecting to have the value of kundengruppe_name in the array like ['case1', 'case2'] but in the console I get this

[]
0: "Endkunden"
1: "Händler"
length: 2
__proto__: Array(0)

So what I want is to find under 0 and 1 and not in the array as expected. What am I doing wrong? What's going on with the array?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
DBR
  • 146
  • 1
  • 10
  • Does this answer your question? [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Heretic Monkey Feb 10 '21 at 18:25
  • Thanks for the link, I know about async. That's why I'm using the currentCell. I don't need to wait or have the issue with having a result to a time when I don't need it anymore or don't know where to put it. currentCell refers to the cell I want to update, regardless if the first ajax call is finished, still working or something else. I use this with lower complexity on another table and that is working fine. But I will have a look at the link, seems very detailed, – DBR Feb 12 '21 at 22:36

0 Answers0