亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

jQuery ztree實現(xiàn)動態(tài)樹形多選菜單

 更新時間:2016年08月12日 16:41:01   作者:ilovexiaou  
這篇文章主要介紹了jQuery ztree實現(xiàn)動態(tài)樹形多選菜單,ztree動態(tài)樹形菜單,初始化加載和延遲加載,感興趣的小伙伴們可以參考一下

我用到的版本ztree core v3.5.24,需要引入的js,css,jquery.js,jquery.ztree.core.js,jquery.ztree.excheck.js(多選框可選),zTreeStyle.css。
需要注意的指向父節(jié)點的pId,我開始寫的是pid一開始沒注意,默認的是pId,當然可以pIdKey指定自定義的(未測)。還有如果圖片沒顯示肯定是沒引入img圖片,記得寫好路徑。

1、先介紹初始化加載ztree

 jsp

<div > 
 <ul id="treeDemo" class="ztree" style="margin-top: 0; width: 160px;"> 
 </ul> 
</div>

js

<script type="text/javascript">
$(function(){
 $.ajax({ 
  url: '${contextPath}/om/quoteOmRequest.do?flag=init',
  data: { 
   name : '1' //隨便寫的,傳入后臺的值
  },
  type:'post',
  traditional: true,
  success: function(data){
   var dataObj = eval(data);
   var zTreeObj;
   var setting = {
     data: { 
      simpleData: { 
       enable:true, 
       /* idKey: "id", 
       pIdKey: "pId" */ 
      } 
     },
     check: {
      enable: true,
      chkboxType :{ "Y" : "", "N" : "s" } //Y:勾選(參數(shù):p:影響父節(jié)點),N:不勾(參數(shù)s:影響子節(jié)點)[p 和 s 為參數(shù),參數(shù)都不寫""為全不影響]
     },
     callback: {
      onCheck: 
        function() { 
         var zTree = $.fn.zTree.getZTreeObj("treeDemo");
         var checkCount = zTree.getCheckedNodes(true);
         var classpurview = "";
         for(var i=0;i<checkCount.length;i++) {
           classpurview += checkCount[i].id+","  //存入數(shù)據(jù)的id,比如這種形式"1,2,5," 后臺截取下
         }
         /* alert(classpurview); */
       } ,
     },
     view: { 
      showLine: true, 
      showIcon: true, 
      dblClickExpand: true 
     }, 
   };
   var zNodes = dataObj; 
   $(document).ready(function(){
    $.fn.zTree.init($("#treeDemo"), setting, zNodes);
   });
  },
  error : function() { 
   alert("異常!"); 
  }
 });
});
</script>

后臺部分可以參考2延遲加載

2、延遲加載ztree
jsp一樣的,js有所有區(qū)別,這個參考了官方api

<script type="text/javascript">
var setting = {
  view: {
   selectedMulti: false
  },
  check: {
   enable: true,
   chkboxType :{ "Y" : "", "N" : "s" } //Y:勾選(參數(shù):p:影響父節(jié)點),N:不勾(參數(shù)s:影響子節(jié)點)[p 和 s 為參數(shù),參數(shù)都不寫""為全不影響]
  },
  async: {
   enable: true,
   url:"${contextPath}/om/quoteOmRequest.do?flag=ajax",
   autoParam:["id"], 
   dataFilter: filter
  },
  callback: {
   beforeClick: beforeClick,
  }
 };

 function filter(treeId, parentNode, childNodes) {
  if (!childNodes) return null;
  for (var i=0, l=childNodes.length; i<l; i++) {
   childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
  }
  return childNodes;
 }
 function beforeClick(treeId, treeNode) {
  if (!treeNode.isParent) {
   alert("請選擇父節(jié)點,此節(jié)點是根節(jié)點...");
   return false;
  } else {
   return true;
  }
 }

 $(document).ready(function(){
  $.fn.zTree.init($("#treeDemo"), setting);
 });
</script>

