Error after update to Editor 2.0 saving Editor values. TypeError: Cannot read property 'length'

Error after update to Editor 2.0 saving Editor values. TypeError: Cannot read property 'length'

nlooijenlooije Posts: 20Questions: 4Answers: 0

Link to test case:
Debugger code (https://debug.datatables.net/elijoh)**:
**Error messages shown
:
jquery.js:348 Uncaught TypeError: Cannot read property 'length' of undefined
at Function.each (jquery.js:348)
at Editor._ajax (datatables.js:78912)
at datatables.js:80056
at Editor._event (datatables.js:79313)
at Editor._submit (datatables.js:80047)
at datatables.js:78448
at Editor._event (datatables.js:79313)
at send (datatables.js:78442)
at Editor.submit (datatables.js:78467)
at HTMLDocument.<anonymous> (datatables.js:79476)
Description of problem:
On every edit of an value on the pages this error appears, With Editor 1.9 it is working fine. In the debugger it is the table #dynamic-table-specification

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 54,578Questions: 1Answers: 8,533 Site admin

    Hi @nlooije,

    Thanks for letting me know about this error and the debug trace. I can see that you are adding an extra parameter (6 for this table) in the Ajax request, but that all looks okay.

    I'm not sure what your exact build is, so could you confirm what is on the line datatables.js:78912 for me please? Is it:

     $.each(opts.replacements, function(key, repl) {
    

    If so are you setting default values for the Editor Ajax object? If it isn't that, then can you show me your DataTables and Editor initialisation, including any event handlers for this table / editor please?

    Thanks,
    Allan

  • nlooijenlooije Posts: 20Questions: 4Answers: 0

    Yes line 78912 is indeed

    $.each(opts.replacements, function (key, repl) {

    It is on every page I have, so also pages without the extra parameter.

    Hereby the initialization of the Editor:

        var editor_specification;
        var ajaxlink = "app/data/ajax/anyDir/any.php";
        var selectorTable = "#dynamic-table-specification";
        editor_specification = new $.fn.dataTable.Editor({
            ajax: {
                url: ajaxlink,
                data: function (d) {
                    d.extra = '1234';
                }
            },
            table: selectorTable,
            fields: [
                {
                    label: "On certificate:",
                    name: "OPCERTJN"
                },
                {
                    label: "Mandatory:",
                    name: "VERPLICHTJN"
                },
                {
                    label: "External",
                    name: "INTERN_EXTERN"
                },
                {
                    label: "Comments:",
                    name: "OPMERKINGEN",
                    type: "textarea",
                    attr: {
                        placeholder: "Please enter an comment here"
                    }
                },
                {
                    label: "Min value",
                    name: "MIN"
                }
            ]
        });
    

    And the datatable is a lot of code because we have made an function to generate the tables on all the pages.

    When I use the package of Datatables 1.10.23 with Editor 1.9.6 everything works fine, when I use the package of Datatables 1.10.24 with editor 2.0 I get on every editor.submit the error.

  • nlooijenlooije Posts: 20Questions: 4Answers: 0

    I have solved it with changing the code at line 78912 with:

    // URL macros
        if(typeof opts.replacements == "object" && opts.replacements) {
            $.each(opts.replacements, function (key, repl) {
                opts.url = opts.url.replace('{' + key + '}', repl.call(this, key, id, action, data));
            });
        }
    

    I think that the opt.replacements value is not set with an array, but I can't find where to change my initialisation of the datatable to solve this.

  • allanallan Posts: 54,578Questions: 1Answers: 8,533 Site admin
    Answer ✓

    Yes - that is a fair fix - thanks for posting it.

    I haven't been able to replicate it locally - I'm thinking it might be that some versions of jQuery don't like being passed undefined into $.each. What version of jQuery are you using please? (regardless, I'll put a fix in, but it would be good to know so we can test it).

    Thanks,
    Allan

  • nlooijenlooije Posts: 20Questions: 4Answers: 0

    I'm using indeed an older version of jquery, v2.1.4. I'm not been able yet to update to the 3.xx branch of it.

  • allanallan Posts: 54,578Questions: 1Answers: 8,533 Site admin

    Excellent - thank you. I’ll try running our test suite against that.

    Regards,
    Allan

Sign In or Register to comment.