简体   繁体   English

更改数据表中行的图标,不影响每行中的其他图标

[英]change icon at row in data-table with out affect other icon at each row

I have fa-square-o up the table and I used while loop to fill data-table from data-base in one column I have two icons of font awesome one is fa-star-o and another is fa-square-o. 我在表上放了fa-square-o,用while循环从一列中填充了数据库中的数据表。我有两个很棒的字体图标,一个是fa-star-o,另一个是fa-square-o。 I use jQuery to change icon on click. 我使用jQuery更改点击图标。 When i click in on icon is working probably but the other icon has same class in other row is hidden. 当我单击图标时可能正在工作,但其他行中具有相同类的其他图标被隐藏了。

This is HTML Code for square upper table 这是方桌的HTML代码

<li class="dropdown li-inv-h">
  <a class="dropdown-toggle" data-toggle="dropdown" href="#">
     <i class=" i-invo"><span class="caret"></span></i></a>
     <ul class="dropdown-menu">
       <li><a href="#">All</a></li>
       <li><a href="#">Nothing</a></li>
       <li><a href="#">Is reading</a></li>
       <li><a href="#">Not reading</a></li>
       <li><a href="#">Delete</a></li>
     </ul>
     <a href="#"><i class="fa fa-square-o" ></i></a>
</li>

This is PHP Code for column has Font Awesome in side While loop 这是专为While循环而在侧边具有Font Awesome的列的PHP代码

