切换没有特定链接的表行

时间:2013-10-04 18:05:59

标签: php jquery

我有一个包含一些行的表,有点难以向您展示所有代码,但我会举一个例子:

<tr id="<?=$row['slug'];?>" class="gradeX <? if($row['parent_name']!=''){?>child<?}?>">
<td id="<?=$row['id'];?>" class="expand-parent"><? if($row['parent_name']==''){?>+<?}?></td>
<td><span id="<?=$row['id'];?>" class="edit-title"><?=$row['title'];?></td>
<td><span id="<?=$row['id'];?>" class="edit-slug"><?=$row['slug'];?></td>
<td><span id="<?=$row['id'];?>" class="edit-parent"><? if($row['parent_name']){ ?><?=$row['parent_name'];?> (ID: <?=$row['parent'];?>)<? } ?></td>
<td id="<?=$row['id'];?>" class="edit-uri"><?=$row['uri'];?></td>
<td id="<?=$row['id'];?>" class="edit-perms"><?=$row['perms'];?></td>
<td>    
<div class="toggle-button" data-toggleButton-style-enabled="success">
<input type="checkbox" class="toggle-perm" id="<?=$row['id'];?>"<?=$cl;?> />
</div>
</td>
</tr>

这是一个循环,产生几百个“父”行,以及几千个“子”行。

我有一些看起来像这样的jquery:

$('.child').toggle();
            $('.expand-parent').click(function(){
                $(this).closest(".child").toggle();
            });

基本上我要做的是,一旦你触发.expand-parent点击功能,它就会切换(显示)属于被点击的父项的子项。告诉jQuery哪个tr个孩子属于每个tr父母并进行相应切换的最佳方法是什么?

修改

HTML示例输出:

                                    <!-- Table row -->
                    <tr id="pp" class="gradeX ">
                        <td id="1" class="expand-parent">+</td>
                        <td><span id="1" class="edit-title">Prospector</td>
                        <td><span id="1" class="edit-slug">pp</td>
                        <td><span id="1" class="edit-parent"></td>
                        <td id="1" class="edit-uri"></td>
                        <td id="1" class="edit-perms"></td>
                        <td>    
                            <div class="toggle-button" data-toggleButton-style-enabled="success">
                                <input type="checkbox" class="toggle-perm" id="1" checked />
                            </div>
                        </td>
                    </tr>
                    <!-- // Table row END -->

                                        <!-- Table row -->
                    <tr id="pp_data_providers" class="gradeX child">
                        <td id="2" class="expand-parent"></td>
                        <td><span id="2" class="edit-title">Data Providers</td>
                        <td><span id="2" class="edit-slug">pp_data_providers</td>
                        <td><span id="2" class="edit-parent">Prospector (ID: 1)</td>
                        <td id="2" class="edit-uri">/plugins/plg_prospector/prospect_providers.php</td>
                        <td id="2" class="edit-perms"></td>
                        <td>    
                            <div class="toggle-button" data-toggleButton-style-enabled="success">
                                <input type="checkbox" class="toggle-perm" id="2" checked />
                            </div>
                        </td>
                    </tr>
                    <!-- // Table row END -->

                                        <!-- Table row -->
                    <tr id="pp_provider_lists" class="gradeX child">
                        <td id="3" class="expand-parent"></td>
                        <td><span id="3" class="edit-title">Provider Lists</td>
                        <td><span id="3" class="edit-slug">pp_provider_lists</td>
                        <td><span id="3" class="edit-parent">Prospector (ID: 1)</td>
                        <td id="3" class="edit-uri">/plugins/plg_prospector/prospect_lists.php</td>
                        <td id="3" class="edit-perms"></td>
                        <td>    
                            <div class="toggle-button" data-toggleButton-style-enabled="success">
                                <input type="checkbox" class="toggle-perm" id="3" checked />
                            </div>
                        </td>
                    </tr>
                    <!-- // Table row END -->

                                        <!-- Table row -->
                    <tr id="pp_partners" class="gradeX child">
                        <td id="4" class="expand-parent"></td>
                        <td><span id="4" class="edit-title">Partners</td>
                        <td><span id="4" class="edit-slug">pp_partners</td>
                        <td><span id="4" class="edit-parent">Prospector (ID: 1)</td>
                        <td id="4" class="edit-uri">/plugins/plg_prospector/prospect_partners.php</td>
                        <td id="4" class="edit-perms"></td>
                        <td>    
                            <div class="toggle-button" data-toggleButton-style-enabled="success">
                                <input type="checkbox" class="toggle-perm" id="4" checked />
                            </div>
                        </td>
                    </tr>
                    <!-- // Table row END -->

                                        <!-- Table row -->
                    <tr id="pp_reporting" class="gradeX child">
                        <td id="5" class="expand-parent"></td>
                        <td><span id="5" class="edit-title">Reporting</td>
                        <td><span id="5" class="edit-slug">pp_reporting</td>
                        <td><span id="5" class="edit-parent">Prospector (ID: 1)</td>
                        <td id="5" class="edit-uri"></td>
                        <td id="5" class="edit-perms"></td>
                        <td>    
                            <div class="toggle-button" data-toggleButton-style-enabled="success">
                                <input type="checkbox" class="toggle-perm" id="5" />
                            </div>
                        </td>
                    </tr>
                    <!-- // Table row END -->

                                        <!-- Table row -->
                    <tr id="pp_campaigns" class="gradeX child">
                        <td id="6" class="expand-parent"></td>
                        <td><span id="6" class="edit-title">Campaigns</td>
                        <td><span id="6" class="edit-slug">pp_campaigns</td>
                        <td><span id="6" class="edit-parent">Prospector (ID: 1)</td>
                        <td id="6" class="edit-uri">/plugins/plg_prospector/prospect_campaigns.php</td>
                        <td id="6" class="edit-perms"></td>
                        <td>    
                            <div class="toggle-button" data-toggleButton-style-enabled="success">
                                <input type="checkbox" class="toggle-perm" id="6" checked />
                            </div>
                        </td>
                    </tr>
                    <!-- // Table row END -->

                                        <!-- Table row -->
                    <tr id="cbm" class="gradeX ">
                        <td id="7" class="expand-parent">+</td>
                        <td><span id="7" class="edit-title">CBM</td>
                        <td><span id="7" class="edit-slug">cbm</td>
                        <td><span id="7" class="edit-parent"></td>
                        <td id="7" class="edit-uri"></td>
                        <td id="7" class="edit-perms"></td>
                        <td>    
                            <div class="toggle-button" data-toggleButton-style-enabled="success">
                                <input type="checkbox" class="toggle-perm" id="7" checked />
                            </div>
                        </td>
                    </tr>
                    <!-- // Table row END -->

1 个答案:

答案 0 :(得分:1)

在有父项的行上,我会添加一个包含父ID的属性。

示例:

<tr id="cbm" class="gradeX" data-parentid="1">

然后你可以像这样编写jQuery事件:

var $this = $(this);
var id = $this.attr('id');
$(this).parents('table').find('tr[data-parentid='+id+']').toggle();

注意: parent('table')遍历table元素 find(..)找到属性data-parentid设置为id的所有tr元素。

拥有多个具有相同ID的元素是违法的,所以我会摆脱它们。最干净的方法是将它移动到tr元素。