我有一个数组,其中包含依赖对象的名称。 问题是依赖对象也可以有依赖对象等等。 我的代码只有2级深度,我需要实现一种递归方式。 我的代码是:
if (obj.detailsObjects) {
_.forEach(obj.detailsObjects, function (o, key) {
if (window[o] instanceof QuadTable) {
//limpamos o details e reiniciamos a info
if ($.fn.DataTable.isDataTable('#' + window[o].tableId)) {
debugger;
window[o].tbl.clear();
window[o].tbl.columns.adjust().draw();
$('#' + window[o].tableId + "_info > .nRecords").text(window[o].tbl.data().count() + " " + window[o].i18nEntries.record);
if (window[o].editorXt) {
$('#' + window[o].tableId + '_xtForm')[0].reset();
}
}
//se o details for um master repetimos a operação para o details do details
//todo recursividade
if (window[o].detailsObjects) {
_.forEach(window[o].detailsObjects, function (ob, key) {
if (window[ob] instanceof QuadTable) {
if ($.fn.DataTable.isDataTable('#' + window[ob].tableId)) {
window[ob].tbl.clear();
window[ob].tbl.columns.adjust().draw();
$('#' + window[ob].tableId + "_info > .nRecords").text(window[ob].tbl.data().count() + " " + window[ob].i18nEntries.record);
if (window[ob].editorXt) {
$('#' + window[ob].tableId + '_xtForm')[0].reset();
}
}
} else if (window[ob] instanceof QuadForm) {
window[ob].clearForm();
}
window[ob].startIn = 0;
});
}
} else if (window[o] instanceof QuadForm) {
window[o].clearForm();
}
window[o].startIn = 0;
});
}
答案 0 :(得分:1)
function recursiveFuntcion(obj) {
// base condition
if (!obj || !obj.detailsObjects)
return;
_.forEach(obj.detailsObjects, function(o, key) {
if (window[o] instanceof QuadTable) {
//limpamos o details e reiniciamos a info
if ($.fn.DataTable.isDataTable('#' + window[o].tableId)) {
debugger;
window[o].tbl.clear();
window[o].tbl.columns.adjust().draw();
$('#' + window[o].tableId + "_info > .nRecords").text(window[o].tbl.data().count() + " " + window[o].i18nEntries.record);
if (window[o].editorXt) {
$('#' + window[o].tableId + '_xtForm')[0].reset();
}
}
//recursive call
recursiveFunction(window[o]);
} else if (window[o] instanceof QuadForm) {
window[o].clearForm();
}
window[o].startIn = 0;
});
}