ztreeDeptSelect 基於jquery和ztree的部門選擇插件

来源:http://www.cnblogs.com/hangwei/archive/2016/01/22/5116831.html
-Advertisement-
Play Games

插件介紹首先我們來看插件的功能演示(效果):插件準備好後。前臺只需編寫html:然後在javascript中渲染插件(代碼使用jQuery寫法):$(".deptName").ztreeDeptSelect();插件即渲染完成。此插件已發佈至github,源碼地址:https://github.co...


插件介紹

首先我們來看插件的功能演示(效果):

插件準備好後。前臺只需編寫html:

<input type="text" class="deptName" />

然後在javascript中渲染插件(代碼使用jQuery寫法):

$(".deptName").ztreeDeptSelect();

插件即渲染完成。

 

此插件已發佈至github,源碼地址: https://github.com/harveyhang/ztreeDeptSelect

需要的朋友,請直接從github下載源碼。

後臺請求數據使用ASP.NET完成,基於.NET Framework4.0。插件將會不斷的完善中,第一次分享代碼至github,希望園友們支持~

使用詳解

1.修改代碼獲取部門數據來源,即重新組織DeptDialogDataHandler.ashx的代碼。源代碼中,.ashx只是示例,是靜態數據,是假定查詢結果返回的數據;

   通常情況下部門數據來自資料庫,所以請根據實際項目來更改獲取部門數據的代碼。

2.部門樹依賴於zTree(一款部門樹插件),插件依賴於jQuery。所以請確保項目中有jquery和zTree的文件。具體請參考demo文件夾下的index.html文件和

   src文件夾下的deptDialog.html文件。

3.目前插件提供三種使用方法:簡單調用,設置預設值,設置參數 ;三種使用方法在demo\index.html都有說明。

部門數據的來源

載入部門樹的前臺代碼:

$(document).ready(function () {
            var zNodes;
            //assign root data to zNodes.
            $.ajax({
                url: 'DeptDialogDataHandler.ashx',
                type: 'post',
                dataType: 'json',
                async: false,
                data: { 'ajaxMethod': 'GetRootData' },
                success: function (data) {
                    zNodes = data;
                }
            });

            //tree setting
            var setting = {
                async: {
                    enable: true, 
                    url: "DeptDialogDataHandler.ashx",
                    autoParam: ["id"], // Parameters of the parent node attribute that is automatically committed when the asynchronous load is loaded
                    otherParam: ["ajaxMethod", 'AsyncCurrentNodeChildren'], //ajax request parameters
                    type: 'post',
                    dataType: 'json'
                },
                view: {
                    showIcon: true,
                    dblClickExpand: false
                },
                showLine: true, // show ztree connect line
                data: {  //Using pId to identify the relationship between father and son 
                    simpleData: {
                        enable: true
                    }
                },
                callback: { //callback function.
                    onDblClick: zTreeOnDblClick,
                    asyncSuccess: zTreeOnAsyncSuccess
                }
            };
            //init ztree.
            $.fn.zTree.init($("#treeDemo"), setting, zNodes);
        });

handler文件的後臺代碼:

<%@ WebHandler Language="C#" Class="DeptDialogDataHandler" %>

using System;
using System.Web;
using Newtonsoft.Json;

/// <summary>
/// Ajax Data Handler.
/// Author: https://github.com/harveyhang
/// Tips:Modify code to apply your own business...
/// </summary>
public class DeptDialogDataHandler : IHttpHandler {
    public void ProcessRequest (HttpContext context) {
        //static data for test,this is the depts data...
        var depts = new[]{ 
            new {deptId = 0, deptName = "All Depts", parentDeptId = -1 },
            new {deptId = 1, deptName = "Technology Dept", parentDeptId = 0 },
            new {deptId = 2, deptName = "Software Dept", parentDeptId = 1 },
            new {deptId = 3, deptName = "Hardware Dept", parentDeptId = 1 },
            new {deptId = 4, deptName = "Human Resource Dept", parentDeptId = 0 } 
        };
        //convert data type to ztree 
        //... convert process is omitted
        /**
         * data convert instruction:
         * the previous code section has three common properties:deptId,deptName,parentDeptId ,
         * which was converted to these properties under:        id    ,name    ,pId
         * if department node has child,set isParent=true;else,set isParent=false;
         **/
        var zTreeDepts = new[] { 
            new {id = 0, name = "All Depts", pId = -1, isParent=true },
            new {id = 1, name = "Technology Dept", pId = 0, isParent=true },
            new {id = 2, name = "Software Dept", pId = 1, isParent=false },
            new {id = 3, name = "Hardware Dept", pId = 1, isParent=false },
            new {id = 4, name = "Human Resource Dept", pId = 0, isParent=false } 
        };
        
        try
        {
            string ajaxMethod = context.Request["ajaxMethod"].ToString();//get ajax method
            System.Reflection.MethodInfo method = this.GetType().GetMethod(ajaxMethod);
            if (method != null)
                method.Invoke(this, new object[] { context });//execute method
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            context.Response.End();
        }
    }
 
