从json回调渲染树视图 [英] rendering tree view from json callback
问题描述
JSON回调给我格式化的日期,只有它。在给定的日期,我必须建立一个树状结构,这将分开不同的年,月和日。回调中获得的非常日期包含在树中。
JSON callback returns me formatted dates and only it. Basing on given dates I have to build a tree structure, which will seperate different years, months and days. Oonly dates which are obtained in the callback are included in the tree.
我在ServiceSucceedCallBack上写下如下所示:
I've write something like below on ServiceSucceedCallBack:
var daty = '';
var roczniki = '';
var miesiace = '';
var dni = '';
for (var i in result.Content) {
roczniki += '<ol id="lata">' + result.Content[i].getFullYear() + '</ol>';
miesiace += '<ol id="miesiace"><li>' + (result.Content[i].getMonth() + 1) + '</li></ol>';
dni += '<ol id="dni"><li>' + result.Content[i].getDate() + '</li></ol>';
}
var $st = $('#toolLeft');
$st.append(roczniki);
$('#lata').append(miesiace);
$('#miesiace').append(dni);
它提供树视图,但每个日期都写入第一个节点(第一个发现的年份)和第二个问题是我不知道如何忽略数据的重复。我的意思是说,如果有一年在树上有一个地方,那么同一年的另一个日子应该去同一个节点,不要创造新的一个....
It gaves a tree view, but every date is written to the first node (first found year) and second problem is that I have no idea how to ommit duplication of datas. I mean, if some year has a place in the tree, than another date with the same year should go to the same node level, no create new one....
推荐答案
它提供树视图,但每个日期都写入第一个节点
It gaves a tree view, but every date is written to the first node
使用 JSON.stringify
来构建一个DOM,就像这个问题一样:
Use JSON.stringify
to build a DOM, as in this question:
Javascript:使用reviver功能,我似乎无法改变所有的键,同时连接数字
如果有一年树中的一个地方,同一年的另一个日期应该去同一个节点级别,不要创建新的一个
If some year has a place in the tree, than another date with the same year should go to the same node level, no create new one
使用循环将每个日期作为对象文字的关键字(如foo)插入,然后使用 JSON.parse
删除重复的键。这是一个例子:
Use a loop to insert each date as the key of an object literal, such as foo, then use JSON.parse
to remove duplicate keys. Here is an example:
var foo = {"2000-01-01":"good", "2001-09-11":"bad", "2000-11-02":"ugly", "2000-01-01":"jetson"}
var bar = JSON.parse(JSON.stringify(foo) )
var baz = JSON.stringify(bar)
这篇关于从json回调渲染树视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!