在ajax表

时间:2016-01-21 15:54:54

标签: javascript php ajax

我需要实现这样的目标,我从这个网站上获取:http://tablesorter.com/docs/ 单击表标题时,将对结果进行排序。我已经尝试过并且在桌子上工作,其中有预设值。

enter image description here

但是,对于我的场景,我使用ajax从服务器获取表结果(基于用户搜索的内容)。单击搜索时页面不会重新加载。我的问题是,如何将sort函数放在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>";
}
?>

有人能给我任何关于如何实现输出的指导吗?三江源

1 个答案:

答案 0 :(得分:1)

在检索并添加元素后调用$("#myTable").tablesorter({sortList: [[0, 0], [1, 0]]});