zoukankan      html  css  js  c++  java
  • 一周代码秀之[11.18~11.24 linq2xml面向对象]

    1、xml

      <Sections>
        <Item key ="1" value ="孕哺期" canBeSelected="false">
          <Child key ="6" value ="备孕期"/>
          <Child key ="7" value ="怀孕期"/>
          <Child key ="8" value ="分娩期" />
        </Item>
        <Item key ="2" value ="0 - 6月"/>
        <Item key ="3" value ="7 - 12月"/>
        <Item key ="4" value ="1 - 3岁"/>
        <Item key ="5" value ="4 - 6岁"/>
      </Sections>

    2、提取类

        public class ParentItem
        {
            public int Key { get; set; }
            public string Value { get; set; }
            public bool canBeSelected { get; set; }
            public List<Child> Children { get; set; }
        }
        public class Child
        {
            public int Key { get; set; }
            public string Value { get; set; }
        }

    3、linq读取xml,并将其填充实体集合

       string menuPath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "/Files/Config/BaseConfig.xml";
                XDocument doc = XDocument.Load(menuPath);
                var listP = from x in doc.Descendants("Sections").Elements("Item")
                            select new ParentItem
                            {
                                canBeSelected = false,
                                Key = (int)x.Attribute("key"),
                                Value = (string)x.Attribute("value"),
                                Children =
                                     (
                                      from t in x.Descendants("Child")
                                      select new Child
                                      {
                                          Key = (int)t.Attribute("key"),
                                          Value = (string)t.Attribute("value")
                                      }
    
                                    ).ToList()
                            };

    4、easyui combobox的绑定的默认json格式

    [{
        "id":1,
        "text":"Folder1",
        "iconCls":"icon-ok",
        "children":[{
            "id":2,
            "text":"File1",
            "checked":true
        }
    ]

    5、 linq构建conbobox的json格式

      var jsonData = from p in listP
                               select new
                               {
                                   id = p.Key,
                                   text = p.Value,
                                   ischecked = selectedNode.Split(',').Contains(p.Key.ToString()) ? true : false,
                                   children = from c in p.Children
                                              select new
                                              {
                                                  id = c.Key,
                                                  text = c.Value,
                                                  ischecked = selectedNode.Split(',').Contains(c.Key.ToString()) ? true : false,
                                              }
    
                               };
                JavaScriptSerializer jss = new JavaScriptSerializer();
    
                string firstNode = (selectedNode == "-1" || selectedNode.Trim() == "") ? "[{"id":-1,"checked":true,"text":"--请选择--"}," : "[{"id":-1,"text":"--请选择--"},";
                if (selectedNode.Trim() == "noheader")
                {
                    return jss.Serialize(jsonData).Replace("ischecked", "checked");
                }
                return firstNode + jss.Serialize(jsonData).Substring(1).Replace("ischecked", "checked");

    6、前端代码

     <select class="easyui-combotree" id="txtSection"> 
      </select>
    
     $('#txtSection').combotree({
                    url: "AddEditKnowledges.aspx?action=load",
                    valueField: 'id',
                    textField: 'text',
                    onClick: function (node) {
                        // alert(node.text + ":" + node.id);
                        if (node.text != '孕哺期') {
                            $("#hdnSection").val(node.id);
                        }
                    }
    });

    7、结果

    生活没有输赢,不要在乎别人如何评价你,开心就好。 QQ群:158138959
  • 相关阅读:
    http与websocket(基于SignalR)两种协议下的跨域基于ASP.NET MVC--竹子整理
    让Visual Studio 2015 支持ASP.NET MVC4.0.0.1
    自定义滚动条CSS样式
    使用NuGet发布自己的类库包(Library Package)
    基于EF的数据外键关联查询
    基于EF创建数据库迁移
    用SQL命令查看Mysql数据库大小
    Python之MySQL数据操作
    Python之MySQL基础
    Python网络编程之黏包问题
  • 原文地址:https://www.cnblogs.com/zjflove/p/3435342.html
Copyright © 2011-2022 走看看