call public function with initComplete

call public function with initComplete

TomLTomL Posts: 4Questions: 1Answers: 0

Hi,
iam looking for a way to call a public function after my DataTable is done with loading the Data from ajax.
Thought initComplete might be the solution. But i got "Uncaught TypeError: this.enableEverything is not a function"

Here is my Code:

private initVolumenTable() {        
        const url = 'resources/limitoverview/banks/loadVolumen';
        const type = 'GET';
        $('#volumen-table').DataTable({
            ajax: {
                url: url,
                dataSrc: 'dhVolumenList',
                contentType: "application/json",
                type: type
            },
            columns: [
                { data: "stammnr" },
                { data: "bank" },
                { data: "maxLfz" },
                { data: "limitEur" },
                { data: "ausnutzung" },
                { data: "verfuegbar" },
                { data: "maxLimitEur" },
                { data: "maxAusnutzung" },
                { data: "maxVerfuegbar" },
            ],
            columnDefs: [
                { targets: [3, 6], render: $.fn.dataTable.render.number('.', null, null, null, '€'), className: 'dt-body-right' },
                { targets: [4, 5, 7, 8], render: $.fn.dataTable.render.number('.', ',', 2, null, '€'), className: 'dt-body-right' },
                { targets: 2, className: 'dt-body-center' }, //TODO css klasse scheint Probleme zu haben
            ],
            scrollY: "400px",
            scrollCollapse: false,
            paging: false,
            ordering: true,
            order: [[5, "desc"]],
            searching: false,
            info: false,
            alwaysCloneTop: true,
            language: {
                zeroRecords: this.noDataFoundLabel
            },
            initComplete: function (settings, json) {
                this.enableEverything();
            },
        });

    }

Replies

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin

    What is enableEverything a method of? this in the initComplete callback is the table node.

    Allan

  • TomLTomL Posts: 4Questions: 1Answers: 0

    Hi Allan,

    enableEverything() {
            this.isUsable = true;
            this.eventAggregator.publish(new DatepickerEnableEvent());
        }
    

    This function just enable variables who are binded to html buttons and Datepickers.
    like: <button type="button" disabled.bind="!isUsable">

    in my typescript file i have to use this for mostly everything.

    Thank you
    Tom

  • kthorngrenkthorngren Posts: 21,309Questions: 26Answers: 4,948

    If enableEverything is a standalone function then just call it without this.

    Kevin

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin

    It doesn't look like it is since it uses this inside it. The syntax also appears to suggest that it is a method of a class. @TomL you need to use the variable that refers to the instance of that class - myPage.enableEverything() for example.

    Might be worth reading through a tutorial such as this one to understand scoping and context in Javascript.

    Allan

  • TomLTomL Posts: 4Questions: 1Answers: 0
    edited August 2017

    Thanks for your answer, allan
    u r right. my InitVolumenTable() is in the same Class (Banks) like my method enableEverything().
    When i try to call:

    ...
    initComplete: function (settings, json) {
                    Banks.enableEverything();
                }
    

    I get the typescirpt error:"Property 'enableEverything' does not exist on type 'typeofBanks'. "

    But i got a ( maybe temp and not beautiful) solution:

     $('#volumen-table').on( 'init.dt', ()=> {
                this.enableEverything();
        
    

    with this i can easily call my annoying ts method on the same time like initComplete ;P

    Tom

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin

    Have you created an instance of your class? You need to call the instance member. It is not a static function, so you can't just use Banks.enableEverything().

    Allan

This discussion has been closed.