2016-01-21 28 views
0

我需要實現這樣的,我從這個網站採取:http://tablesorter.com/docs/ 當表頭被點擊時,結果將被排序。我已經嘗試過,並且已經在其中預置了值的桌子上工作。排序函數在ajax表

enter image description here

但是,對於我的方案,我使用AJAX來獲取表的結果(根據用戶搜索的內容)從服務器。單擊搜索時頁面不會重新加載。我的問題是,我如何將排序功能放在ajax表中?

enter image description here

這是我如何應用我的代碼,但它沒有工作。 `

 $(document).ready(function() 
     { 
      $("#myTable").tablesorter({sortList: [[0, 0], [1, 0]]}); 
     } 
     ); 


    </script> 
</head> 
<?php 
session_start(); 
include("dbFunctions.php"); 



$query = "SELECT subject_name AS 'Subject Name', subject_status AS 'Subject Status', subject_id AS 'Edit' FROM subject"; 


if (isset($_GET['parameter1']) && $_GET['parameter1'] != "0") { 
    $parameter1 = $_GET['parameter1']; 
} else { 
    $parameter1 = ""; 
} 
$query = $query . " WHERE subject_name LIKE '%$parameter1%' "; 

$parameter2 = $_GET['parameter2']; 
if ($parameter2 != "all") { 
    $query = $query . " AND subject_status = '$parameter2' "; 
} 

$result = mysqli_query($link, $query) or die(mysqli_error($link)); 

$colCount = mysqli_field_count($link); 
mysqli_close($link); 


if (mysqli_num_rows($result) != 0) { 
    ?> 
    <br> 
    <body> 
     <table id="myTable" border="1"> 
      <thead><tr> 
        <?php for ($i = 0; $i < $colCount; $i++) { ?> 
         <th><?php echo mysqli_fetch_field_direct($result, $i)->name; ?></th> 
         <?php 
        } 
        ?> 
       </tr> </thead> 
      <tbody> 
       <?php 
       while ($row = mysqli_fetch_array($result)) { 
        ?> 
        <tr> 
         <?php for ($i = 0; $i < $colCount; $i++) { ?> 
          <td><?php 
           if (mysqli_fetch_field_direct($result, $i)->name == "Edit") { 
            $subjectId = $row[mysqli_fetch_field_direct($result, $i)->name]; 
            echo "<button><a href='editSubject.php?subject_id=$subjectId'>Edit</a></button>"; 
           } else { 
            echo $row[mysqli_fetch_field_direct($result, $i)->name]; 
           } 
           ?></td> 
         <?php } ?> 
        </tr> 
       <?php } ?> 
      </tbody> 
     </table> 
    </body> 

    <?php 
} else { 
    echo "<h3>No Matching Records Found</h3>"; 
} 
?> 

誰能給我如何實現輸出的任何指導? Thankyou

+0

定義'不工作'。一個錯誤,或者只是不做你想要的? – PaulG

+0

當我按下標題時,表格不排序! – John

回答

1

檢索並添加元素後調用$("#myTable").tablesorter({sortList: [[0, 0], [1, 0]]});

+0

我該怎麼做? – John

+0

@John我沒有在你的代碼中看到ajax,但在'success callback' –