2016-04-28 31 views
0

當我的數據表中存在選定的複選框行時,如何啓用/禁用我的按鈕?如何啓用/禁用Datatables複選框選定行時的按鈕

var pctoreceive = $('#pcToReceive').DataTable({ 
      'columnDefs': [{ 
       'targets': 0, 
       'searchable': false, 
       'orderable': false, 
       'className': 'dt-body-center', 
       'render': function (data, type, full, meta) { 
        return '<input type="checkbox" name="id[]" value="' 
         + $('<div/>').text(data).html() + '">'; 
       } 
      }], 

我縮短了我的代碼。上面說我添加了一個新列checkbox節目選擇

enter image description here

在沒有選擇的行這兩個按鈕必須被禁止。否則啓用

$('#rc-select-all').on('click', function() { 
    // Check/uncheck all checkboxes in the table 
    var rows = pctoreceive.rows({ 
     'search': 'applied' 
    }).nodes(); 
    $('input[type="checkbox"]', rows).prop('checked', this.checked); 
}); 

// Handle click on checkbox to set state of "Select all" control 
$('#pcToReceive tbody').on('change', 'input[type="checkbox"]', function() { 
    // If checkbox is not checked 
    if (!this.checked) { 
     var el = $('#rc-select-all').get(0); 
     // If "Select all" control is checked and has 'indeterminate' property 
     if (el && el.checked && ('indeterminate' in el)) { 
      // Set visual state of "Select all" control 
      // as 'indeterminate' 
      el.indeterminate = true; 
     } 
    } 
}); 

回答

1

您的問題與datatable無關。

只需設置一個計數器變量來存儲檢查了多少輸入檢查,然後如果> 0啓用您的按鈕。

見這個小例子(fiddle

var counterChecked = 0; 

$('body').on('change', 'input[type="checkbox"]', function() { 

    this.checked ? counterChecked++ : counterChecked--; 
    counterChecked > 0 ? $('#submitButton').prop("disabled", false): $('#submitButton').prop("disabled", true); 

}); 
+0

爲什麼它的身體嗎?爲什麼不在特定的桌子上? – qwerzxcxyz

+1

只是因爲我沒有把任何表放在我的例子,但在你的代碼中,你應該使用特定的表格選擇器的c :) – pumpkinzzz

+0

我試了一下,它只適用於行..但不是在選擇/取消選擇複選框header( – qwerzxcxyz

相關問題