Rails,创建树状结构。 JSON 输出
我有一个数据表,可以彼此作为父母或孩子,它用字段parent_id处理(我使用的是act_as_tree gem)
第一级项目有0作为parent_id,
可能有无限数量的孩子。我想输出为 JSON。 最终的输出应该是这样的,
{
"feild1": "dd",
"filed2": "ee",
"child" : {
"feild1": "dd",
"filed2": "ee",
}
"child" : {
"feild1": "dd",
"filed2": "ee",
"child" : {
"feild1": "dd",
"filed2": "ee",
}
}
}
到目前为止,
def coa_tree
@roots = Coa.find(:all, :conditions => ['parent_id = ?', 0])
@response = @roots
@roots.each do |root|
logger.debug "roots each"
output = root
root.children.each do |child|
output = {:child => output, :child => child}
end
end
respond_with(@response)
end
我所拥有的就是这个,显然我什至还没有接近解决问题。如果有人能指出我正确的方向,我将非常感激。也许有一个我不知道的插件可以帮助我解决这个问题。 谢谢。
i have a table of data, which can have each other as parents or childs, its handled with the field parent_id (I am using the act_as_tree gem)
the first level items have 0 as parent_id
there could be an infinite number of children. I want to output as JSON.
the final output shud be something like this
{
"feild1": "dd",
"filed2": "ee",
"child" : {
"feild1": "dd",
"filed2": "ee",
}
"child" : {
"feild1": "dd",
"filed2": "ee",
"child" : {
"feild1": "dd",
"filed2": "ee",
}
}
}
so far i all i have is this
def coa_tree
@roots = Coa.find(:all, :conditions => ['parent_id = ?', 0])
@response = @roots
@roots.each do |root|
logger.debug "roots each"
output = root
root.children.each do |child|
output = {:child => output, :child => child}
end
end
respond_with(@response)
end
clearly i haven't even come close to solving the problem. If someone could point me to the right direction i would really appreciate it. maybe there is a plugin that i don't know about that would help me solve this.
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 JSON 示例无效,因为同一对象中有多个具有相同名称的键,但从 ActiveRecord 对象将树结构输出为 JSON 绝对是可能的。
尝试将这样的方法添加到您的模型类中:
现在您可以使用以下方法以 JSON 形式检索整个树:
Your JSON example is invalid because there are multiple keys in the same object with the same name, but outputting a tree structure as JSON from an ActiveRecord object is definitely possible.
Try adding a method like this to your model class:
Now you can retrieve the entire tree as JSON using:
我要在 Todd Yandell 的答案中添加的唯一一点是,对于“属性”和“子项”没有单独的键可能会很有用。换句话说,虽然 Yandell 的方法将返回如下数据:
您可能希望上述问题中的数据的格式如下:
该格式对于客户端代码需要树格式 JSON 数据且不允许灵活地为父属性和子属性定义不同的键(例如,Sencha Touch 中的 NestedList 组件等)。
为了实现这一目标,在 Yandell 的概念的基础上,我提出了以下建议:
The only thing I would add to Todd Yandell's answer is that it may be useful to not have separate keys for "attributes" and "children". In other words, while Yandell's method would return data like this:
You may wish for the data in the question, above, to be formatted like this:
That format is particularly useful with situations where client-side code expects tree-formatted JSON data and does not allow flexibility to define different keys for parent attributes and children (e.g., a NestedList component in Sencha Touch, etc.).
To accomplish this, building on Yandell's concept, I came up with the following: