Chidnode返回count = 0,虽然它有价值? [英] Chidnode returns count=0 althought it has value ?

查看:62
本文介绍了Chidnode返回count = 0,虽然它有价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我正在使用此代码



Hi all I'm using this code

my category class is







[Serializable]
    public class Category
    {
    [DisplayName("Id")]
    public int CategoryId { get; set; }

    [Required]
    public string Name { get; set; }

    public DateTime CreatedDate { get; set; }

    public int ParentId { get; set; }

    public virtual ICollection<Product> Products { get; set; }







and my TreeViewNodeVM is

    public string id { get; set; }
    public string text { get; set; }
    public IList<TreeViewNodeVM> ChildNode { get; set; }





i将表格i数据库作为类别





i have table i database as Categories

CategoryId        Name         CreatedDate          ParentId
1               category    2014-05-06 19:00:20.107     0
2               Category1   2014-05-06 19:00:20.107     1
3               Category2   2014-05-06 19:00:20.107     1
I am binding my jstree with this table as





我的Categorycontroller代码是





My Categorycontroller code is

public JsonResult GetList(int id = 0)
       {
           var objList = GetTreeVeiwList();
          return Json(objList, JsonRequestBehavior.AllowGet);
       }







public List GetTreeVeiwList() {

        var rootNode = (from e1 in _CategoriesBusiness.Select()
                        where e1.ParentId == 0
                        select new TreeViewNodeVM()
                        {
                            id =Convert.ToString(e1.CategoryId),
                            text = e1.Name
                        }).ToList();
        BuildChildNode(rootNode);

        return rootNode;
    }




public JsonResult SaveChanges(string ar)
{
    var data = new JavaScriptSerializer().Deserialize<string[]>(ar);
    Categories objCategories=null;
    foreach (var a in data)
    {
        var ab = a.Split(' ');
         objCategories=new Categories();
         int tempCategoryId;
         if (int.TryParse(ab[1], out tempCategoryId))
         {
             objCategories.CategoryId = tempCategoryId;

             var objcat=(from cat in _CategoriesBusiness.Select() where cat.CategoryId==tempCategoryId select cat).FirstOrDefault();
             if (objcat != null)
             {
                 objcat.Name = ab[0].Trim();
                 int tempParantId;
                 if (int.TryParse(ab[2], out tempParantId))
                 {
                     objcat.ParentId = tempParantId;
                 }
                 _CategoriesBusiness.Update(objcat);
             }
             else
             {
                 objCategories.CategoryId = Convert.ToInt32(ab[1]);
                 objCategories.Name = ab[0];
                 int tempParantId1;
                 if (int.TryParse(ab[2], out tempParantId1))
                 {
                     objCategories.ParentId = tempParantId1;
                 }
                 _CategoriesBusiness.Create(objCategories);
             }

         }
         else
         {
             objCategories.Name = ab[0];
             int tempParantId1;
             if (int.TryParse(ab[2], out tempParantId1))
             {
                 objCategories.ParentId = tempParantId1;
             }
             _CategoriesBusiness.Create(objCategories);

         }
                    }

    return Json(new { },JsonRequestBehavior.AllowGet);
}




    private void BuildChildNode(List<TreeViewNodeVM> ListrootNode)
    {
        foreach (TreeViewNodeVM rootNode in ListrootNode)
        {
            if (rootNode != null)
            {
                List<TreeViewNodeVM> chidNode = (from e1 in _CategoriesBusiness.Select()
                                                 where e1.ParentId.ToString() == rootNode.id
                                                 select new TreeViewNodeVM()
                                                 {
                                                     id = Convert.ToString(e1.CategoryId),
                                                     text = e1.Name
                                                 }).ToList<TreeViewNodeVM>();
                if (chidNode.Count > 0)
                {
                    BuildChildNode(chidNode);
                    foreach (var childRootNode in chidNode)
                    {
                        rootNode.children.Add(childRootNode);
                    }

                }
            }
        }
    } 



and view is:

< href="../../Content/dist/themes/default/style.min.css" rel="stylesheet">

<script src="../../Content/dist/jstree.js">

@model List<Web.ViewModels.TreeModels.TreeViewNodeVM>;
@{

    ViewBag.Title = "Index";
}
&lt;h2&gt;
    Categories&lt;/h2&gt;

&lt;input type="button" id="btnaddFolder" value="Add new folder" onclick="File_Folder()" /&gt;
&lt;input type="button" id="btnaddCat" value="Add new Category" onclick="File_create()" /&gt;
&lt;input type="button" id="btnSave" value="Save Changes" onclick="SaveChanges()" /&gt;
&lt;input type="button" id="btnCancel" value="Cancel" /&gt;

&lt;div id="jstree"&gt;

&lt;/div&gt; 

  &lt;script&gt;


      function SaveChanges() {
          debugger
          var ar = [];
          var i = 0;
          var arr = "";
          $('#jstree li').each(function () {
              debugger

              var id = $(this).attr('id');
              var text = $(this).find("a:first").text();              

              if ($(this).parent('ul.jstree-children')) {
                  var parent = $(this).parent('ul.jstree-children').parent('li').attr('id');
                  alert(text + " " + id + " " + parent);

                  var item = text + " " + id + " " + parent;
                  ar.push(item);

                  i++;
              }

          });
          arr = JSON.stringify(ar);
          //$('#jstree li.jstree-node').each(function () {

          //    some.push($(this).attr("id"));
          //    // or
          //    //some.push(this.id);
          //});
          debugger;
          $.ajax({
              type: 'post',
              url: "/Category/SaveChanges", data: { ar: arr }, success: function (result) {
                  $("#div1").html(result);
              }
          });

      }

      function AddNewNode()
      {
          File_create();
      }



      $(function () {
          // 6 create an instance when the DOM is ready
          debugger

          $("#jstree").jstree({

              "core": {


                  "animation": 0,
                  "check_callback": true,
                  "themes": { "stripes": true },
                  'data': {

                      'url': function (node) {

                          debugger
                          return node.id === '#' ?
                            '/Category/GetList' : '/Category/GetList1';
                      },
                      'data': function (node) {
                          debugger
                          return { 'id': node.id };
                      }
                  }
              },



              "contextmenu": {
                  "items": function ($node) {
                      var tree = $("#jstree").jstree(true);
                      return {
                          "Create": {
                              //"separator_before": false,
                              //"separator_after": false,
                              "label": "Create",
                              "action": function (data) {
                                  debugger;
                                      File_create();
                                  //var ref = $('#jstree').jstree(true);
                                  //ref.create_node(data);
                              }
                          },
                          "Rename": {
                              "separator_before": false,
                              "separator_after": false,
                              "label": "Rename",
                              "action": function (obj) {
                                  debugger;
                                  tree.edit($node);
                              }
                          },
                          "Remove": {
                              "separator_before": false,
                              "separator_after": false,
                              "label": "Remove",
                              "action": function (obj) {
                                  tree.delete_node($node);
                              }
                          }
                      };
                  }
              },




              "types": {
                  "#": {
                      "max_children": 1,
                      "max_depth": 4,
                      "valid_children": ["root"]
                  },
                  "root": {
                      "icon": "/static/3.0.0/assets/images/tree_icon.png",
                      "valid_children": [],
                      "name":"Folder"
                  },
                  "default": {

                      "valid_children": ["default", "file"]
                  },
                  "file": {
                      "icon": "glyphicon glyphicon-file",
                      "valid_children": []
                  }
              },
              "plugins": [
                "contextmenu", "dnd", "search",
                "state", "types", "wholerow"
              ]


          })


      });


      function File_Folder() {


          debugger
          var ref = $('#jstree').jstree(true),
              sel = ref.get_selected();
          if (!sel.length) { return false; }
          var number = 1 + Math.floor(Math.random() * 10);


          sel = ref.create_node(sel, { "type": "default"});
          if (sel) {
              ref.edit(sel);
          }
      }


      function File_create() {
          debugger
          var ref = $('#jstree').jstree(true),
              sel = ref.get_selected();
          if (!sel.length) { return false; }         

          sel = ref.create_node(sel, { "type": "file"});
          if (sel) {
              ref.edit(sel);
          }
      }
  &lt;/script&gt;

</pre>



but childnode return count=0 

any one can tell me where is the problem 

childnode must return category1 , category2 for the parent categories as shown i code that I'm using now

<b>What I have tried:</b>

I don't know what wrong , I need something to try

推荐答案

' #jstree li')。 (function(){
debugger

var id =
('#jstree li').each(function () { debugger var id =


this )。attr(' id');
var text =
(this).attr('id'); var text =


this )。find ( a:first)。text();

if
(this).find("a:first").text(); if (


这篇关于Chidnode返回count = 0,虽然它有价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