Skip to content

Instantly share code, notes, and snippets.

@fmpwizard
Created March 17, 2011 03:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmpwizard/873797 to your computer and use it in GitHub Desktop.
Save fmpwizard/873797 to your computer and use it in GitHub Desktop.
Case Class to replace html and if it fails, append some NodeSeq to some other ID
case class ReplaceOrAppend(replacementId: String, replacement: NodeSeq,
appendingId: String, appending: NodeSeq) extends JsCmd with HtmlFixer {
override val toJsCmd = {
val (replacementHtml, replacementJs) = fixHtmlAndJs("inline", replacement)
val (appendHtml, appendJs) = fixHtmlAndJs("inline", appending)
var ret =
"""
try {
var parent1 = document.getElementById(""" + replacementId.encJs + """);
parent1.innerHTML = """ + replacementHtml + """;
for (var i = 0; i < parent1.childNodes.length; i++) {
var node = parent1.childNodes[i];
parent1.parentNode.insertBefore(node.cloneNode(true), parent1);
}
parent1.parentNode.removeChild(parent1);
} catch (e) {
$('#""" + appendingId + """').append(""" + appendHtml + """);
}
"""
ret
}
}
Usage:
ReplaceOrAppend(
"id1",// the id to call Replace
<td id="id1" class={cssClass}>{cellNotes}</td>, //the NodeSeq that will replace the existing id2
"result-grid", //the id of the element we will append a NodeSeq to
<tr>
<td id={rowName}>{rowName}</td>
<td id={((ind + 0) + rowName)} class="notice"><span>N/A</span></td>
<td id={((ind + 1) + rowName)} class="notice"><span>N/A</span></td>
<td id={((ind + 2) + rowName)} class="notice"><span>N/A</span></td>
<td id={((ind + 3) + rowName)} class="notice"><span>N/A</span></td>
</tr> // The NodeSeq to append
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment