我们遍历JSON对象以使用预定义的标题项填充表数据。
(来自this former question的回答的代码)
var stories = {};
for (var i=0; i<QueryResults.Results.length; i++) {
var result = QueryResults.Results[i],
name = result.StoryName
if (!(name in stories))
stories[name] = {};
stories[name][result.Name] = result.State;
}
var keys = ['QA Cycle 1', 'QA Cycle 2', 'Development - GUI', 'Development - Backend'];
var data = "<table><tr><th>Story Name</th><th colspan='2'>Quality Assuance</th><th colspan='2'>Development</th></tr><tr><td> </td>";
for (var i=0; i<keys.length; i++)
data += "<th>"+keys[i]+"</th>";
data += "</tr>";
for (var storyname in stories) {
data += "<tr><th>"+storyname+"</th>";
for (var i=0; i<keys.length; i++)
data += "<td>"+(stories[storyname][keys[i]] || "n.a.")+"</td>";
data += "</tr>";
}
data += "</table>";
$('body').append(data);
截至目前,每项任务都有“定义”,“正在进行”,“完成”等状态。 “不适用。
现在,我想合并两个子任务“开发 - GUI”&amp; “开发 - 后端”分为一个主要任务“开发”,然后通过计算以下规则来插入累积状态:
通过执行此操作,将从表中删除第二行[即子任务],并为主任务列呈现累积结果。
JSFiddle:http://jsfiddle.net/ylokesh/a5HRu/4/
任何建议或帮助都会很棒。提前谢谢。
答案 0 :(得分:1)
只需改变一下:
var keys = ['QA Cycle 1', 'QA Cycle 2', 'Development'];
…
for (var storyname in stories) {
var story = stories[storyname];
// variables for shortening the code
var sub1 = story['Development - GUI'],
sub2 = story['Development - Backend'],
main = 'Development';
if (sub1 && sub2) { // neither is NA
if (sub1 == sub2)
story[main] = sub1;
else if (sub1=="Completed" && sub2=="Defined")
story[main] = "In-Progress";
else
story[main] = "unknown subtask state combination";
}
data += "<tr><th>"+storyname+"</th>";
for (var i=0; i<keys.length; i++)
data += "<td>"+(story[keys[i]] || "n.a.")+"</td>";
data += "</tr>";
}
…