再次:修改jquery.checktree的功能,添加默認選項

需要用到jQuery 的checktree插件,看了一下,但不支持默認選中的功能,還好javaeye的subwayline13同學改過了而且發表了主題:修改jquery.checktree的BUG,順便牢騷一下jquery插件的質量令人擔憂哇!!的文章,我下載下來看了一下,發現還有bug,弄完之後在發出來吧。

 

首先說一下,subwayline13同學改完之後的事情,首先他在那個文章裏面解決的css的問題是不對的,其實很簡單,只要將12行的ul.checktree li 的padding換成margin就基本差不多了,剩下的自己可以在根據需要調整一下。

 

然後就是改完成以後的腳本里面其實是有問題的,subwayline13同學改完之後,是可以用了。

問題是:

       如果選擇樹在初始化時,一個有子節點的節點,而它的子節點並不是全部選中的情況下,這樣這個節點本身初始化時應該就是選中的狀態,在點擊它的時候,他下面的子節點應該就全部成爲選中狀態,而自己本身也應該還是選中狀態。

 

而subwayline13同學,改完之後,並沒有改源程序裏的

            // Create the image for the checkbox next to the label
            var $checkbox = jQuery('<div class="checkbox"></div>');

            // When you click the checkbox, it should do the checking/unchecking
            $checkbox.click(function() {
                // Make the current class checked
                jQuery(this)

                // if it's half checked, its now either checked or unchecked
                    .removeClass("half_checked")
                    .toggleClass("checked")

                // Send a click event to the checkbox to toggle it as well
                    .siblings(":checkbox").click()                   //這裏有問題,不應該簡單的是click

                ;

 

我的解決辦法是刪除 .siblings(":checkbox").click()    ,然後放到下面的判斷裏面:

                // Check/uncheck children depending on our status.
                if (jQuery(this).hasClass("checked")) {
                    // Fire the check callback for this parent
                    if (settings.onCheck) settings.onCheck(jQuery(this).parent());

                    jQuery(this).siblings("ul").find(".checkbox").not(".checked")
                        .removeClass("half_checked")
                        .addClass("checked")
                        .each(function() {
                            if (settings.onCheck) settings.onCheck(jQuery(this).parent());
                        })
                        .siblings(":checkbox")
                            .attr("checked", "checked")
                    ;
                }
                else {
                    // Fire the uncheck callback for this parent
                    if (settings.onUnCheck) settings.onUnCheck(jQuery(this).parent());

                    jQuery(this).siblings("ul").find(".checkbox").filter(".checked")
                        .removeClass("half_checked")
                        .removeClass("checked")
                        .each(function() {
                            if (settings.onUnCheck) settings.onUnCheck(jQuery(this).parent());
                        })
                        .siblings(":checkbox")
                            .attr("checked", "")
                    ;
                }

 

 

改爲:

 

if (jQuery(this).hasClass("checked")) { //checkbox image
                                    if (settings.onCheck)
                                        settings.onCheck(jQuery(this).parent());
                                    jQuery(this).siblings(":checkbox").attr("checked", "checked"); //這裏是新加的
                                    jQuery(this) //checkbox image
                                            .siblings("ul")//checkbox image >ul
                                            .find(".checkbox")//checkbox image >ul >all checkbox image
                                            .not(".checked")
                                            .removeClass("half_checked")
                                            .addClass("checked")
                                            .each(function() {
                                                if (settings.onCheck)
                                                    settings.onCheck(jQuery(this).parent());
                                            }).siblings(":checkbox")//checkbox image >ul >all checkbox
                                            .attr("checked", "checked");
                                } else {
                                    // Fire the uncheck callback for this parent
                                    if (settings.onUnCheck)
                                        settings.onUnCheck(jQuery(this)
                                                .parent());
                                    jQuery(this).siblings(":checkbox").attr("checked", "");//這裏是新加的
                                    jQuery(this)
                                            .siblings("ul")
                                            .find(".checkbox")
                                            .filter(".checked")
                                            .removeClass("half_checked")
                                            .removeClass("checked")
                                            .each(function() {
                                                if (settings.onUnCheck)
                                                    settings.onUnCheck(jQuery(this).parent());
                                            }).siblings(":checkbox")
                                            .attr("checked", "");
                                }

 

這樣就ok了。如果還有人發現問題,希望通知我一聲呀。

 

 

弄成資源了,之前沒有列子,不好意思了,現在補充上.想要的下載吧。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章