简体   繁体   English

单击按钮时仅显示表数据的$(this)行

[英]Display only $(this) row of table data when button is clicked

I am working on a drop down option feature within a table that loads its data from a mysql database. 我正在处理从MySQL数据库加载其数据的表中的下拉选项功能。 When a user clicks a button that's within a row it displays table data that was previously hidden. 当用户单击行中的按钮时,它将显示以前隐藏的表数据。 It should only display the data in the row below it but instead it applies this to all rows in the table with class $(.options). 它仅应在其下面的行中显示数据,而应将其应用于表中具有类$(。options)的所有行。 The goal is to only apply this to the row under the row that contains .button. 目标是仅将其应用于包含.button的行下的行。 This is what I have so far: 这是我到目前为止的内容:

CSS: CSS:

.options
{
display:none;   
}

MySql Table PHP): MySql表PHP):

while($sound=mysql_fetch_assoc($records)){
    echo "<tbody>";
    echo "<tr>";
    echo "<td width='40' class='player'>&nbsp;&nbsp;<a href='beats/".$sound['downloadlink']."' class='sm2_button'>Play/</a></td>";
    echo '<td width="250" class="name">'.$sound['name'].'&nbsp;&nbsp;&nbsp;<span class="red date">'.$sound['date'].'</span></td>';
    echo "<td width='88' class='bpm'>".$sound['bpm']." B.P.M.</td>";
    echo "<td width='72' class='length'>".$sound['length']."</td>";
    echo "<td width='275' class='keywords'>".$sound['keywords']."</td>";
    echo "<td width='96' class='buy'><img class='button' src='99cents.png'/></td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td  height='100' class='options' colspan='1'></td>";
    echo "<td class='options' colspan='1'>mp3</td>";
    echo "<td class='options' colspan='2'>wav</td>";
    echo "<td class='options' colspan='2'>tracked out</td>";
    echo "</tr>";
    echo "</tbody>";

Jquery Function: jQuery函数:

    $(".button").on('click', function(){
        $('.options').css('display', function(i,v){return v=='none' ? 'inline' : 'none' });
    });

You can use such code in your jQuery function: 您可以在jQuery函数中使用以下代码:

$(".button").on('click', function(){
    // use $(this) to get the element that was clicked. In this case it will be img element
    $(this)
       // find a parent of type tr, that is a row
       .parents('tr')
       // find the next row
       .next('tr')
       // manipulate the item
       .css('display', function(i,v){return v=='none' ? 'inline' : 'none' });
});

Or since you are doing only show / hide manipulation, instead of your .css() function you can use toggle() function 或者因为您只执行显示/隐藏操作,而不是.css()函数,所以可以使用toggle()函数

After messing around with the help from Fisherman and dotnetom I was able to get it working. 在渔民和dotnetom的帮助下弄乱之后,我得以使其工作。 here is what it looks like: 这是它的样子:

Table: 表:

    echo "<tbody>";
echo "<tr>";
echo "<td width='40' class='player'>&nbsp;&nbsp;<a href='beats/".$sound['downloadlink']."' class='sm2_button'>Play/</a></td>";
echo '<td width="250" class="name">'.$sound['name'].'&nbsp;&nbsp;&nbsp;<span class="red date">'.$sound['date'].'</span></td>';
echo "<td width='88' class='bpm'>".$sound['bpm']." B.P.M.</td>";
echo "<td width='72' class='length'>".$sound['length']."</td>";
echo "<td width='275' class='keywords'>".$sound['keywords']."</td>";
echo "<td width='96' class='buy'><img class='button' src='99cents.png'/></td>";
echo "</tr>";
echo "<tr>";
echo "<td style='display:none;' height='100' colspan='6'></td>";
echo "</tr>";
echo "</tbody>";

J query: J查询:

$(".button").on('click', function(){
$(this).parents('tr').next('tr').find('td').toggle('display', function(i,v){return v=='none' ? 'inline' : 'none' });

}); });

Thanks! 谢谢! (the toggle animation effect looks dope) (切换动画效果看起来像涂料)

Because you need to toggle only one row of the button so , you need to assign id for the button also for the row you want to toggle. 由于您只需要切换按钮的一行,因此,您还需要为要切换的行分配按钮ID。 In Your php file . 在您的php文件中。

$i = 0;   
 while($sound=mysql_fetch_assoc($records)){
        echo "<tbody>";
        echo "<tr>";
        echo "<td width='40' class='player'>&nbsp;&nbsp;<a href='beats/".$sound['downloadlink']."' class='sm2_button'>Play/</a></td>";
        echo '<td width="250" class="name">'.$sound['name'].'&nbsp;&nbsp;&nbsp;<span class="red date">'.$sound['date'].'</span></td>';
        echo "<td width='88' class='bpm'>".$sound['bpm']." B.P.M.</td>";
        echo "<td width='72' class='length'>".$sound['length']."</td>";
        echo "<td width='275' class='keywords'>".$sound['keywords']."</td>";
        echo "<td width='96' class='buy'><img class='button' data-index='$i' src='99cents.png'/></td>";
        echo "</tr>";
        echo "<tr id='row-$i'>";
        echo "<td  height='100' class='options' colspan='1'></td>";
        echo "<td class='options' colspan='1'>mp3</td>";
        echo "<td class='options' colspan='2'>wav</td>";
        echo "<td class='options' colspan='2'>tracked out</td>";
        echo "</tr>";
        echo "</tbody>";
    $i++;
    }

See i assign a data-index to track next row id. 请参阅我分配data-index以跟踪下一行ID。 now in your js file 现在在您的js文件中

$(".button").on('click', function(){
       $('#row-'+$(this).data('index')).toggle();
    });

Hope This work 希望这项工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM