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

Question

Question

Refer to index of collection field?

asked on October 25, 2014

I found from a website that the function following

$(this).closest("tr")[0].rowIndex;

It is a function for refer to index of each row in a table.

I wonder if I want to refer to index of a collection field,then what function I can use?

I try this:

$(this).closest("ul")[0].rowIndex;

But It's doesn't work.

I have to move value of each index of collection to an array by using corresponding index.

So please give me a suggest and thank you in advance.

 

0 0

Answer

SELECTED ANSWER
replied on October 30, 2014

Not sure what you have in mind to do with the indexing. If you only want to target a few specific rows for information, I would recommend just using jQuery selectors. For instance, if I wanted to reference a Single Line field with id=17 in the 3rd row of a collection with id=8:

$('#q8 .kx-repeatable > div:nth-child(3) #q17 input').val()

#q8 selects my collection, which contains a '.kx-repeatable' DIV that has a set of DIV tags nested directly below it, one for each row. The ':nth-child(N)' will select a specific one of these rows, with 1-based indexing. Anything you put after that will just reference the elements within that specific row of the collection, the same way you normally do for finding things on the entire form.

If you want to loop through the whole collection and do something to each row, but need to have an index to refer to, you can use the each() function. That would look like this:

$('#q8 .kx-repeatable > div').each( function (ind) {
  // Do Something to Each Row
  alert("Row: "+ind.toString()+" FieldValue: "+$(this).find('#q17 input').val());
});

If you go this route, just note that this index 'ind' will be 0-based.

3 0

Replies

replied on October 31, 2014

Hi Scott,

   Appriate your information.I 'm just a beginner of javaScript coding,so this sample is very helpful and show me the way to refer to collection which is hard to find information from the others website.

I will try your route.and thank you for your time.

Regards,

0 0
replied on December 17, 2014

Hi Scott,

I am actually writing code that utilizes a "field input" reference of a specific row in table consisting of multiple rows.  For instance, my code must reference "Field Input 1" of a row and decide if its length is > 0; if the length of "Field Input 1" is >0, then "Field Input 2" of the same row will be disabled.  This code works in the opposite manner where if the length of "Field Input 2" is >0, then "Field Input 1" is disabled. 

The code must not affect field inputs of the same name in different rows. For example, if "Field Input 1" of row 1 was disabled, "Field Input 1" of row 2 must not be disabled as well.

In the actual code, "Field Input 1" is called "debit" and "Field Input 2" is called "credit".  One significant issue of my code is that I am not referencing a specific row correctly. I am using a counter called "Clicks" to count the number of times the "add row"  selector is clicked such that a new row is added to the collection table.   Your code does not recognize the row index that is specified by my counter "Clicks", this is what you recommended  $('#q8 .kx-repeatable > div:nth-child(3) #q17 input').val()

$(document).ready(function () {
$('.cf-table-block').on('blur', 'input', sumtotal);
  
function sumtotal () {
  
var DEBIT = 0; var CREDIT = 0; var DB =0; var CR = 0; 
var totalDEBIT = 0; var totalCREDIT = 0; var Clicks = 0;
  
$('.cf-table-add-row').click(function () {
  
Clicks++;

if ($('#q8 .kx-repeatable > div:nth-child(Clicks) .debit input').val().legnth>0)
 {
$('#q8 .kx-repeatable > div:nth-child(Clicks) .credit input').val().attr("disabled", true);
 }
else if ($('#q8 .kx-repeatable > div:nth-child(Clicks) .credit input').val().length>0)
 {
$('#q8 .kx-repeatable > div:nth-child(Clicks) .debit input').val().attr("disabled", true)
 }
$('.cf-table-block tbody tr').each(function () {
  DB = parseNumber($(this).find('.debit input').val().toFixed(2));
  totalDEBIT = $('.totalDEBIT input').val(DB).toFixed(2); 
  
  CR = parseNumber($(this).find('.credit input').val().toFixed(2)); 
  totalCREDIT = $('.totalCREDIT input').val(CR).toFixed(2);
})
  $('.RAmount input').val( DB – CR).toFixed(2);
 });
}
function parseNumber(n) {
var f = parseFloat(n); //Convert to float number.
return isNaN(f) ? 0 : f; //treat invalid input as 0;
}
});

 

 

 

 

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

Sign in to reply to this post.