2016-12-29 90 views
2

我有一個條件column == 1,如果是這樣的話,功能MakeCellsEditable和功能myCallbackFunction初始化:如何在執行後終止我的所有功能?

<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 
    }); 
} 

我想要做的是,該功能被執行後,我要殺死他們。因爲否則,如果我一次點擊第1列,那麼我所有的表格都是可編輯的(不僅是第1列)。 我試過table.unbind();table.die(),但這並沒有奏效。


我在代碼的最後測試:

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

但它沒有工作

+3

你有沒有讀過https://github.com/ejbeaty/CellEdit? 'table.MakeCellsEditable(「摧毀」);' –

+0

哦,還沒有看到這個! – Jarla

+0

但是我測試過'table.MakeCellsEditable(「destroy」);'不幸的是它沒有工作 – Jarla

回答

1

要回答在標題的問題:是:

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

thiswilljustworkonce(); 
thiswilljustworkonce(); 
1

使用CellEdit插件的columns選項來指定哪些列需要可編輯。將不需要刪除事件處理程序。

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 
}); 

查看this example的代碼和演示。