使用关联父项的ajax配置选项使用json数据在rails中创建jstree =>孩子们
我的模型是:
class Project < ActiveRecord::Base
belongs_to :parent, :foreign_key => :project_id, :class_name => "Project"
has_many :children, :dependent => :destroy, :class_name => "Project", :foreign_key => :project_id
jquery 树插件 与 json_data 示例:
$(function () {
$("#demo2").jstree({
"json_data" : {
"ajax" : {
"url" : "/static/v.1.0pre/_docs/_json_data.json",
"data" : function (n) {
return { id : n.attr ? n.attr("id") : 0 };
}
}
},
"plugins" : [ "themes", "json_data" ]
});
});
以 json 格式提供数据的基本结构是:
{
"data" : {
"title" : "The node title",
// omit `attr` if not needed; the `attr` object gets passed to the jQuery `attr` function
"attr" : { "id" : "node_identificator", "some-other-attribute" : "attribute_value" } },
// `state` and `children` are only used for NON-leaf nodes
"state" : "closed", // or "open", defaults to "closed"
"children" : [ /* an array of child nodes objects */ ]
}
我想动态地为项目集合创建 jstree。该集合可以有子项,每个子项都有子项,依此类推。我的控制器已经响应 json 并且在视图中我现在有了创建树的代码。
我使用这个 json 数据示例进行测试,它创建了我想要的树:
{
"data" : {
"title" : "Projectos",
"attr" : { "href" : "/projects" } },
"children" : [ {
"data" : {
"title" : "teste",
"attr" : { "href" : "/projects/7" , "class" : "selected" } },
"state" : "open" ,
"children" : [ {
"data" : {
"title" : "teste_1",
"attr" : { "href" : "/projects/9" } },
"children" : [ ] }
] } , {
"data" : {
"title" : "teste1",
"attr" : { "href" : "/projects/8" } },
"children" : [ ] }
], "state" : "open" }
我想按照上面的架构示例自动生成 json 数据到我的项目集合。有什么建议吗?
My model is:
class Project < ActiveRecord::Base
belongs_to :parent, :foreign_key => :project_id, :class_name => "Project"
has_many :children, :dependent => :destroy, :class_name => "Project", :foreign_key => :project_id
jquery tree plugin with json_data example:
$(function () {
$("#demo2").jstree({
"json_data" : {
"ajax" : {
"url" : "/static/v.1.0pre/_docs/_json_data.json",
"data" : function (n) {
return { id : n.attr ? n.attr("id") : 0 };
}
}
},
"plugins" : [ "themes", "json_data" ]
});
});
and the basic structure to supply data in the json format is:
{
"data" : {
"title" : "The node title",
// omit `attr` if not needed; the `attr` object gets passed to the jQuery `attr` function
"attr" : { "id" : "node_identificator", "some-other-attribute" : "attribute_value" } },
// `state` and `children` are only used for NON-leaf nodes
"state" : "closed", // or "open", defaults to "closed"
"children" : [ /* an array of child nodes objects */ ]
}
I want to create the jstree for a collection of projects dynamically. this collection can have childs, and each child childs and so on. my controller already responds to json and in view I have now the code to create the tree.
I test with this json data example and it creates the tree that I want to:
{
"data" : {
"title" : "Projectos",
"attr" : { "href" : "/projects" } },
"children" : [ {
"data" : {
"title" : "teste",
"attr" : { "href" : "/projects/7" , "class" : "selected" } },
"state" : "open" ,
"children" : [ {
"data" : {
"title" : "teste_1",
"attr" : { "href" : "/projects/9" } },
"children" : [ ] }
] } , {
"data" : {
"title" : "teste1",
"attr" : { "href" : "/projects/8" } },
"children" : [ ] }
], "state" : "open" }
I want to generate json data automatically to my collection of projects following the schema example above. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OP 写道:
The OP wrote: