我已经设法解决了这个问题。javascript 函数被添加到模板中,但不是用于创建 DOM 元素,就像我最初开始的那样,而是作为重新排序列表项元素。
下面是一个适合我的模板:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function sortByWorkItemType(arr, isReverse = false) {
var worItemType = ["epi", "fea","pro", "tas", "bug"];
const directionFactor = isReverse ? -1 : 1;
const comparator = (a, b) => {
if (!a && !b) return 0;
if (!a && b) return -1 * directionFactor;
if (a && !b) return 1 * directionFactor;
const comparableA = a.innerText.toLowerCase().substring(0, 3);
const comparableB = b.innerText.toLowerCase().substring(0, 3);
const comparisonResult = worItemType.indexOf(comparableA) - worItemType.indexOf(comparableB);
return comparisonResult * directionFactor;
};
const safeCopyMonthNames = [...arr.children];
safeCopyMonthNames.sort(comparator);
return safeCopyMonthNames;
}
function sortList() {
var list, i, switching, b, shouldSwitch;
list = document.getElementById("workItemList");
switching = true;
while (switching) {
switching = false;
b = list.getElementsByTagName("LI");
for (i = 0; i < (b.length - 1); i++) {
shouldSwitch = false;
if (b[i].innerHTML.toLowerCase() > b[i + 1].innerHTML.toLowerCase()) {
shouldSwitch = true;
break;
}
}
if (shouldSwitch) {
b[i].parentNode.insertBefore(b[i + 1], b[i]);
switching = true;
}
}
var result = sortByWorkItemType(list);
document.getElementById("workItemList").innerHTML = "";
for(i = 0; i < result.length; i++) {
document.getElementById("workItemList").insertAdjacentHTML("beforeend", result[i].outerHTML.valueOf());
}
}
</script>
</head>
<body onload="sortList()">
<h1>Release notes for build $defname</h1>
@@BUILDLOOP@@
<h3>$($build.definition.name) </h3>
<b>Build number</b> : $($build.buildnumber) <br />
<b>Build started</b> : $("{0:dd/MM/yy HH:mm:ss}" -f [datetime]$build.startTime) <br />
<b>Source branch</b> $($build.sourceBranch)
<h3>Associated work items </h3>
<ul id="workItemList">
@@WILOOP@@
<li> <b>$($widetail.fields.'System.WorkItemType') $($widetail.id)</b> [Assigned by: $($widetail.fields.'System.AssignedTo'.'displayName')] $($widetail.fields.'System.Title') <br /> $($widetail.fields.'System.Description') </li>
@@WILOOP@@
</ul>
@@BUILDLOOP@@
</body>
</html>