2009-07-22 15 views
22

jQuery का उपयोग करके, मैं नीचे दी गई उदाहरण तालिका में मनमानी तालिका कक्ष का कॉलम इंडेक्स कैसे पा सकता हूं, जैसे कि एकाधिक कॉलमों में फैले सेल कई इंडेक्स हैं?तालिका का उपयोग करते हुए कॉलम इंडेक्स ढूँढना जब तालिका में कॉलम-स्पैनिंग सेल होते हैं

var index = 0; 
cell.parent('tr').children().each( 
    function(idx,node) { 
      if ($(node).attr('colspan')) { 
      index+=parseInt($(node).attr('colspan'),10); 
      } else { 
       index++; 
      } 

     return !(node === cell[0]); 
    } 
); 
console.log(index); 

यह शायद मतलब था एक प्लगइन के रूप में या विस्तार के माध्यम से यह करने के लिए:

एचटीएमएल

<table> 
    <tbody> 
    <tr> 
     <td>One</td> 
     <td>Two</td> 
     <td id="example1">Three</td> 
     <td>Four</td> 
     <td>Five</td> 
     <td>Six</td> 
    </tr> 
    <tr> 
     <td colspan="2">One</td> 
     <td colspan="2">Two</td> 
     <td colspan="2" id="example2">Three</td> 
    </tr> 
    <tr> 
     <td>One</td> 
     <td>Two</td> 
     <td>Three</td> 
     <td>Four</td> 
     <td>Five</td> 
     <td>Six</td> 
    </tr> 
    </tbody> 
</table> 

jQuery

var cell = $("#example1"); 
var example1ColIndex = cell.parent("tr").children().index(cell); 
// == 2. This is fine. 

cell = $("#example2"); 
var example2ColumnIndex = cell.parent("tr").children().index(cell); 
// == 2. It should be 4 (or 5, but I only need the lowest). How can I do this? 
+1

मुझे लगता है कि आपको अपने आप कुछ लिखना होगा (जैसे सेठ नीचे किया गया था)। हालांकि, पंक्तियों से सावधान रहें, जो आपकी गणना को और अधिक कठिन बना देगा। – aquinas

+3

@Aquinas - rowspans ... ewww। – seth

+0

इस कोड ने मेरी खोज में मेरी मदद की! – mjbates7

उत्तर

29

यहां एक प्लगइन है जो 'noncolspan' अनुक्रमणिका की गणना कर सकता है।

$(document).ready(
     function() 
     { 
     console.log($('#example2').getNonColSpanIndex()); //logs 4 
     console.log($('#example1').getNonColSpanIndex()); //logs 2 
    } 

); 

$.fn.getNonColSpanIndex = function() { 
    if(! $(this).is('td') && ! $(this).is('th')) 
     return -1; 

    var allCells = this.parent('tr').children(); 
    var normalIndex = allCells.index(this); 
    var nonColSpanIndex = 0; 

    allCells.each(
     function(i, item) 
     { 
      if(i == normalIndex) 
       return false; 

      var colspan = $(this).attr('colspan'); 
      colspan = colspan ? parseInt(colspan) : 1; 
      nonColSpanIndex += colspan; 
     } 
    ); 

    return nonColSpanIndex; 
}; 
+1

फ़ंक्शन में पहली पंक्ति को बदलने के लिए (तालिका शीर्षलेखों का समर्थन करने के लिए) अगर (! $ (यह) .is ('td') &&! $ (This) .is ('th')) –

+1

अच्छा विचार आंद्रे । कोड अपडेट किया गया। – SolutionYogi

+0

@SolutionYogi, मुझे लगता है कि आपका अपडेट कोड है (! $ (यह) .is ('td') ||! $ (यह) .is ('th')) के बजाय अगर (! $ (यह) .is ('td') &&! $ (यह) .is ('th')) –

2

आप कुछ इस तरह कर सकता है।

5

मेरा समाधान प्लगइन के निर्माण से कम, समाधान समाधान के समान है। यह मुझे थोड़ा समय लगा ... लेकिन मैं अभी भी इसलिए यहाँ यह पर गर्व है यह है :)

cell = $("#example2"); 
var example2ColumnIndex2 = 0; 

cell.parent("tr").children().each(function() { 
    if(cell.get(0) != this){ 
     var colIncrementor = $(this).attr("colspan"); 
     colIncrementor = colIncrementor ? colIncrementor : 1; 
     example2ColumnIndex2 += parseInt(colIncrementor); 
    } 
}); 
console.log(example2ColumnIndex2); 
0

थोड़ा संशोधित संस्करण यहाँ है: http://jsfiddle.net/Lijo/uGKHB/13/

//INDEX 
alert (GetNonColSpanIndex ('Type')); 

function GetNonColSpanIndex(referenceHeaderCellValue) { 

    var selectedCell = $("th").filter(function (i) { 
     return ($.trim($(this).html() )) == referenceHeaderCellValue; 

    }); 

    alert(selectedCell.html()); 

    var allCells = $(selectedCell).parent('tr').children(); 
    var normalIndex = allCells.index($(selectedCell)); 
    var nonColSpanIndex = 0; 

    allCells.each(
    function (i, item) { 
     if (i == normalIndex) 
      return false; 

     var colspan = $(selectedCell).attr('colspan'); 
     colspan = colspan ? parseInt(colspan) : 1; 
     nonColSpanIndex += colspan; 
    } 
    ); 

    return nonColSpanIndex; 
}; 

संबंधित मुद्दे