How to make a function in Javascript that receives a Datatable as a parameter?

Asked

Viewed 137 times

1

Hello

I made a generic function in Javascript that needs to receive a Datatable as a parameter, but for some reason it is not taking the data from this Datatable.

This function is called when the user clicks on the Datatable cell

$('#tableBaseHour').on('click', 'tbody td', function () {
    ClickTables('#tableBaseHour', $('#tableBaseHour thead th').length);
});

I will have a total of 12 Datatables on the same screen, so for this reason I need a generic function that takes only the Datatable id and does the function. The function is this:

function ClickTables(clickTable, lengthCols) {
var table = $(clickTable).DataTable();

var colIndex = table.column($(this).closest('td')).index();

var rowclicked = table.row($(this).closest('td')).index();

//SE A LINHA CLICADA NÃO ESTÁ DENTRO DA ÁREA DE CAMPOS QUANTITIES
if (table.rows().count() == table.row($(this).closest('tr')).index() ||
    colIndex == lengthCols - 1 || colIndex == 0 || rowclicked == 12) {
    return;
}
//SE A CÉLULA CLICADA ESTÁ DENTRO DA ÁREA DE CAMPOS QUANTITIES E É HABILITADA À EDIÇÃO
else {
    var row = table.row($(this).closest('tr')).index();
    //ABRE A MODAL DE EDIÇÃO DO CAMPO
    $('#newBHUnit').modal('show');
    $('#YearId').prop('disabled', true);
    $('#Month').prop('disabled', true);
    $("#InputTypeId").prop('disabled', true);
    $("#AnalyzeTypeId").prop('disabled', true);
    $("#LowTierId").prop('disabled', true);

    //CHAMA A FUNÇÃO QUE REALIZA O EDIT
    EditBaseHour(table.column(colIndex).title, $('#findYearBH').val(), row + 1);
}
}

This function is taking the information from the field, playing for a Modal and allowing editing the record in it.

When the function was applied directly within the on click it worked perfectly, but for some reason inside the method it does not work (the data passed in Edit that depend on the Datatable information passes null and the Edit function is not performed).

Could someone help me with this?

  • 1

    using "onclick" we can use "$(this)" but in a generic function it does not refer to the clicked object but to the parameters passed.

  • That was exactly what was giving problem, I passed what was being referenced by this as parameter to the function and it worked. Thank you!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.