2014-12-19 5 views
2

다른 것과 비교하여 다른 값을 모두 강조하는 기능을 HTML 표에 추가하려고합니다. 행마다 비교가 이루어집니다.테이블 행의 항목 간 Hightlight 차이점

많은 노력을 기울여서 다음 JQuery/Javascrit 코드를 얻을 수있었습니다. 나는 이것이 이 아니라고 확신한다. 효율적/우아하고 빠른 방법이지만, 그것을 해결하는 유일한 방법이다.

HTML 표가 상당히 복잡하여 여기에 게시하기가 어렵습니다.

문제 나 발생하고있어 스크립트가 루프 밖으로 잘 작동한다는 것입니다,하지만 난에 대한 내부에 넣어 경우 은이 중단 - LOOP 나는 이유를 이해하지 않습니다.

var numRows = $('.ctable tbody tr').length, numCols = $('.ctable tbody tr:first th').length, v, undefined; 
var values = new Array(numRows); 
var noDuplicates = new Array(numCols); 
var result = new Array(numCols); 


for (i = 1; i = numRows; i++) { 

    // Get a row and copy into an array the values of each VISIBLE cell 
    $(".ctable tbody tr:eq(" + i + ") td.values:visible").each(function(){ 
     v = $(this).text(); 
     values.push(v.trim()); 
    }); 

    // Remove from the array the 'undefined' values 
    values = values.filter(function(item){ 
     return item !== undefined; 
    }); 

    // Push into new array duplicate values 
    noDuplicates = return_duplicates(values); 

    // Compare the two arrays and get the differences (uses underscore.js) 
    result = _.difference(values, noDuplicates); 

    // This is a 'highlight' plugin and you may pass to it an array 
    $(".ctable tbody tr:eq(" + i + ") td.values:visible").highlight(values); 
} 

function return_duplicates(arr) { 
    var len=arr.length, out=[], counts={}; 
    for (var i=0;i<len;i++) { 
     var item = arr[i]; 
     counts[item] = counts[item] >= 1 ? counts[item] + 1 : 1; 
    } 
    for (var item in counts) { 
     if(counts[item] > 1) 
     out.push(item); 
    } 
    return out; 
} 
+0

의 여기 numRows의 값을 줄 수 있습니까? 루프 반복을 위해 js 디버거를 사용해 보셨습니까? – ObiWanKenobi

+0

for 루프를 사용하여 스크립트를 중단 할 수 있습니까? – Gnucki

+0

'return_dublicates' 메소드를 테스트 해 보았습니다. 나는 당신의 코드가 괜찮아 보인다. Fiddle : http://jsfiddle.net/jyrkim/Lbcprygt/ – jyrkim

답변

1

시도

for (i = 1; i < numRows; i++) { 

대신

for (i = 1; i = numRows; i++) {