使用PHP在升序降序日期之间切换onclick

时间:2017-07-20 10:24:32

标签: javascript php jquery css-tables usort

我遇到了这个问题。我正在尝试创建一个函数,我可以按表中的日期对结果进行排序。到目前为止我得到了这个:

function wedstrijdenClub (){

    $laMatches = WaterpoloAPI::call("Matches", "getMatches", Array(
        "",
        "",
        isset($_GET["ClubId"]) ? $_GET["ClubId"] : "",
        "",
        "",

    ));

        $asc = $laMatches;
        $desc = $laMatches ;

        // Sort Matches ascending
       usort($desc, function($a, $b) {
                       return stringToUnix($a->Date) - stringToUnix($b->Date);
       });


       // Sort Matches descending

       usort($asc, function($a, $b) {
                       return stringToUnix($b->Date) - stringToUnix($a->Date);
       }); 

    echo '<div class="asc">'.implode('<br />',$asc).'</div>' ;
   echo '<div class="desc">'.implode('<br />',$desc).'</div>' ;

    echo "<h6 id='rcorners' style='background-color:#3db7e4; padding: 1rem; color:white;'><strong>Wedstrijden</strong></h6>";
    echo "<table class='hover'>";
    echo "<tbody >";

    $lnToday = strtotime(date("d-m-Y"));                          
                          $lcCurrent = "";                        
       foreach($laMatches as $loMatch) {
           if(stringToUnix($loMatch->Date) < $lnToday) {
                if($lcCurrent != $loMatch->Date) {     



echo "<thead>";
echo "<tr >";
echo "<th class='text-center date'>";
echo "$loMatch->Date</th>";
echo "<th class='text-center'></th>";      
echo "<th class='text-center'></th>";
echo "<th class='text-center'></th>";
echo "</tr>";               



echo "</tr>
          </thead>";           
                }               
                $lcCurrent = $loMatch->Date;           
           }
    echo "<tr class='text-center'>";
        echo "<td >$loMatch->Time</td>";
        echo "<td>$loMatch->HomeTeam </td>";
        echo "<td><strong><a href='..\wedstrijd?MatchId=".$loMatch->Id."'>$loMatch->ResultHome - $loMatch->ResultGuest </a></strong></td>";
        echo "<td> $loMatch->AwayTeam</td>";
    echo "</tr>";   
    }

    echo "</tbody>";
    echo "</table>";



}

在我的php页面

<style> div.asc, { display:block ; } div.desc { display:none ; } </style>
    <div class="large-12 columns block asc"><?php wedstrijdenClub(); ?></div>
                <div class="large-12 columns block desc"><?php wedstrijdenClub(); ?></div>

                <a class="asc">Asc</a>
 <a class="desc">Desc</a>

并在我的页脚

<script>
jQuery(document).on('click','a.asc, a.desc',function(e){
e.preventDefault() ;
jQuery('div.block').hide() ;
jQuery('div.'+jQuery(this).attr('class')).show() ;
}) ;
</script>

我在哪里拨打比赛清单。它下降和上升...但我想在点击事件上...我将如何做到这一点?我还在学习......如果这是一个愚蠢的问题,那就很抱歉。

1 个答案:

答案 0 :(得分:0)

我认为你需要的JS部分非常简单:

jQuery(document).on('click','a.asc, a.desc',function(e){
e.preventDefault() ;
jQuery('div.block').hide() ;
jQuery('div.'+jQuery(this).attr('class')).show() ;
}) ;
div.asc, { display:block ; }
div.desc { display:none ; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>

<div class="block asc">A B C D E F</div>
<div class="block desc">F E D C B A</div>

 <a class="asc">Asc</a>
 <a class="desc">Desc</a>

请注意你的php部分:当你使用usort函数时,$ laMatches的值会改变,所以调用2次usort将只显示最后的结果。

您需要复制$ laMatches并对副本进行排序:

 $laMatches = Array('red','blue','green','brown','yellow') ;

    $asc = $laMatches ;
    $desc = $laMatches ;

   usort($desc, function($a, $b) {
                   return strlen($a) - strlen($b) ;
   });
   usort($asc, function($a, $b) {
                    return strlen($b) - strlen($a) ;
   });

   echo '<div class="asc">'.implode('<br />',$asc).'</div>' ;
   echo '<div class="desc">'.implode('<br />',$desc).'</div>' ;