while($row = mysqli_fetch_assoc($qry)){
echo ' <tr>
<td class="col-lg-1">
   <span><i class="fa fa-square-o sqr" id="'.$row["id"].'"></i></span>
   <span class="fa-m-inv"><i class="fa fa-star-o str" id="'.$row["id"].'"></i></span>
</td>
<td class="col-lg-1">'.$row["name"].'</td>
</tr>

My java-script is 我的Java脚本是

$('.sqr').click(function(){
    if ($(this).hasClass("fa-square-o")) {
        $("i.fa-square-o").removeClass("fa-square-o");
        $(this).addClass("fa-check-square-o");
        $(this).css({'color':'blue'});
    }else{
        $("i.fa-square-o").removeClass("fa-check-square-o");
        $(this).addClass("fa-square-o");
        $(this).css({'color':'black'});
    }
});
$('.str').click(function(){
        //var id = $(this).attr("id");
    if ($(this).hasClass("fa-star-o")) {
        $("i.fa-star-o").removeClass("fa-star-o");
        $(this).addClass("fa-star");
        $(this).css({'color':'yellow'});
    }else{
        $("i.fa-star-o").removeClass("fa-star");
        $(this).addClass("fa-star-o");
        $(this).css({'color':'black'});
    }
});

T Ť

 $('.sqr').click(function(){ if ($(this).hasClass("fa-square-o")) { $("i.fa-square-o").removeClass("fa-square-o"); $(this).addClass("fa-check-square-o"); $(this).css({'color':'blue'}); }else{ $("i.fa-square-o").removeClass("fa-check-square-o"); $(this).addClass("fa-square-o"); $(this).css({'color':'black'}); } }); $('.str').click(function(){ if ($(this).hasClass("fa-star-o")) { $("i.fa-star-o").removeClass("fa-star-o"); $(this).addClass("fa-star"); $(this).css({'color':'red'}); }else{ $("i.fa-star-o").removeClass("fa-star"); $(this).addClass("fa-star-o"); $(this).css({'color':'black'}); } }); 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <li class="dropdown li-inv-h"> <a class="dropdown-toggle" data-toggle="dropdown" href="#"> <i class=" i-invo"><span class="caret"></span></i></a> <ul class="dropdown-menu"> <li><a href="#">Checked</a></li> <li><a href="#">Unchecked</a></li> <li><a href="#">Reading</a></li> <li><a href="#">Unreading</a></li> <li><a href="#">Delete</a></li> </ul> <a href="#"><i class="fa fa-square-o" ></i></a> </li> <table id="data-table" class="table table-hover"> <thead> <tr> <th></th> <th>User</th> </tr> </thead> <tbody> <tr> <td class="col-lg-1"> <span> <i class="fa fa-square-o sqr"></i> </span> <span> <i class="fa fa-star-o str"></i> </span> </td> <td>Name1</td> </tr> <tr> <td class="col-lg-1"> <span> <i class="fa fa-square-o sqr"></i> </span> <span> <i class="fa fa-star-o str"></i> </span> </td> <td>Name2</td> </tr> <tr> <td class="col-lg-1"> <span> <i class="fa fa-square-o sqr"></i> </span> <span> <i class="fa fa-star-o str"></i> </span> </td> <td>Name3</td> </tr> <tr> <td class="col-lg-1"> <span> <i class="fa fa-square-o sqr"></i> </span> <span> <i class="fa fa-star-o str"></i> </span> </td> <td>Name4</td> </tr> </tbody> </table> 

The problem for me not in php code but in java-script via font-icon change, When i click in one icon in row all other icon in other row is effective. 对我来说,不是在PHP代码中而是通过字体图标更改在Java脚本中出现的问题,当我单击行中的一个图标时,另一行中的所有其他图标均有效。 that mean i have one empty star in each row when i click this star is remove from this row and com other star fill by yellow color but in my code when i click star all other star in each row is remove also. 这意味着当我单击该星时,每行中都有一个空星,并从该行中删除另一颗星,并用黄色填充其他星,但是在我的代码中,当我单击星时,每行中的所有其他星也都被删除。

I want Fix this problem, also i want when any class has fa-check-square-o change fa-icon upper table to fa-minus-square and add id of row to array to make one event choice like delete. 我想修复此问题,我也想在任何类都具有fa-check-square-o的情况下将fa-icon上表更改为fa-minus-square,并将行的id添加到数组以做出一个删除之类的事件选择。

I read more post and not found post nearly this 我读了更多帖子,却没有找到差不多的帖子

Thank you every one. 谢谢大家。

Change $("i.fa-star-o").removeClass("fa-star-o"); 更改$("i.fa-star-o").removeClass("fa-star-o"); To

$(this).removeClass("fa-star-o");

 $('.sqr').click(function(){ if ($(this).hasClass("fa-square-o")) { $(this).removeClass("fa-square-o"); $(this).addClass("fa-check-square-o"); $(this).css({'color':'blue'}); }else{ $(this).removeClass("fa-check-square-o"); $(this).addClass("fa-square-o"); $(this).css({'color':'black'}); } }); $('.str').click(function(){ if ($(this).hasClass("fa-star-o")) { $(this).removeClass("fa-star-o"); $(this).addClass("fa-star"); $(this).css({'color':'red'}); }else{ $(this).removeClass("fa-star"); $(this).addClass("fa-star-o"); $(this).css({'color':'black'}); } }); 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <li class="dropdown li-inv-h"> <a class="dropdown-toggle" data-toggle="dropdown" href="#"> <i class=" i-invo"><span class="caret"></span></i></a> <ul class="dropdown-menu"> <li><a href="#">Checked</a></li> <li><a href="#">Unchecked</a></li> <li><a href="#">Reading</a></li> <li><a href="#">Unreading</a></li> <li><a href="#">Delete</a></li> </ul> <a href="#"><i class="fa fa-square-o" ></i></a> </li> <table id="data-table" class="table table-hover"> <thead> <tr> <th></th> <th>User</th> </tr> </thead> <tbody> <tr> <td class="col-lg-1"> <span> <i class="fa fa-square-o sqr"></i> </span> <span> <i class="fa fa-star-o str"></i> </span> </td> <td>Name1</td> </tr> <tr> <td class="col-lg-1"> <span> <i class="fa fa-square-o sqr"></i> </span> <span> <i class="fa fa-star-o str"></i> </span> </td> <td>Name2</td> </tr> <tr> <td class="col-lg-1"> <span> <i class="fa fa-square-o sqr"></i> </span> <span> <i class="fa fa-star-o str"></i> </span> </td> <td>Name3</td> </tr> <tr> <td class="col-lg-1"> <span> <i class="fa fa-square-o sqr"></i> </span> <span> <i class="fa fa-star-o str"></i> </span> </td> <td>Name4</td> </tr> </tbody> </table> 

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

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