后臺部分,也包含了1的后臺,先看核心代碼,這個用的struts,哪個框架都差不多。

  String flag = request.getParameter("flag");
  if(flag.equals("init")){//直接加載ztree
   List<Inner> list = getTrees();//得到所有節(jié)點
   try {
    convertListToJson(list);
   } catch (Exception e) {
    e.printStackTrace();
   }
  }else if(flag.equals("ajax")){//延遲加載ztree
   String id = request.getParameter("id");
   if(id==null){//第一次進入初始化父節(jié)點
    List<Inner> list = new ArrayList<OmRequestImpl.Inner>();
    Inner in1=getById(1);
    Inner in5=getById(5);
    list.add(in1);
    list.add(in5);//測試用的,得到初始化0級父節(jié)點,應(yīng)該從數(shù)據(jù)庫中獲取。
    try {
     convertListToJson(list);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }else{//根據(jù)父節(jié)點id加載對應(yīng)的子節(jié)點
    List<Inner> list = getChilds(Integer.valueOf(id));//通過父id取得子節(jié)點集合,測試就自己寫個方法,應(yīng)該從數(shù)據(jù)庫中獲取。
    try {
     convertListToJson(list);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  }

這里涉及了一些方法,我為了測試方便自己寫了個測試bean,正式的直接從數(shù)據(jù)庫中獲取。下面是bean及一些小方法。

public class Inner{
  private Integer id; 
  private Integer pId; //指向父節(jié)點id
  private String name; //顯示的內(nèi)容 
  private String isParent;//是否是父節(jié)點
  public Inner(){
   this.isParent="false";
  }
  public String getIsParent() {
   return isParent;
  }
  public void setIsParent(String isParent) {
   this.isParent = isParent;
  }
  public Integer getpId() {
   return pId;
  }
  public void setpId(Integer pId) {
   this.pId = pId;
  }
  public Integer getId() {
   return id;
  }
  public void setId(Integer id) {
   this.id = id;
  }
  public String getName() {
   return name;
  }
  public void setName(String name) {
   this.name = name;
  }
}

// -- 將運行結(jié)果用json字符返回客戶端
 public void convertListToJson(List<?> list)throws Exception{
   JSONArray json = JSONArray.fromObject(list); 
   response.setHeader("Cache-Control", "no-cache");
   response.setContentType("text/html; charset=UTF-8"); 
   PrintWriter writer;
   writer = response.getWriter();
   writer.write(json.toString());
   writer.close();
 }

這些方法可忽略,測試用的,可通過數(shù)據(jù)庫獲取

public List<Inner> getTrees(){
  Inner in=new Inner();
  in.setId(1);
  in.setpId(0);
  in.setIsParent("true");
  in.setName("父節(jié)點1");
  Inner in1=new Inner();
  in1.setId(2);
  in1.setpId(1);
  in1.setName("子節(jié)點11");
  in1.setIsParent("true");
  Inner in2=new Inner();
  in2.setId(3);
  in2.setpId(1);
  in2.setName("子節(jié)點12");
  Inner in3=new Inner();
  in3.setId(4);
  in3.setpId(2);
  in3.setName("子節(jié)點111");
  Inner in4=new Inner();
  in4.setId(5);
  in4.setpId(0);
  in4.setIsParent("true");
  in4.setName("父節(jié)點2");
  Inner in5=new Inner();
  in5.setId(6);
  in5.setpId(5);
  in5.setName("子節(jié)點21");
  List<Inner> list=new ArrayList<Inner>();
  list.add(in);
  list.add(in1);
  list.add(in2);
  list.add(in3);
  list.add(in4);
  list.add(in5);
  return list;
 }
 public Inner getById(Integer id){
  List<Inner> list = getTrees();
  for (Inner inner : list) {
   if(id==inner.getId()){
    Inner in=inner;
    return in;
   }
  }
  return null;
 }
 public List<Inner> getChilds(Integer id){
  List<Inner> list = getTrees();
  List<Inner> result =new ArrayList<OmRequestImpl.Inner>();
  for (Inner inner : list) {
   if(id.intValue()==inner.getpId().intValue()){
    result.add(inner);
   }
  }
  return result;
 }

更多關(guān)于ztree控件的內(nèi)容,請參考專題《jQuery插件ztree使用匯總》

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • jQuery熱氣球動畫半透明背景的后臺登錄界面代碼分享

    jQuery熱氣球動畫半透明背景的后臺登錄界面代碼分享

    這篇文章主要介紹了jQuery實現(xiàn)熱氣球動畫背景登錄框,適合用于后臺登陸界面設(shè)計,推薦給大家,有需要的小伙伴可以參考下。
    2015-08-08
  • jQuery獲取動態(tài)生成的元素示例

    jQuery獲取動態(tài)生成的元素示例

    頁面上可以動態(tài)添加數(shù)據(jù),比如table,點擊按鈕可以動態(tài)添加行,下面與大家分享下jQuery如何獲取動態(tài)生成的元素
    2014-06-06
  • Expandable "Detail" Table Rows

    Expandable "Detail" Table Rows

    Expandable "Detail" Table Rows...
    2007-08-08
  • jQuery時間驗證和轉(zhuǎn)換為標準格式的時間格式

    jQuery時間驗證和轉(zhuǎn)換為標準格式的時間格式

    本篇文章主要介紹了jQuery時間驗證和轉(zhuǎn)換為標準格式的時間示例代碼,具有一定的參考價值,有興趣的可以了解一下。
    2017-03-03
  • jquery的 filter()方法使用教程

    jquery的 filter()方法使用教程

    filter() 方法返回符合一定條件的元素。該方法讓您規(guī)定一個條件。不符合條件的元素將從選擇中移除,符合條件的元素將被返回。這篇文章主要介紹了 jquery的 filter()方法使用,需要的朋友可以參考下
    2018-03-03
  • Jquery 數(shù)組操作大全個人總結(jié)

    Jquery 數(shù)組操作大全個人總結(jié)

    jQuery的數(shù)組處理,便捷,功能齊全. 最近的項目中用到的比較多,最近時間緊迫,今天抽了些時間回過頭來看 jQuery中文文檔順便對jQuery數(shù)組做個總結(jié)
    2013-11-11
  • jquery實現(xiàn)最簡單的滑動菜單效果代碼

    jquery實現(xiàn)最簡單的滑動菜單效果代碼

    這篇文章主要介紹了jquery實現(xiàn)最簡單的滑動菜單效果代碼,涉及jQuery基于鼠標事件操作頁面元素動態(tài)變換的基本技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09
  • jQuery學(xué)習(xí)基礎(chǔ)知識小結(jié)

    jQuery學(xué)習(xí)基礎(chǔ)知識小結(jié)

    jQuery學(xué)習(xí)基礎(chǔ)知識小結(jié),剛開始學(xué)習(xí)jquery的朋友可以參考下。
    2010-11-11
  • jquery自定義組件實例詳解

    jquery自定義組件實例詳解

    這篇文章主要為大家詳細介紹了jquery自定義組件實例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • jQuery插件分享之分頁插件jqPagination

    jQuery插件分享之分頁插件jqPagination

    jqPagination 是一個簡單易用的輕量級 jQuery分頁插件,其使用了 HTML5 和 CSS3 技術(shù)來實現(xiàn)。此插件提供了幾個參數(shù)設(shè)置選項,通過簡單的配置即可生成分頁控件。此外,它的外觀樣式是可自定義的,擴展性很強。
    2014-06-06

最新評論