function generateReport() { try { var getUrl = window.location; var baseUrl = getUrl.protocol + "//" + getUrl.host + "/"; var url = baseUrl + 'polarion/ImpactAnalysisTree/ImpactAnalysisTree?projectId=' + projectId; var parameters = ""; var Id = false; var title = false; var workItemId = document.forms["tracegraph"]["itemId"]; if (workItemId != null) { if (!workItemId.value.trim().length == 0) { parameters = '&op=search&workItemId=' + workItemId.value; Id = true; } } if (!Id) { $(".widget-errors").text("Enter a valid Work Item ID /Title to generate traceability report.").show(); return false; } setTimeout(function() { var jsonTree = [{"name": "DAIS-2183","title": "系统架构测试使用","image": "/polarion/icons/default/enums/type_purple_feature.png","url": "#/project/DAISCHproject/workitem?id=DAIS-2183","nodeDispText": "DAIS-2183 : 系统架构测试使用","nodeDispType": "7","parent": "null","backlinked": [{"name": "DAIS-2332","title": "集成","image": "/polarion/icons/default/enums/type_purple_testcase.png","url": "#/project/DAISCHproject/workitem?id=DAIS-2332","nodeDispText": "DAIS-2332 : 集成","nodeDispType": "7","parent": "DAIS-2183","linkcolor": "#ccc","backlinked": [{"name": "DAIS-2333","title": "任务","image": "/polarion/icons/default/enums/type_task.gif","url": "#/project/DAISCHproject/workitem?id=DAIS-2333","nodeDispText": "DAIS-2333 : 任务","nodeDispType": "7","parent": "DAIS-2332","linkcolor": "#ccc","backlinked": []}]}],"linked": [{"name": "DAIS-2320","title": "系统需求","image": "/polarion/icons/default/enums/type_purple_spec.png","url": "#/project/DAISCHproject/workitem?id=DAIS-2320","nodeDispText": "DAIS-2320 : 系统需求","nodeDispType": "7","parent": "DAIS-2183","linkcolor": "#ccc","linked": []},]}]; root = jsonTree[0]; root.x0 = height / 2; root.y0 = width; console.log(root); var t1 = d3.layout.tree().nodeSize([30, 10]) .separation(function separation(a, b) { return a.parent == b.parent ? 1.2 : 1.4; }) .children(function(d) { return d.linked; }); var t2 = d3.layout.tree().nodeSize([30, 10]) .separation(function separation(a, b) { return a.parent == b.parent ? 1.2 : 1.2; }).children(function(d) { return d.backlinked; }); t1.nodes(root); t2.nodes(root); var rebuildChildren = function(node) { node.children = getChildren(node); if (node.children) node.children.forEach(rebuildChildren); } rebuildChildren(root); root.isRight = false; update(root); }); $(location).attr('href', url+parameters); } catch (e) { console.log(e); alert(e); } return false; } 使用ajax重新渲染页面
时间: 2023-07-22 08:12:03 浏览: 150
要使用Ajax重新渲染页面,你可以在`generateReport()`函数中添加以下代码:
```javascript
$.ajax({
url: url + parameters,
type: 'GET',
success: function(response) {
// 在这里处理从服务器返回的响应数据
// 可以更新页面元素或执行其他操作
},
error: function(xhr, status, error) {
// 在这里处理错误情况
console.log(error);
alert('An error occurred: ' + error);
}
});
```
请确保在使用Ajax之前引入jQuery库。在成功回调函数中,你可以根据从服务器返回的响应数据更新页面元素或执行其他操作。在错误回调函数中,可以处理错误情况并提供适当的错误消息。
注意:在使用Ajax重新渲染页面之前,你可能需要在服务器端提供相应的接口或处理程序来处理请求并返回所需的数据。
相关问题
function generateReport() { try{ var getUrl = window.location; var baseUrl = getUrl.protocol + "//" + getUrl.host + "/"; var url = baseUrl + 'polarion/ImpactAnalysisTree/ImpactAnalysisTree?projectId='+projectId; var parameters = ""; var Id =false; var title = false; var workItemId = document.forms["tracegraph"]["itemId"]; if(workItemId != null ){ if(!workItemId.value.trim().length == 0 ){ parameters = '&op=search&workItemId=' + workItemId.value; Id = true; } } if( !Id){ $(".widget-errors").text("Enter a valid Work Item ID /Title to generate traceability report.").show(); return false; } // Post $(location).attr('href', url+parameters) } catch (e){ console.log(e); alert(e); } return false; } 获取代码块的返回值并重新渲染页面
这段代码的目的是生成一个报告,并且在生成报告之后重新渲染页面。然而,根据代码的逻辑,这段代码并没有直接返回任何值,并且重新渲染页面的操作是通过改变 `$(location).attr('href', url+parameters)` 来实现的。
所以,如果你想要获取代码块的返回值并重新渲染页面,你需要对该代码进行一些修改。一种可能的方法是使用 AJAX 异步请求来执行生成报告的操作,并在成功返回后重新渲染页面。
以下是一个示例代码,展示了如何使用 AJAX 异步请求来获取生成报告的返回值并重新渲染页面:
```javascript
function generateReport() {
try {
var getUrl = window.location;
var baseUrl = getUrl.protocol + "//" + getUrl.host + "/";
var url = baseUrl + 'polarion/ImpactAnalysisTree/ImpactAnalysisTree?projectId=' + projectId;
var parameters = "";
var Id = false;
var title = false;
var workItemId = document.forms["tracegraph"]["itemId"];
if (workItemId != null) {
if (!workItemId.value.trim().length == 0) {
parameters = '&op=search&workItemId=' + workItemId.value;
Id = true;
}
}
if (!Id) {
$(".widget-errors").text("Enter a valid Work Item ID /Title to generate traceability report.").show();
return false;
}
// AJAX 异步请求
$.ajax({
url: url + parameters,
method: 'POST',
success: function(response) {
// 在这里处理生成报告的返回值
console.log(response);
// 重新渲染页面的操作
// ...
// 示例:重新加载页面
location.reload();
},
error: function(error) {
console.log(error);
alert(error);
}
});
} catch (e) {
console.log(e);
alert(e);
}
return false;
}
```
请注意,以上示例代码仅供参考,并且还需要根据你的具体需求进行修改和调整。
function generateReport() { try { var getUrl = window.location; var baseUrl = getUrl.protocol + "//" + getUrl.host + "/"; var url = baseUrl + 'polarion/ImpactAnalysisTree/ImpactAnalysisTree?projectId=' + projectId; var parameters = ""; var Id = false; var title = false; var workItemId = document.forms["tracegraph"]["itemId"]; if (workItemId != null) { if (!workItemId.value.trim().length == 0) { parameters = '&op=search&workItemId=' + workItemId.value; Id = true; } } if (!Id) { $(".widget-errors").text("Enter a valid Work Item ID /Title to generate traceability report.").show(); return false; } // AJAX 异步请求 $.ajax({ url: url + parameters, method: 'POST', success: function(response) { Data = JSON.parse(response); }, error: function(error) { console.log(error); alert(error); } }); } catch (e) { console.log(e); alert(e); } return false; } 根据返回的值重新渲染下面代码 <FORM id="tracegraph"> <br /> <form name="tracegraph"> <div id="inputDiv"> <table> <tr> <td valign="center" align="right">Work Item ID </td> <td> <div class="autocomplete"> <input id="itemId" type="text" name="itemId" autocomplete="off" placeholder="Work Item ID" style="width: 225px;"> </div> </td> </tr> </table> </div> </form> <table> <tr> <td> <button onclick="generateReport()" class="reportbutton" style="margin-left: 18px;" VALIGN=MIDDLE ALIGN=CENTER> Display Report</button> </td> </tr> </table> <br> <div class="widget-errors"></div> <div id="mainSVG"></div>
根据返回的值重新渲染下面代码:
```html
<FORM id="tracegraph">
<br />
<form name="tracegraph">
<div id="inputDiv">
<table>
<tr>
<td valign="center" align="right">Work Item ID </td>
<td>
<div class="autocomplete">
<input id="itemId" type="text" name="itemId" autocomplete="off" placeholder="Work Item ID" style="width: 225px;">
</div>
</td>
</tr>
</table>
</div>
</form>
<table>
<tr>
<td>
<button onclick="generateReport()" class="reportbutton" style="margin-left: 18px;" VALIGN=MIDDLE ALIGN=CENTER>Display Report</button>
</td>
</tr>
</table>
<br>
<div class="widget-errors"></div>
<div id="mainSVG"></div>
</FORM>
```
阅读全文