    /// <summary>
    /// async load children
    /// </summary>
    /// <param name="context"></param>
    public void AsyncCurrentNodeChildren(HttpContext context)
    {
        try
        {
            int id = int.Parse(context.Request.Params["id"]);
            var childrenData = new[] { 
                new {id = 1, name = "Technology Dept", pId = 0, isParent=true },
                new {id = 4, name = "Human Resource Dept", pId = 0, isParent=false } 
            };
            switch(id)
            {//suppose the data was getted
                case 0:
                    break;
                case 1:
                    childrenData = new[] { 
                        new {id = 2, name = "Software Dept", pId = 1, isParent=false },
                        new {id = 3, name = "Hardware Dept", pId = 1, isParent=false }
                    };
                    break;
                case 2:
                case 3:
                case 4:
                    childrenData = null;
                    break;
            }
            context.Response.Write(JsonConvert.SerializeObject(childrenData));
        }
        catch (Exception)
        {
            throw;
        }
    }
    
    /// <summary>
    /// root data
    /// </summary>
    /// <param name="context"></param>
    public void GetRootData(HttpContext context)
    {
        try
        {
            //suppose the data was getted
            var root = new { id = 0, name = "All Depts", pId = -1, isParent = true };
            context.Response.Write(JsonConvert.SerializeObject(root));
        }
        catch (Exception)
        {
            throw;
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}
View Code

此部分可以改為其它網頁語言,如php,java等。因為插件是前臺通用的 ,後臺數據可以根據實際需要定製。

總結

目前此插件尚有一個未修複的bug:“用Google Chrome瀏覽器時,部門視窗無法彈出”。因Chrome已阻止了js的showModalDialog方法。

IE或火狐等主流瀏覽器尚未發現有bug。當然,歡迎廣大園友批評指正。

由於是上github的項目,代碼註釋全部為本人"手寫"的英文。本人英語水平一般,這蹩腳的英語,,,還望諸位見諒~

如果各位覺得github上的英文描述不清晰,或者您對此插件有任何建議,歡迎留言一起交流~

最後,

希望本文對你有幫助。 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 下麵,我們給大家提供了一個用於HTML5開發的各種用途的JavaScript庫列表。這些框架能夠給前端開發人員提供更好的功能實現的解決方案。如果你有收藏優秀的框架,也可以在後面的評論中分享給我們。1.EasyStar.js如果構建HTML5游戲或其他互動內容,那麼一定要檢查outEasyStar.j...
  • <h2 CSS 偽類 (Pseudo classes) 實例</h2 <h3 CSS 實例</h3 CSS 背景實例 CSS 文本實例 CSS 字體(font)實例 CSS 邊框(border)實例 CSS 外邊距 (margin) 實例 CSS 內邊距 (padding) 實例 CSS 列表實例....
  • 一.highcharts簡介以及引入 highcharts作為免費提供給個人學習、個人網站和非商業用途使用的前端圖表演示插件的確使用起來十分方便和輕便。在我最近完成一個需求的時候用到了它, 它的相容性也很強,其在標準(W3C標準)瀏覽器中使用SVG技術渲染圖形,在遺留的IE瀏覽器中使用VML技術來....
  • Form Plugin API 里提供了很多有用的方法可以讓你輕鬆的處理表單里的數據和表單的提交過程。測試環境:部署到Tomcat中的web項目。一、引入依賴js 二、初始化回調函數。首先,我們初始化這個表單,給它一個beforeSubmit回調函數 - 這是一個用來校驗的函數。$(docum...
  • JS中處理相容的方式總結:1、使用try、catch來處理相容 ->原理:在try中讓JS代碼執行,如果執行過程中報錯了,說明不相容,我們在catch中用其他的方式來實現我們的相容處理 ->前提:不相容的情況下執行報錯才可以 ->弊端:不管當前的瀏覽器是否相容,都要先把try中的代碼執行一遍,...
  • 在AngularJS中,有時候需要監視Scope中的某個變數,因為變數的改變會影響一些界面元素的顯示。有時,也希望通過jQuery調用Scope的某個方法。比如以下場景: jQ Button Toggle jQ button state Counter: {{counter}}以...
  • 好吧,正則表達式,我從來沒記過。以前要用的時候都是網上Copy一下的。這裡還是扯一下吧,以後要是有要用到的正則表達式那麼就收集到這個帖子里。(儘管我認為不會,因為我根本就不是一個專業的前端,我只是來划下水的\(^o^)/)應用範圍:正則表達式主要應用於對字元串中的信息實現查找,替換和提取操作。可處理...
  • 第一章HTML 基礎1.html 的基本結構?解析:2.HTML 全稱Hyper Text Markup Language(超文本標記語言)擴展XML:Extendsible Markup Language(可擴展性標記語言)3.高級記事本Ue(UltraEdit)nodepad++Editplus...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...