Answer the question
In order to leave comments, you need to log in
How to convert array to horizontal html table?
Greetings.
The task is to organize a horizontal drop-down list.
I found such a thing . Formation occurs with the help of ul - li elements.
If anyone knows what is better found - there will be a big gratitude.
What is the meaning of the question. In php there is a selection from the database, slightly normalized. This is an associative array, the array has an element that may be missing. The idea is that if this element is present, it contains the same array, with the same conditions (there can be several such elements), and all this goes deep. As I understand it, you need to do a recursive traversal, but there is a problem with the cycle, inside the element there may be a link to a parent element, and then it will bypass indefinitely.
Can you tell me the easiest way to convert such a dataset into a list?
Answer the question
In order to leave comments, you need to log in
function fn_tree(a) {
// где a - ответ полученный от сервера
// до вызова функции надо проверить на typeof(a) === 'undefined' || a === null
var arr1 = [];
for (var i1 = 0; i1 < a.length; ++i1) {
var arr2 = [];
if (typeof(a.[i1].nextChild) === 'undefined' || a.[i1].nextChild === null) {
for (var i2 = 0; i2 < a.[i1].nextChild.length; ++i2) {
var html2 = '' +
'<div>' +
a.[i1].nextChild.[i2].id +
'</div>' +
'';
arr2.push(html2);
};
}
var html1 = '' +
'<div>' +
'<div>' +
a.[i1].id +
'<div>' +
'<div>' +
arr2.join('') +
'<div>' +
'</div>' +
''; // рагульно строим DOM и чем-то его наполняем, нет времени :-)
arr1.push(html1);
};
var b = arr1.join('');
return b;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question