से अधिक रिकर्सिव यात्रा मैं कोणीय जे एस और उनके उदाहरणों में से एक का उपयोग कर रहा: http://jsfiddle.net/furf/EJGHX/गतिशील नेस्ट वस्तु सरणी
मैं जब अद्यतन समारोह होता है डेटा लेने के लिए और यह करने के लिए कुछ मानों को जोड़ने से पहले मैं सर्वर से भेजने की जरूरत है। (यदि जेएस के बजाए कोणीय के साथ ऐसा करना बेहतर होगा तो मुझे बताएं)
मैं 'पैरेंटिड' और 'इंडेक्स' पाने और बच्चों को अपडेट करने की कोशिश कर रहा हूं।
यहाँ डेटा मैं
{
"children": [{
"id": "5",
"parentid": "0",
"text": "Device Guides",
"index": "1",
"children": [{
"id": "10",
"index": "0",
"text": "Grandstream GXP-21XX"
}, {
"id": "11",
"index": "1",
"text": "Polycom Soundstation/Soundpoint"
}, {
"id": "23",
"index": "2",
"text": "New Polycom"
}]
}, {
"id": "6",
"parentid": "0",
"text": "Pre-Sales Evaluation",
"index": "0",
"children": []
}, {
"id": "7",
"parentid": "0",
"text": "Router Setup Guides",
"index": "2",
"children": [{
"id": "9",
"index": "0",
"text": "Sonicwall"
}, {
"id": "12",
"index": "1",
"text": "Cisco"
}]
}, {
"id": "9",
"parentid": "7",
"text": "Sonicwall",
"index": "0",
"children": []
}, {
"id": "10",
"parentid": "5",
"text": "Grandstream GXP-21XX",
"index": "0",
"children": []
}, {
"id": "11",
"parentid": "5",
"text": "Polycom Soundstation/Soundpoint",
"index": "1",
"children": []
}, {
"id": "12",
"parentid": "7",
"text": "Cisco",
"index": "1",
"children": []
}, {
"id": "15",
"parentid": "0",
"text": "Post-Sales Implementation Check List",
"index": "7",
"children": [{
"id": "16",
"index": "0",
"text": "Porting and New Number Details"
}, {
"id": "18",
"index": "1",
"text": "Partner Setup"
}, {
"id": "19",
"index": "2",
"text": "test"
}, {
"id": "21",
"index": "3",
"text": "test"
}]
}, {
"id": "16",
"parentid": "15",
"text": "Porting and New Number Details",
"index": "0",
"children": []
}, {
"id": "18",
"parentid": "15",
"text": "Partner Setup",
"index": "1",
"children": []
}, {
"id": "19",
"parentid": "15",
"text": "test",
"index": "2",
"children": []
}, {
"id": "20",
"parentid": "0",
"text": "test",
"index": "11",
"children": []
}, {
"id": "21",
"parentid": "15",
"text": "test",
"index": "3",
"children": []
}, {
"id": "23",
"parentid": "5",
"text": "New Polycom",
"index": "2",
"children": []
}, {
"id": "24",
"parentid": "0",
"text": "Test Markup",
"index": "14",
"children": []
}, {
"id": "25",
"parentid": "0",
"text": "test",
"index": "15",
"children": []
}]
}
इस तरह मैं वर्तमान में यह माध्यम से पाशन कर रहा हूँ के माध्यम से पाशन कर रहा हूँ है, लेकिन यह केवल पहला आयाम हो जाता है
for (i = 0, l = data.length; i < l; i++) {
parentid = data[i].id == null ? '0' : data[i].id;
data[i].index = i;
if (data[i].children) {
if (data[i].children.length > 0) {
for (q = 0, r = data[i].children.length; q < r; q++) {
data[i].children[q].parentid = parentid;
data[i].children[q].index = q;
}
}
}
}
मुझे यह एक और पहेली पर मिला, लेकिन मुझे नहीं पता कि मैं अभिभावक को कैसे पकड़ूंगा या सूचकांक
$.each(target.children, function(key, val) { recursiveFunction(key, val) });
function recursiveFunction(key, val) {
actualFunction(key, val);
var value = val['children'];
if (value instanceof Object) {
$.each(value, function(key, val) {
recursiveFunction(key, val)
});
}
}
function actualFunction(key, val) {}
ऐसा लगता है कि इसे काम करना चाहिए। मैंने इसका परीक्षण किया और मुझे लगता है कि पैरेंट आईडी सेट करने के लिए नहीं लगता है। यहां बताया गया है कि यह कैसे एकीकृत है: http://jsfiddle.net/EJGHX/38/ लाइन 213, यदि आप कोई नया बच्चा आइटम जोड़ते हैं और इसे स्थानांतरित करते हैं तो आप परिणाम को कंसोल में देखेंगे। – Stephen
@Stephen - मैंने आपके द्वारा प्रदान किए गए उदाहरण में कोड को तुरंत देखा है, और ऐसा लगता है कि किसी भी नोड्स में उनके साथ जुड़े आईडी नहीं हैं। आप इस जानकारी को कहां सेट करने की उम्मीद कर रहे हैं? फिलहाल, प्रत्येक 'child.parentId' को' 0 '' पर सेट किया जा रहा है क्योंकि 'parent.id' मौजूद नहीं है। – Andrew