$.each in render function

$.each in render function

xKushGenexKushGene Posts: 2Questions: 2Answers: 0
edited December 2017 in Free community support

I want to Convert my column.data. So I load the results from the convert before i call the table to load:

function test(){
    return $.ajax({
        url: "../api.php?getGroups",
        type: "Get",
        dataType: 'json',
        success: function(data) {
            console.log(data);
        }
    });
}
// Load Table Data
$(document).ready(function() {
    $.when(test()).done(function(a1){
    var table = $('#customerTable').DataTable({
        "ajax": {
            "url": "../api.php?getUsers&type=kunden",
            "type": "GET",
            "dataSrc": ""
        },
        "columns": [
            { 
                // Format Date
                "data" : "created_at",
                render: function(data) {
                    return moment(data).format('LLL')
                }           
            },  
            { 
                // Convert Usergroup
                "data" : "usergroup",
                render: function(data){
                    $.each(a1, function(i, v) {
                        if(v.id == data){
                            return v.groupname;
                        }
                    });         
                },
                "defaultContent": "<i>Not Set</i>"
            },
          [....]

But the $.each doesn't work. Everytime i will get the "defaultContent". How can I return $.each results inside render ?

Answers

  • allanallan Posts: 63,457Questions: 1Answers: 10,466 Site admin

    Without knowing what the content of a1 is, I can't fully say. However, my guess is that the issue is your are returning from inside the $.each function (i.e. an inner anonymous function) rather than the rendering function. You should assign the matched value to a variable and then return that from outside the $.each.

    Allan

This discussion has been closed.