以下函数从表中获取一些变量并将它们放在一个数组中:
var data = [];
function getInfo() {
var tds = null;
$('tr.dirRow').each(function () {
// Get child <td>s of each .dirRow class <tr>s
var tds = $(this).children();
// Get/sanitize information for new table
var fullName = tds.eq(1).text();
var org = tds.eq(4).text(); //other variables...
// Push data
if (fullName) {
data.push({
"id": fullName,
"org": org //other variables...
});
}
});
}
我有一个创建组织结构图的功能:
function createOrgChart() {
var peopleElement = document.getElementById("people");
var orgChart = new getOrgChart(peopleElement, {
theme: "sara",
dataSource: document.getElementById("orgChartData"),
customize: {
//***Need help getting this information in this spot***
// "id": { color: "green" }, <--this is what the data should be formatted like
}
});
}
我可以根据下面的for()
循环正确地在上面评论的部分中调用我想要的信息.log但是我无法弄清楚如何输出到“自定义”部分,它需要的格式。
在上面函数的“customize:{”部分正确记录我想要的输出的代码:
for (var i = 0; i < data.length; i++) {
if (data[i].org.substring(0, 4) == "Dep1") {
console.log("\"" + data[i].id + "\": { color: \"green\"},");
//results in:
//"id1": { color: "green" },
} else if (data[i].org.substring(0, 4) == "Dep2") {
console.log("\"" + data[i].id + "\": { color: \"red\"},");
//results in:
//"id2": { color: "red" },
}
}
数据数组,请参阅注释:
[Object { id="First Last", parentID="Supervisor Name", org="Dep1", more...}, Object { id="First2 Last2", parentID="Supervisor2", org="Dep2", more...}, //more objects
最终代码对我有用,如果它可以帮助其他人:
var data = [];
var customize = {};
var orgColors = {
'Dep1': { color: "blue" },
'Dep2': { color: "green" },
'Dep3': { color: "grey" }
}
$(document).ready(function () {
getInfo();
createOrgChart();
});
function getInfo() {
var tds = null;
$('tr.dirRow').each(function () {
// Get child <td>s of each .dirRow class <tr>s
var tds = $(this).children();
// Get/sanitize information for new table
var fullName = tds.eq(1).text();
var org = tds.eq(4).text(); //other variables...
// Push data
if (fullName) {
data.push({
"id": fullName,
"org": org //other variables...
});
}
customize[fullName] = orgColors[org.substring(0, 4)];
});
}
function createOrgChart() {
var peopleElement = document.getElementById("people");
var orgChart = new getOrgChart(peopleElement, {
theme: "sara",
dataSource: document.getElementById("orgChartData"),
customize: customize
});
}
答案 0 :(得分:4)
很难准确说出你想要的东西,但是你可能会尝试这样的事情:
var orgFormats = {
'Dep1': { color: "green"},
'Dep2': { color: "red"}
}
// then later, when you know what personData is
var customize = {};
customize[personData.id] = orgFormats[personData.org.substring(0, 4)];
var configObject = {
theme: 'sara',
dataSource: document.getElementById("orgChartData"),
customize: customize
};