• 一周代码秀之[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
  • 相关阅读:
    搭建nexus私服(maven)
    maven配置本地仓库(从本地仓库下载jar包到.m2仓库)
    关于引入多个jquery冲突的问题(附一个很好用的validate前端验证框架及使用方法)
    java excel导出
    Eclipse+Maven创建webapp项目 及部署在tomcat上
    solr添加IK分词和自己定义词库
    将Mysq数据导入solr索引库
    solr +zookeeper+Jetty 集群搭建
    solr +zookeeper+tomcat 集群搭建
    如何用Maven创建web项目(具体步骤)转载
  • 原文地址:https://www.cnblogs.com/zjflove/p/3435342.html
Copyright © 2020-2023  润新知