如何使用带有KnockoutJS的JQuery DataTables添加事件单击

时间:2019-01-16 08:37:07

标签: knockout.js datatables

我找到了有关带有datatablejs的淘汰赛的演示。但是现在我想为表中的每一行添加事件点击。怎么办?

 this.selected = function(item){
    console.log(item);
  };

Here demo

1 个答案:

答案 0 :(得分:0)

您可以在处理程序的每个行级别上添加事件委托。将“选定的”方法引用作为参数传递给处理程序,并在委托时调用它。

ko.bindingHandlers.dataTable = {
  init: function(element, valueAccessor, allBindingsAccessor) {
    var value = valueAccessor(),
      rows = ko.toJS(value);

    allBindings = ko.utils.unwrapObservable(allBindingsAccessor()),
      options = allBindings.dataTableOptions || {},
      $element = $(element);
    $($element).children( "tbody").on( 'click', 'tr', function () {
    		allBindings.selectCallback(this)
        if ( $(this).hasClass('selected') ) {
            $(this).removeClass('selected');
        }
    } );
    
    $element.dataTable(options);

    ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
      $element.dataTable().fnDestroy();
    });


  },
  update: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
    var value = valueAccessor(),
      rows = ko.toJS(value);
    console.log(rows);
    $(element).find("tbody tr").remove();
    var table = $(element).DataTable();
    table.clear().draw();
    $.each(rows, function(index, row) {
      var myArray = [];
      $.each(row, function(key, value) {
        myArray.push(value)
      });
      table.row.add(myArray).draw().node()
    });
  }
}



function employee(id, firstName, lastName, phone, dept) {
  var self = this;
  this.id = ko.observable(id);
  this.firstName = ko.observable(firstName);
  this.lastName = ko.observable(lastName);
  this.phone = ko.observable(phone);
  this.dept = ko.observable(dept);
}

function model() {
  var self = this;
  this.employees = ko.observableArray('')
  this.id = ko.observable('');
  this.firstName = ko.observable('');
  this.lastName = ko.observable('');
  this.phone = ko.observable('');
  this.dept = ko.observable('');
  
 
  this.selected = function(item){
  	console.log(item);
  };
	  
  this.add = function() {
    self.employees.push(new employee(
      this.id(), this.firstName(), this.lastName(), this.phone(), this.dept()
    ));
    // console.log(ko.toJSON(self.employees))
  }
}

var mymodel = new model();

$(document).ready(function() {
  ko.applyBindings(mymodel);
  mymodel.employees.push(new employee('1', 'Joe', 'Smith', '333-657-4366', 'IT'));
  
  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js"></script>
 <link rel="stylesheet" type="text/css" href="http://cdn.datatables.net/1.10.4/css/jquery.dataTables.min.css">

<table data-bind="dataTable: employees, selectCallback:selected">
  <thead>
    <tr>
      <th>Id</th>
      <th>First</th>
      <th>Last</th>
      <th>Phone</th>
      <th>Dept</th>
    </tr>
  </thead>

  <tbody>
  </tbody>
</table>

<p style="padding-top: 20px;">
  Id:
  <input data-bind="textInput: id" />
</p>
<p>
  First:
  <input data-bind="textInput: firstName" />
</p>
<p>
  Last:
  <input data-bind="textInput: lastName" />
</p>
<p>
  phone:
  <input data-bind="textInput: phone" />
</p>
<p>
  dept:
  <input data-bind="textInput: dept" />
</p>
<p>
  <input type="button" value="add employee" data-bind="click: add" />
</p>