You are viewing limited content. For full access, please sign in.

Question

Question

Forms - Get row number in a table

asked on January 21, 2021

Hi

I thought that this will be a simple step but cannot seem to get a result. I’m simply trying to get the row number of in a table. The table data populate from a lookup statement. The row number was going to be used for comparing text (at a later stage).

I have tried so many variation of jquery code but cannot seem to get the row number.

The table (class name “scit”) has one column consisting of text (class name “id1”). At this stage the click is only set on the text field for testing but will move it later.

As per the attached alerts, the td id and row count do return correct values but not the actual row no.

Thank you for the assistance,

John

 

 

 

1 0

Answer

SELECTED ANSWER
replied on January 25, 2021

Hi Jason

Thank you for the post. This is perfect!

 

0 0

Replies

replied on January 21, 2021

Hi John

If you add another field in your table (which you can hide), I typically name this field ROWID, and in that field you put the formula =ROW(), this will give you the Row Number in the table.

I have then been able to use this ROW Number in a number of calculations to target data in a table as it's just another field

Hope this helps

 

2 0
replied on January 21, 2021

Hi Steve 

Thank you. That helps, that is an easier way to add row numbers in future. Can I ask you another question.

How do I call the row number value and use it as a variable. I'm not sure what am I doing wrong here. When I click on row two, I expect a variable of 2 but get 1. It would seem it take the value from the first row. The ROWID column class is "rowid".

I really appreciate the assistance.

 

0 0
replied on January 21, 2021

Hi John, so what are you trying to get to as the end state? I always look to see if I can do this with formulas before resorting to JS.

0 0
replied on January 21, 2021

Hi Steve

The most important function that I wanted to achieve was moving a row either up or down.

The table is basically a list of tasks that the user must perform. The list will be partly sorted by the stored procedure but will not be 100% accurate and the user can manually add tasks. The sequence of the tasks is important. 

The row number is therefore important as a starting point. I initially thought that this will be an easy and I can use "var row = $(this).closest('tr');" but didn't work either. 

Hope this make sense.

Thanks

 

 

 

0 0
replied on January 21, 2021

hmmm, I don't think so easy. There have been a number of request for this feature and I've not seen any code from anyone else either

0 0
replied on January 21, 2021

Thanks for the advise!

0 0
replied on January 25, 2021 Show version history

Hello,

Here's what I did to allow users to reorder table rows on a recent form. The table in this example only has one field, but it wouldn't take much to modify this.

JavaScript

var upIcon = '🠉';
var downIcon = '🠋';

$(document).ready(function(){
  if(isIE()){
    upIcon = '˄';
    downIcon = '˅';
  }

  $('.questionTable tbody tr .col0 a').each(function(){
    questionButtons($(this));
  });
  
  eventHandlers();
});

function reorder(row, dest){
  var src = row.find(':input');
  var a = src.val();
  var b = dest.val();
  
  src.val(b).change();
  dest.val(a).change();
  
  if(src.val() != ''){
    src.blur();
  }
  
  if(dest.val() != ''){
    dest.blur();
  }
}

function questionButtons(target){
  // add up/down arrows for each row
  target.before('<div class="moveBtn"><div><span title="Move Up" class="moveUp">' + upIcon + '</span></div><div><span title="Move Down" class="moveDown">' + downIcon + '</span></div></div>');
}

function eventHandlers(){
  //  add up/down arrows for new row
  $('.questionTable .cf-table-add-row').on('click',function(){
    var target = $('.questionTable tbody tr:last-of-type .col0 a');
    questionButtons(target);
  });
  
  // move up event
  $('.questionTable').on('click','.moveUp',function(){
    var row = $(this).parents('tr');
    var prev = row.prev('tr').find(':input');
    
    reorder(row, prev);
  });
  
  // move down event
  $('.questionTable').on('click','.moveDown',function(){
    var row = $(this).parents('tr');
    var next = row.next('tr').find(':input');
    
    reorder(row, next);
  });
}

function isIE() {
    var ua = window.navigator.userAgent; //Check the userAgent property of the window.navigator object
    var msie = ua.indexOf('MSIE '); // IE 10 or older
    var trident = ua.indexOf('Trident/'); //IE 11

    return (msie > 0 || trident > 0);
}

I also included some CSS to position the buttons as well as ensure the up arrow is not visible on the first row and the down arrow is not visible on the last:

.moveBtn {
  float: right;
  margin-right: 4px;
}

.moveBtn > div {
  height: 18px;
}

.moveBtn > div > span {
  color: blue;
  cursor: pointer;
}

.questionTable tbody tr:first-of-type .moveUp,
.questionTable tbody tr:last-of-type .moveDown {
  visibility: hidden;
}

 

1 0
SELECTED ANSWER
replied on January 25, 2021

Hi Jason

Thank you for the post. This is perfect!

 

0 0
replied on January 25, 2021

Thanks Jason, taking it to the next level as always

@SteveKnowlton

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.