简体   繁体   中英

Sort Function in ajax table

i need to achieve something like this, which i took from this website: http://tablesorter.com/docs/ When the table header is clicked, the result will be sorted. I have tried and its working on table that has value preset in it.

在此处输入图片说明

However, for my scenario, i am using ajax to fetch the table result(based on what the user searched) from the server. The page doesn't reload when search is clicked. My Question is, how do i put the sort function in ajax table?

在此处输入图片说明

This is how i applied my code, but its no working. `

        $(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>";
}
?>

Can anyone give me any guidance on how to achieve the output? Thankyou

call $("#myTable").tablesorter({sortList: [[0, 0], [1, 0]]}); after you retrieve and add the elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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