Javascript-форум (https://javascript.ru/forum/)
-   jQuery (https://javascript.ru/forum/jquery/)
-   -   JSTREE как получить id parent????? (https://javascript.ru/forum/jquery/23560-jstree-kak-poluchit-id-parent.html)

dsnj 28.11.2011 00:31

JSTREE как получить id parent?????
 
<body>

<div id="wrap">
    <div id="header">header</div>
    <div id="main">
        <div id="demo1" style="float:left;width:50%;">
            <ul>
                <li>
                    <a class="aa" id='a' href="">aaa</a>
                    <!-- UL node only needed for children - omit if there are no children -->
                    <ul>
                        <li><a class="ab" id="a_1" href="#"> bbb</a></li>
                        <li><a class="ab" id="a_2" href="#"> ccc</a></li>
                    </ul>
                </li>
            </ul>

        </div>
        <div id="content">www</div>
    </div>
</div>

<div id="footer">
    footer
</div>

<script type="text/javascript">
$(function () {
    $("#demo1").jstree({
        "themes": {
        "theme": "default",
        "dots": true,
        "icons": true,
      },

        "plugins" : [ "themes", "html_data", "ui" ]
    });
   $('.ab').click(function(){
            var id_ab = $(this).attr('id');
            var id_aa = $(this).parent().attr('id');
            $('#content').html('ab \'s content, ID_ab = ' + id_ab + ' ID_aa = ' + id_aa); 
        })    
            
});
</script>


</body>


В РЕЗУЛЬТАТЕ получаю след. контент: 'ab 's content, ID_ab = a_1 ID_aa = undefined

ПОМОГИТЕ В ПОЛУЧЕНИИ ID родителя элемента!!!

ЗАРАНЕЕ СПАСИБО!!!

melky 28.11.2011 00:38

$(this).parent("div").attr("id");

dsnj 28.11.2011 00:40

Цитата:

$(this).parent("div").attr("id");
попробовал вставить "div" - результат не поменялся...(

melky 28.11.2011 00:43

какой id должен быть на выходе ?

dsnj 28.11.2011 00:44

id='a'

melky 28.11.2011 00:48

Цитата:

Сообщение от dsnj (Сообщение 139397)
ПОМОГИТЕ В ПОЛУЧЕНИИ ID родителя элемента!!!

O_o так это не родитель.

как-то так.
$(this).parent("ul").parent('li').find('a:first[id]').attr('id')

dsnj 28.11.2011 00:52

а что
find('a:first[id]')
делает?? опять результат UNDEFINED

melky 28.11.2011 00:54

ищет первую <a> , у которой есть атрибут id.

dsnj 28.11.2011 00:57

опять результат UNDEFINED

melky 28.11.2011 00:58

тьфу. с просонья букву забыл
$(this).parent*!*s*/!*("ul").parent('li').find('a:first[id]').attr('id');

dsnj 28.11.2011 01:04

ещё вопрос, а если будет несколько узлов класса аа (class="aa") будет ли работать
find
корректно или нужно др. параметры указывать?

melky 28.11.2011 01:15

он выберет первую. если нужно подправить - править селектор.

dsnj 28.11.2011 01:34

Цитата:

он выберет первую. если нужно подправить - править селектор.
Можно чуть подробнее - новичок я

melky 28.11.2011 09:42

http://jquery.page2page.ru/index.php5/Селекторы

dsnj 28.11.2011 11:03

я посмотрел инфу по ссылке но ничего не получилось подходящее найти... Окажите помощь! дерево примерно такое (строится по полученным таблицам из БД):

<div class="panel">

<div id="tree" class="tree">
<ul>
<?php
foreach($courses as $course_row)
{
?>
        <li><a class="course" id="<?=$course_row->course_id?>" href="#"><?echo $course_row->course_title?></a>
        <ul>
<?      foreach($section_to_course as $row_StC)
        {
            if($course_row->course_id == $row_StC->course_id)
            {
                foreach($sections as $section_row)
                    if($section_row->section_id == $row_StC->section_id)
                    {
?>
                        <li><a class="section" id="<?=$section_row->section_id?>" href="#"><?echo $section_row->section_title?></a>
                        <ul>
<?                      foreach($test_to_section as $row_TtS)
                        {
                            if($section_row->section_id == $row_TtS->section_id)
                            {
                                foreach($tests as $test_row)
                                    if($test_row->test_id == $row_TtS->test_id)
                                    {
?>
                                    <li><a class="test" id="<?=$test_row->test_id?>" href="#"><?echo $test_row->test_title?></a>
                                    </li>
<?                      
                                    }
                            }
                        }
?>
                        </ul>
                        </li>
                        
<?
                    }
            }
        }
?>
        </ul>
        </li>
<?
}
?> 
</ul>
</div>

<div id="content">www</div>

</div>


в результате рекомендованных и введённых обработок событий:

script type="text/javascript" class="source">
    $(function () {
	$("#tree")
    .jstree({ 
		"plugins" : [ "themes", "html_data", "ui", "crrm", "contextmenu", "dnd", "hotkeys", "json_data" ]
	})
    
    $('.course').click(function(){
            var id_course = $(this).attr('id');
            $('#content').html('course \'s content, ID_course = ' + id_course);
            //return false;
        })
    $('.section').click(function(){
            var id_section = $(this).attr('id');
            var id_course = $(this).parents("ul").parent('li').find('a:[id]').attr('id');
            $('#content').html('section \'s content, ID_section = '+ id_section + ' ID_course = ' + id_course);
            //return false;
        })
    $('.test').click(function(){
            var id_test = $(this).attr('id');
            var id_section = $(this).parents("ul").parent('li').find('a:[id]').attr('id');
            //$('#content').html('course \'s content');
            $('#content').html('test \'s content, ID_test = ' + id_test + ' ID_section = ' + id_section);
            //return false;
        })     
    });
</script>





РЕЗУЛЬТАТ:
для section идентификаторы родительской вкладки - вроде определяются...
а вот для test идентификаторы родительской вкладки работают некорректно - судя по всему, они принимают значение идентификатора родительской вкладки для section.

Как подправить то??? может с деревом самим что-то не так???


Часовой пояс GMT +3, время: 02:51.