OL Learn

Current, previous and next record values from table

I’m trying to collect the current, previous and next record values from table in my template.

I’m able to get the next value, but can’t get the past value and having difficulties getting the last record.

results.each(function(index) {
	var field, nfield, pfield, result = "";
	var nindex, pindex, tlength;
	tlength = record.tables["DetailLine"].length;
	
	if (index == 0){
	    pindex = index;
	} else {
	    pindex = index -1;
	}
	
	if (index == tlength){
	    nindex = index;
	} else {
	    nindex = index +1;
	}
	
	field = record.tables["DetailLine"][index].fields["VPN"];
	
	if (nindex != index){
	    nfield = record.tables["DetailLine"][nindex].fields["VPN"];
	} else {
	    nfield = "NULL";
	}
	if (pindex != index){
	    pfield = record.tables["DetailLine"][pindex].fields["VPN"];
	} else {
	    pfield = "NULL";
	}
	
	
	result = "Current Value: " + field + " Next Value: " + nfield + " Previous Value: " + pfield;
	this.html(result);
});

I can loop through an entire table, but index-1 does not seen to be supported.

results.each(function(index){
	var myTable = record.tables.DetailLine;
	var result = "";	
	if (myTable){ 
		for(i=0; i < myTable.length; i++){
			result += i + ":=" + myTable[i].fields.VPN + ", ";
		}
	}
    this.html(result);
});

Not quite sure I understand what you’re trying to achieve, but the following piece of code allows me to display the Previous + Current + Next elements on each line of the detail table:

results.each(function(index) {
	var result = "";
	var myTable = record.tables.DetailLine;
	if (myTable){ 
		let prev="",curr="",next="";
		if(index>0){
			prev = myTable[index-1].fields.VPN;
		}
		if(index<(myTable.length-1)){
			next = myTable[index+1].fields.VPN;
		}
		curr = myTable[index].fields.VPN;

		result = '[' + prev + '] ' + curr + ' [' + next + ']';
	}
    this.html(result);
});

Hopefully, this will help put you on the right track.