Question: How can I kill all my functions after execution?

Question

How can I kill all my functions after execution?

Answers 2
Added at 2016-12-29 16:12
Tags
Question

I have a condition column == 1 and if this is the case, the function MakeCellsEditable and the function myCallbackFunction are initialized:

<script src="https://raw.githubusercontent.com/ejbeaty/CellEdit/master/js/dataTables.cellEdit.js"></script>

$(document).ready(function () {
    var table = $('#myTable').DataTable();
    $('#myTable tbody').on('mousedown', 'td', function () {
        $('#myTable').data('column', table.cell(this).index().columnVisible);
    });
    if (column == 1) {
       table.MakeCellsEditable({
           "onUpdate": myCallbackFunction
       });
    }
});

function myCallbackFunction(updatedCell, updatedRow, oldValue) {
    var array = updatedRow.data();
    var id = array[0];
    var column = $('#myTable').data('column');
    console.log("The column is: " + column);
    jQuery.ajax({
        type: "POST",
        url: "update.php",
        data: {
            updatedCell: updatedCell.data(),
            id: id,
            column: column,
        },
        cache: false
    });
}

What I want to do is, after the functions are executed, I want to kill them. Because otherwise if I one time clicked column 1, then all my tables are editable (not only column 1). I tried table.unbind(); or table.die(), but this didn't work out.


I tested at the end of the code:

 function destroyTable() {
        if ($.fn.DataTable.isDataTable('#myTable')) {
            table.destroy();
            table.MakeCellsEditable("destroy");
        }
    }

But it didn't work out

Answers
nr: #1 dodano: 2016-12-29 17:12

To answer the question in the headline: Yep:

function thiswilljustworkonce(){
 alert("once");
this.thiswilljustworkonce=function(){};
}

thiswilljustworkonce();
thiswilljustworkonce();
nr: #2 dodano: 2016-12-29 17:12

Use columns option for CellEdit plugin to specify which column needs to be editable. There would be no need to remove event handler.

var table = $('#example').DataTable();

function myCallbackFunction (updatedCell, updatedRow, oldValue) {
    console.log("The new value for the cell is: " + updatedCell.data());
    console.log("The values for each cell in that row are: " + updatedRow.data());
}

table.MakeCellsEditable({
    "columns": [0],
    "onUpdate": myCallbackFunction,
    "confirmationButton": true
});

See this example for code and demonstration.

Source Show
◀ Wstecz