$.ajax doesn't work

$.ajax doesn't work

nessinitsnessinits Posts: 86Questions: 27Answers: 0

Does anyone know what's wrong? The $.ajax to configure the i18n (language) data doesn't seem to be triggered or doesn't work.

(function($){

    $(document).ready(function() {
        
        // am_settingsEditor
        var am_settingseditor = new $.fn.dataTable.Editor( {
            ajax: url_root + 'assets/app/' + app_id + '/php/table.am_settings.php',
            table: '#am_settings',
            fields: [ 
                {
                    label: "Naam:",
                    name:  "am_settings.name"
                },{
                    label: "Testwaarde:",
                    name:  "am_settings.rulevalue"
                },          {
                    label: "Waarde:",
                    name:  "am_settings.value"
                }
            ],
        }); // am_settingsEditor

        // am_settingsEditor
        $.ajax( {
            url: url_root + 'assets/app/am/core/language.json',
            dataType: 'json',
            success: function ( json ) {
                am_settingseditor = new $.dataTable.Editor( {
                    i18n: json,
                } );
            }
        } );

        // am_settingsTable
        var am_settingstable = $('#am_settings').DataTable( {
            dom: 'Bfrtip',
            ajax: url_root + 'assets/app/' + app_id + '/php/table.am_settings.php',
            columns: [
                { data: "am_settings.name" },
                { data: "am_settings.rulevalue" },
                { data: "am_settings.value" }
            ],
            select: true,
            lengthChange: false,
            buttons: [
                { extend: 'create', editor: am_settingseditor },
                { extend: 'edit',   editor: am_settingseditor },
                { extend: 'remove', editor: am_settingseditor }
                ],
            language: {
                url: '//cdn.datatables.net/plug-ins/1.10.15/i18n/Dutch.json'
            }
        });  // am_settingsTable

    }); // end function ready

}(jQuery));

This question has accepted answers - jump to:

Answers

  • nessinitsnessinits Posts: 86Questions: 27Answers: 0

    Evolution resulted to the following, but still it's not working

            // am_settingsEditor
            var am_settingseditor = new $.fn.dataTable.Editor( {
                ajax: url_root + 'assets/app/' + app_id + '/php/table.am_settings.php',
                table: '#am_settings',
                fields: [ 
                    {
                        label: "Naam:",
                        name:  "am_settings.name"
                    },{
                        label: "Testwaarde:",
                        name:  "am_settings.rulevalue"
                    },          {
                        label: "Waarde:",
                        name:  "am_settings.value"
                    }
                ],
                i18n: function () {
                    $.ajax( {
                        url: url_root + 'assets/app/am/core/language.json',
                        dataType: 'json',
                        success: function ( json ) {
                            return json;
                        }
                    } )
                }
            }); // am_settingsEditor
    
    
  • rduncecbrduncecb Posts: 125Questions: 2Answers: 28

    Returning a value from an ajax success callback does not return the value from the enclosing ajax function, in this case i18n will be undefined. You need to refactor your code so the ajax call is made and you create the Editor from the ajax success callback, much like in your first post, however, you need to provide more options to the Editor constructor than just i18n like in your second example.

  • nessinitsnessinits Posts: 86Questions: 27Answers: 0

    Thanks, rduncecb. In that cane I have another question. I've tried that, but when I do it like that, I get an error on the creation of my buttons in the datatable.

                buttons: [
                    { extend: 'create', editor: am_settingseditor },
                    { extend: 'edit',   editor: am_settingseditor },
                    { extend: 'remove', editor: am_settingseditor }
                    ],
    
    

    These buttons result in an error Can't find variable am_settingseditor. Any idea how to solve that?

  • allanallan Posts: 63,893Questions: 1Answers: 10,531 Site admin

    It suggests that the am_settingseditor variable hasn't been set by the time that code executes, or perhaps it is done in a different scipe.

    Can you link to the page so I can see the whole script?

    Allan

  • nessinitsnessinits Posts: 86Questions: 27Answers: 0

    Allan,

    The current code is:

    (function($){
                
        $(document).ready(function() {
    
            $.ajax( {
                url: url_root + 'assets/app/am/core/language.json',
                dataType: 'json',
                success: function ( json ) {
                    am_settingseditor = new $.dataTable.Editor( {
                        ajax: url_root + 'assets/app/' + app_id + '/php/table.am_settings.php',
                        table: '#am_settings',
                        fields: [ 
                        {
                            label: "Naam:",
                            name:  "am_settings.name"
                        },{
                            label: "Testwaarde:",
                            name:  "am_settings.rulevalue"
                        },          {
                            label: "Waarde:",
                            name:  "am_settings.value"
                        }
                        ],
                        i18n: json,
                    });
                }
            } );    
    
            // am_settingsTable
            var am_settingstable = $('#am_settings').DataTable( {
                dom: 'Bfrtip',
                ajax: url_root + 'assets/app/' + app_id + '/php/table.am_settings.php',
                columns: [
                    { data: "am_settings.name" },
                    { data: "am_settings.rulevalue" },
                    { data: "am_settings.value" }
                ],
                select: true,
                lengthChange: false,
                buttons: [
                    { extend: 'create', editor: am_settingseditor },
                    { extend: 'edit',   editor: am_settingseditor },
                    { extend: 'remove', editor: am_settingseditor }
                    ],
                language: {
                    url: '//cdn.datatables.net/plug-ins/1.10.15/i18n/Dutch.json'
                }
            });  // am_settingsTable
    
        }); // end function ready
    
    }(jQuery));
    

    The result is as described: An error (Can't find variable am_settingseditor) on:

                buttons: [
                    { extend: 'create', editor: am_settingseditor },
                    { extend: 'edit',   editor: am_settingseditor },
                    { extend: 'remove', editor: am_settingseditor }
                    ],
    
    
  • rduncecbrduncecb Posts: 125Questions: 2Answers: 28
    Answer ✓

    Line 30 is executed BEFORE the ajax success callback is made due to ajax being asynchronous, this means am_settingseditor has not been defined and you get the error you see. You'll need to refactor the code to only create the table (or only add the buttons to the table See button().add() ) from the success callback on line 8

  • allanallan Posts: 63,893Questions: 1Answers: 10,531 Site admin
    Answer ✓

    am_settingseditor is only being created when the Ajax call is completed. But it is being used before that. Remember that Ajax is async!

    Try moving your DataTables initialisation into the Ajax success function.

    Allan

  • nessinitsnessinits Posts: 86Questions: 27Answers: 0

    Thanks, it works.

This discussion has been closed.