0

I am working on a scheduling project and have run into an issue with finding the column index of a selected cell (so that i may then get the appropriate header for the column). The issue comes into play when a previous cell(or cells) have/has a rowspan. In which case the cell index is off by that amount of cells. I have been at this since yesterday. My current attempt involves using a solution I found in previous posts, and this is this:

parentTr.find('>td, >th').each(function(i,o) {
    if ( this == reference ) 
    {
    cellNum = i;

    var counter = columnNum;

    while ( counter-- ) 
    {
    $(this).closest('tr').next().find('>td,>th').each(function(i,o) 
    {
        if ( cellNum == i ) 
        {
            $(this).addClass('rowspan-affected');
            $(this).attr('colNum', columnNum);
         }
     });
    }
    }
});         
})

The problem is that this solution counts the number of rowspanned cells on the entire page. I need a count of the rowspanned cells for only the current clicked cell, and then be able to add that count to index so I can get the proper header. What I have been trying looks like this:

        var $this = $(this);
        //get the row header contents
        var row = $this.parent('tr').contents('th:eq(0)').html();
        //trying this
        var colCount = $(this).prevAll().find('td').parent('tr').attr('rowspan');
        alert (colCount);
        //used to get the cell index
        var rowIndex = $(this).parent().index('.main tbody tr');
        var tdIndex = $(this).index('.main tbody tr:eq('+rowIndex+') td');
        //alert ("tdindex " + (tdIndex+1));
        var headerObj = $(this).parents('.main').find('th').eq(tdIndex+1);
        //strip whitespace before passing
        var toPass =   $.trim(headerObj.text());
        //toPass = $.trim(toPass)
        //alert (toPass);

This information is gathered and then passed to a new form with the selected information used to populate the form. If anyone can help me with this, I would be greatly appreciative!!!

royjm
  • 107
  • 14
  • What about this solution: http://stackoverflow.com/questions/5981317/jquery-select-visual-column-in-table-with-rowspan – Nicolás Ozimica Aug 21 '13 at 19:07
  • I looked it over and I don't see how it could apply. The rowspans in my table are indeterminate and they may no exist at all. However, when they do exist they throw off the index count of the selected cell. Also, my tables are of indeterminate size as far as columns go. The could be 3,4, or 23 columns wide. They are created dynamically from a database that has up to 10 different scheduling departments. – royjm Aug 21 '13 at 19:26
  • 1
    What about this: http://stackoverflow.com/questions/13407348/table-cellindex-and-rowindex-with-colspan-rowspan – Inglis Baderson Aug 22 '13 at 01:57
  • @AgreeOrNot looking it over now, thanks. – royjm Aug 22 '13 at 02:07
  • @AgreeOrNot, I'm only at a basic level when it comes to javascript, so I am unsure of how to use or adapt plugins for my needs...I am a php and database guy who is trying to make this work. – royjm Aug 22 '13 at 02:15
  • 1
    Save the source code as a `.js` file and add `` after the jQuery one. Then you can do `var colIndex = $('td').cellPos().left`. – Inglis Baderson Aug 22 '13 at 02:33
  • AgreeOrNot Okay, did that and testing out the results now. This looks to be what I needed. I will need to give credit to @nrodic. I have a better understanding of how it works now that I've had a chance to test it. It is rather brilliant! – royjm Aug 22 '13 at 03:14

0 Answers0