1

I have a treeview which is implemented using jquery Treeview version 1.4. TreeView is Implemented as below

<div id="container">
   <ul id="outerlist">
      <li>
         <span>level-1</span>
         <ul>
            <li>
               <span>level-2</span>
               <ul>
                  <li>
                     <input type="radio" name="lastnode" value="125" onclick="somefunction()" />
                     <span>lastnodetext</span>
                  </li>
               </ul>
            </li>
         </ul>
      </li>
   </ul>
</div>

What i need is ,onload the lastnode radiobutton to be checked and all its parent nodes to be expanded (note :there are multiple levels and radiobuttons in the tree).

Below is the code that i have used acheive this

$('#container').find(':radio').each(function (index, value) {
var $this = $(this);
if ($this.val() != undefined && $this.val() == "125") {
    $this.attr('checked', 'checked');
    $(this).parents('ul').show();
    return false;
}});

This code works perfectly fine in terms of selecting the radiobutton and expanding the level-2 and level-1 nodes. Although the Image (+ or -) does not change. How can i achieve this

Thanks in Advance!!

4

1 回答 1

1

我浏览了树视图文档,发现您只需要添加class="open"需要li打开的即可。这将处理所有其他事情,例如添加图像等。

此外,您正在对单个单选按钮进行操作,因此不需要全部迭代然后选择您想要的一个。相反,使用 jQuery 选择器只获取您的单选按钮并对其进行操作。

使用下面的代码 -

HTML:将 css 类添加到您的第一级和第二li

<div id="container">
   <ul id="outerlist">
      <li class="firstLevel">
         <span>level-1</span>
         <ul>
            <li class="secondLevel">
               <span>level-2</span>
               <ul>
                  <li>
                     <input type="radio" name="lastnode" value="125" onclick="somefunction()" />
                     <span>lastnodetext</span>
                  </li>
               </ul>
            </li>
         </ul>
      </li>
   </ul>
</div>

jQuery : 找到第一级和第二级 li 来添加openCSS 类。

var $radio = $('#container input[type="radio"][value="125"]');
$radio.attr('checked',true);
$radio.closest('.firstLevel').addClass('open');
$radio.closest('.secondLevel').addClass('open');
于 2015-04-06T06:40:27.797 回答