Only creating detail row based on previous row

Hi,

I have data that contains a letter and I have captured the letter contents line by line as a detail table. All I want to do is recreate the lettewr in the template but I only want to have one empty line if the data has multiple empty lines. In my css I have:

#grid {
display: grid;
grid-auto-rows: auto;
grid-template-columns: 100%;
}

in my html I have

and I have a script with my selector as #grid. This is the code I’m using but it doesn’t do anything and I don’t know why.

var grid = query("#grid");
var detail = record.tables["detail"];
var last = 'first';

for (var i = 0; i < detail.length; i++) {
	var line = record.tables["detail"][i].fields["line"];
	if (line == "" || line == null) {
		line = "&nbsp;";
 	};
 	if (last == "" || last == null || last == "&nbsp;") {
 		logger.info('no div');
 	} else {
 		var div = '<div>';
 		div += line;
 		div += '</div>';
 		grid.add(div);
 	}
 	last = line;
}

I don’t know why this doesn’t work./ I’ve also tried results.add(div) but that doedn’t wortk euither.

can someone tell me what I’m doing wrong?

I’ve also tried appending all the divs to a single srring and doing a single .add after the loop aand that doesn’tr work either

I would add a variable to check if the previous line was empty. Something like this:

let html = '';
let detail = record.tables["details"];
let prev = true;

for (var i = 0; i < detail.length; i++) {
	let str = detail[i].fields.line;
	if ( ! str && prev === true)  {
	 	html += '<p>&nbsp;</p>';
	 	prev = false;
	} else if ( str ) {
		html += '<p>' + str + '</p>';
		prev = true;
	}
}

results.append( html );

thanks for the help. I chaanged my code to this:

var detail = record.tables["detail"];
let last = true;
let div = '';

for (var i = 0; i < detail.length; i++) {
	let line = detail[i].fields.line;
	if (line == "" || line == null) {
		line = "&nbsp;";
		last = false;
 	};
 	if (last === true) {
 		div += '<div>';
 		div += line;
 		div += '</div>';
 		last = true;
 	}
}
results.add(div);

but when I preview nothing is added to the div #grid and I can’t figure out what I’m doing wrong

ok I used append and that worked. what doesn’t work is its still creating empty divs multple times in a row. I’m using this:

var detail = record.tables["detail"];
let last = true;
let div = '';

for (var i = 0; i < detail.length; i++) {
	let line = detail[i].fields.line;
	if (! line && prev === true) {
		line = "&nbsp;";
		last = false;
 	};
 	if (last === true) {
 		div += '<div>';
 		div += line;
 		div += '</div>';
 		last = true;
 	}
}

results.append(div);

I got it. I forgot to trim in the ddatamapper./ this was my final code:

var detail = record.tables["detail"];
let last = true;
let div = '';

for (var i = 0; i < detail.length; i++) {
	let line = detail[i].fields.line;
	if (line == '' && last === true) {
		div += "<div>&nbsp;</div>";
		last = false;
 	} else {
 		div += '<div>';
 		div += line;
 		div += '</div>';
 		last = true;
 	}
}

results.append(div);

Guess you need to change the following

to

} else if ( line ) {