2017-12-25 150 views
6

Tôi đang sử dụng handsontable plugin js. Tôi muốn sử dụng chức năng getCellMeta trong afterChange móc nhưng không hoạt động. Tôi khi sử dụng chức năng out afterChange hook, chức năng đang hoạt động. Nhưng không làm việc trong hook afterChange.Làm thế nào để sử dụng getCellMeta trong afterChange tại Handsontable?

var container = document.getElementById('t1'), 
    options = document.querySelectorAll('.options input'), 
    table, 
    hot; 

hot = new Handsontable(container, {  
    autoWrapRow: true, 
    startRows: 81, 
    startCols: 206, 
    autoColumnSize : true, 
    stretchH: 'all', 
    afterChange : function(change,source) { 
     if (source === 'loadData') { 
     return; 
     } 
     var test = this.getCellMeta(change[0],change[1]); // not working, not return "id" meta 
     console.log(test); 
    } 
}); 

$.ajax({ 
    url: 'path', 
    type: 'GET', 
    dataType: 'json', 
    success: function (res) { 
    var data = [], row, pc = 0; 
    for (var i = 0, ilen = hot.countRows(); i < ilen; i++) 
    { 
     row = []; 
     for (var ii = 0; ii<hot.countCols(); ii++) 
     { 
     hot.setCellMeta(i,ii,'id',res[pc].id); 
     row[ii] = res[pc].price; 
     if(pc < (res.length-1)) { 

     pc++; 
     } 
     } 
     data[i] = row; 
    } 
    hot.loadData(data); 
    } 
}); 

var test = this.getCellMeta(0,0); // is working, return "id" meta 
console.log(test); 

Nhật ký bảng điều khiển đầu ra tôi đã thử sau khiThay đổi; enter image description here

Sử dụng nhật ký bảng điều khiển đầu ra sau afterChange; enter image description here

Cách nhận siêu dữ liệu sau khi thay đổi?

Cảm ơn.

+0

Tôi không quen với plugin này nhưng tài liệu chỉ hiển thị trướcGetCellMeta và afterGetCellMeta; getCellMeta đến từ đâu? – kshikama

+0

[liên kết] (https://image.prntscr.com/image/pSN__mN-RpCONgy_320krA.png) @kshikama –

Trả lời

3

Bạn sắp thực hiện, đó chỉ là một sai lầm nhỏ trong callback của bạn: doc for afterChange xác định rằng đối số đầu tiên (changes) của callback là:

một mảng 2D chứa thông tin về mỗi tế bào thay đổi nội dung [[row, prop, oldVal, newVal], ...].

Vì vậy, để lấy "meta" của hàng/col ô bị ảnh hưởng (giả sử chỉ có một ô), bạn cần gọi số this.getCellMeta(change[0][0],change[0][1]) chẳng hạn.

Ví dụ mà đọc toàn bộ mảng thay đổi:

var hot = new Handsontable(container, { 
    /* rest of init... */ 
    afterChange : function(changes,source) { 
     console.log("Changes:", changes, source); 
     if (changes) { 
      changes.forEach(function(change) { 
       var test = hot.getCellMeta(change[0],change[1]); 
       console.log(test.id, test); // 'id' is the property you've added earlier with setMeta   
      }); 
     } 
    } 
}); 

Xem demo fiddle, mở giao diện điều khiển JS, thực hiện bất kỳ thay đổi (s) trong bảng.

Các vấn đề liên quan