茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    zTree插件下拉樹(shù)使用入門(mén)教程
    來(lái)源:易賢網(wǎng) 閱讀:5995 次 日期:2016-07-08 13:50:11
    溫馨提示:易賢網(wǎng)小編為您整理了“zTree插件下拉樹(shù)使用入門(mén)教程”,方便廣大網(wǎng)友查閱!

    最近,因?yàn)楣ぷ餍枰粋€(gè)樹(shù)形下拉框的組件,經(jīng)過(guò)查資料一般有兩種的實(shí)現(xiàn)方法。其一,就是使用zTree實(shí)現(xiàn);其二,就是使用easyUI實(shí)現(xiàn)。因?yàn)楣镜那岸瞬皇鞘褂胑asyUI設(shè)計(jì)的,故這里我選擇了zTree來(lái)實(shí)現(xiàn)下拉樹(shù)。

    這里使用簡(jiǎn)單的數(shù)據(jù)格式(即簡(jiǎn)單的Json格式)類(lèi)似如下Json:

    var zNodes =[

          {id:1, pId:0, name:"北京"},

          {id:2, pId:0, name:"天津"},

          {id:3, pId:0, name:"上海"},

          {id:6, pId:0, name:"重慶"},

          {id:4, pId:0, name:"河北省", open:true, nocheck:true},

          {id:41, pId:4, name:"石家莊"},

          {id:42, pId:4, name:"保定"},

          {id:43, pId:4, name:"邯鄲"},

          {id:44, pId:4, name:"承德"},

          {id:5, pId:0, name:"廣東省", open:true, nocheck:true},

          {id:51, pId:5, name:"廣州"},

          {id:52, pId:5, name:"深圳"},

          {id:53, pId:5, name:"東莞"},

          {id:54, pId:5, name:"佛山"},

          {id:6, pId:0, name:"福建省", open:true, nocheck:true},

          {id:61, pId:6, name:"福州"},

          {id:62, pId:6, name:"廈門(mén)"},

          {id:63, pId:6, name:"泉州"},

          {id:64, pId:6, name:"三明"}

         ];

    這里首先需要一個(gè)實(shí)體bean,用來(lái)封裝對(duì)應(yīng)查出來(lái)的數(shù)據(jù),如下:

    public class ZtreeNode {

      // id

      private String id;

      // 父id

      private String pId;

      // 顯示名稱(chēng)

      private String name;

      // 是否打開(kāi) (這里默認(rèn)是不打開(kāi)的,如果需要打開(kāi),設(shè)為true)

      // private boolean open ;

      // 能否選擇 (設(shè)置節(jié)點(diǎn)是否能夠選擇,默認(rèn)都能選擇,設(shè)為true對(duì)應(yīng)的節(jié)點(diǎn)不能選擇)

      // private boolean nocheck ;

      /**getter and setter*/

    }

    這里需要注意的是 pId 中的第二的字母是大寫(xiě)的,如果寫(xiě)成小寫(xiě)的就不能構(gòu)造成樹(shù)形結(jié)構(gòu),所有的都是根節(jié)點(diǎn)。

    然后,將從數(shù)據(jù)庫(kù)中查出來(lái)的數(shù)據(jù),轉(zhuǎn)換為對(duì)應(yīng)的ztree需要的bean,再轉(zhuǎn)換為相應(yīng)的Json,代碼如下:

    // 獲取商品分類(lèi)樹(shù) 返回json

      public String getGoodsCategoryTreeJson() {

        List<GoodsCategory> allGoodsCategoryList = goodsCategoryService.getGoodsCategoryTreeJson() ;

        List<ZtreeNode> ztreelist = new ArrayList<ZtreeNode>();

        for(GoodsCategory gcty : allGoodsCategoryList){

          ZtreeNode treenade = new ZtreeNode();

          treenade.setId(gcty.getId());

          treenade.setpId(gcty.getParent()==null?"":gcty.getParent().getId());

          treenade.setName(gcty.getName());

          ztreelist.add(treenade);

        }

        return ajax(ztreelist);

      }

    將list轉(zhuǎn)換為對(duì)應(yīng)的Json方法,如下:

    用到的Json工具包:

    import org.springframework.base.util.JsonUtil;

    private static final String HEADER_ENCODING = "UTF-8";

    private static final boolean HEADER_NO_CACHE = true;

    private static final String HEADER_TEXT_CONTENT_TYPE = "text/plain";

    private static final String HEADER_JSON_CONTENT_TYPE = "text/plain";

    // AJAX輸出

      protected String ajax(String content, String contentType) {

        try {

          HttpServletResponse response = initResponse(contentType);

          response.getWriter().write(content);

          response.getWriter().flush();

        } catch (IOException e) {

          e.printStackTrace();

        }

        return NONE;

      }

      // 根據(jù)文本內(nèi)容輸出AJAX

      protected String ajax(String text) {

        return ajax(text, HEADER_TEXT_CONTENT_TYPE);

      }

      // 根據(jù)操作狀態(tài)輸出AJAX

      protected String ajax(Status status) {

        HttpServletResponse response = initResponse(HEADER_JSON_CONTENT_TYPE);

        Map<String, String> jsonMap = new HashMap<String, String>();

        jsonMap.put(STATUS_PARAMETER_NAME, status.toString());

        JsonUtil.toJson(response, jsonMap);

        return NONE;

      }

      // 根據(jù)操作狀態(tài)、消息內(nèi)容輸出AJAX

      protected String ajax(Status status, String message) {

        HttpServletResponse response = initResponse(HEADER_JSON_CONTENT_TYPE);

        Map<String, String> jsonMap = new HashMap<String, String>();

        jsonMap.put(STATUS_PARAMETER_NAME, status.toString());

        jsonMap.put(MESSAGE_PARAMETER_NAME, message);

        JsonUtil.toJson(response, jsonMap);

        return NONE;

      }

      // 根據(jù)Object輸出AJAX

      protected String ajax(Object object) {

        HttpServletResponse response = initResponse(HEADER_JSON_CONTENT_TYPE);

        JsonUtil.toJson(response, object);

        return NONE;

      }

      // 根據(jù)boolean狀態(tài)輸出AJAX

      protected String ajax(boolean booleanStatus) {

        HttpServletResponse response = initResponse(HEADER_JSON_CONTENT_TYPE);

        Map<String, Object> jsonMap = new HashMap<String, Object>();

        jsonMap.put(STATUS_PARAMETER_NAME, booleanStatus);

        JsonUtil.toJson(response, jsonMap);

        return NONE;

      }

      private HttpServletResponse initResponse(String contentType) {

        HttpServletResponse response = ServletActionContext.getResponse();

        response.setContentType(contentType + ";charset=" + HEADER_ENCODING);

        if (HEADER_NO_CACHE) {

          response.setDateHeader("Expires", 1L);

          response.addHeader("Pragma", "no-cache");

          response.setHeader("Cache-Control", "no-cache, no-store, max-age=0");

        }

        return response;

      }

    這樣前臺(tái)所需要的數(shù)據(jù),就從庫(kù)里取出,并封裝成了對(duì)應(yīng)的Json。

    接下來(lái)就是前臺(tái)的實(shí)現(xiàn)了,前臺(tái)需要導(dǎo)入的js和css如下:

    <link rel="stylesheet" href="${base}/template/ztree/css/demo.css" type="text/css">

    <link rel="stylesheet" href="${base}/template/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css">

    <script type="text/javascript" src="${base}/template/ztree/js/jquery.ztree.core.js"></script>

    這里只有demo.css是自己添加的,其他都是官方制定的,demo.css是將官方的demo用到的css修改的,如下(這里有冗余樣式?jīng)]有刪除掉);

    div.content_wrap {width: 400px;}

    div.content_wrap div.left{float: left;}

    div.content_wrap div.right{float: right;width: 340px;}

    div.zTreeDemoBackground {text-align:left;}

    ul.ztree {margin-top: 10px;border: 1px solid #617775;background: #fefefe;width:220px;height:360px;overflow-y:scroll;overflow-x:auto;}

    ul.log {border: 1px solid #617775;background: #f0f6e4;width:300px;height:170px;overflow: hidden;}

    ul.log.small {height:45px;}

    ul.log li {color: #666666;list-style: none;padding-left: 10px;}

    ul.log li.dark {background-color: #E3E3E3;}

    /* ruler */

    div.ruler {height:20px; width:220px; background-color:#f0f6e4;border: 1px solid #333; margin-bottom: 5px; cursor: pointer}

    div.ruler div.cursor {height:20px; width:30px; background-color:#3C6E31; color:white; text-align: right; padding-right: 5px; cursor: pointer}

    然后,就是對(duì)應(yīng)的下拉框:

    <div class="content_wrap">

      <div class="zTreeDemoBackground left">

         <input id="citySel" class="formText" type="text" onclick="showMenu(); return false;" readonly value="" style="width:150px;"/>

         <input id="treeids" type="hidden" name="goods.goodsCategory.id" >

         <input type="button" onclick="showMenu();" value="∨">

      </div>

    </div>

     8<div id="menuContent" class="menuContent" style="display:none; position: absolute;">

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

    </div>

    這里有一個(gè)隱藏的文本框用來(lái)存放下拉框選擇內(nèi)容對(duì)應(yīng)的id。

    對(duì)應(yīng)的腳本如下:

    <SCRIPT type="text/javascript">

        var setting = {

          view: {

            dblClickExpand: false

          },

          data: {

            simpleData: {

              enable: true

            }

          },

          callback: {

            onClick: onClick

          },

          view: {

              // 不顯示對(duì)應(yīng)的圖標(biāo)

            showIcon: false

          }

        };

        function onClick(e, treeId, treeNode) {

          var zTree = $.fn.zTree.getZTreeObj("treeDemo"),

          nodes = zTree.getSelectedNodes(),

          v = "";

          ids = "";

          nodes.sort(function compare(a,b){return a.id-b.id;});

          for (var i=0, l=nodes.length; i<l; i++) {

            v += nodes[i].name + ",";

            ids += nodes[i].id + ",";

          }

          if (v.length > 0 ) v = v.substring(0, v.length-1);

          var cityObj = $("#citySel");

          cityObj.attr("value", v);

          // 將選中的id放到隱藏的文本域中

          if (ids.length > 0 ) ids = ids.substring(0, ids.length-1);

          var treeids = $("#treeids");

          treeids.attr("value", ids);

        }

        function showMenu() {

          var cityObj = $("#citySel");

          var cityOffset = $("#citySel").offset();

          $("#menuContent").css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast");

          $("body").bind("mousedown", onBodyDown);

        }

        function hideMenu() {

          $("#menuContent").fadeOut("fast");

          $("body").unbind("mousedown", onBodyDown);

        }

        function onBodyDown(event) {

          if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {

            hideMenu();

          }

        }

        var zNodes ;

        $(document).ready(function(){

           // 加載數(shù)據(jù)

          $.ajax({  

            async : false,  

            cache:false,  

            type: 'POST',  

            dataType : 'json',  

            url: '${base}/admin/goods!getGoodsCategoryTreeJson.action', 

            error: function () {

              alert('請(qǐng)求失敗');  

            },  

            success:function(data){ 

              zNodes = data; 

            }  

          }); 

          $.fn.zTree.init($("#treeDemo"), setting, zNodes);

           

        });

    </SCRIPT>

    這樣,一個(gè)下拉框就做完了。

    如下圖所示:

    名單

    如果,需要在修改頁(yè)面中回寫(xiě)相應(yīng)的下拉列表數(shù)據(jù),添加如下的腳本:

    <script type="text/javascript">

    $(document).ready(function(){

      if ("${goods.goodsCategory.id}"!="") {

        var treeObj = $.fn.zTree.getZTreeObj("treeDemo");

        var node = treeObj.getNodeByParam("id", "${goods.goodsCategory.id}" , null);

        treeObj.selectNode(node,false , false);

        onClick(event,"${goods.goodsCategory.id}",node,true);

      }

    });

    </script>

    以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)zTree插件有所幫助。

    更多信息請(qǐng)查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機(jī)網(wǎng)站地址:zTree插件下拉樹(shù)使用入門(mén)教程
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢(xún)回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢(xún)?yōu)闇?zhǔn)!

    2026上岸·考公考編培訓(xùn)報(bào)班

    • 報(bào)班類(lèi)型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢(xún) | 簡(jiǎn)要咨詢(xún)須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專(zhuān)用圖標(biāo)
    聯(lián)系電話(huà):0871-65099533/13759567129 獲取招聘考試信息及咨詢(xún)關(guān)注公眾號(hào):hfpxwx
    咨詢(xún)QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專(zhuān)用圖標(biāo)