将数据表添加到表中(wordpress)

时间:2017-04-04 15:14:02

标签: php wordpress datatables

我正在尝试将数据表添加到我的项目中,它会显示箭头和搜索栏,但都不起作用。

我尝试使用脚本和表格在页面上测试代码并且它可以工作但是当我将它移动到我的wordpress网站时,我遇到了问题。

有什么建议吗?我在头文件页面中有jquery和datatables脚本和css,以及/ head之后的表本身脚本:

    <script>
       $(document).ready(function(){
       $('#myTable').DataTable();
       });
   </script>

我的表本身就像

     echo "<table id='myTable' class='display table' width='100%'>";
                    echo "<thead>";
                    echo "<tr>";
                    echo "<th>Foster ID</th>";
                    echo "<th>Foster's Name</th>";
                    echo "<th>City</th>";
                    echo "<th>E-Mail</th>";
                    echo "</tr>";
                    echo "</thead>";
                    if(!empty($result)){
                        foreach ($result as $results){
                            $fosterId = $results->memberId;
                            $fosterName = $results->memberName;
                            $city = $results->city;
                            $email = $results->email;
                            echo "<tbody>";
                            echo "<tr>";
                            echo "<td>$fosterId</td>";
                            echo "<td>$fosterName</td>";
                            echo "<td>$city</td>";
                            echo "<td>$email</td>";
                            echo "</tr>";
                        }
                    } else {
                        echo "<tr>";
                        echo "<td colspan='5'>No Fosters</td>";
                        echo "<tr>";
                        echo "</tbody>";
                    }
                    echo "</table>";

1 个答案:

答案 0 :(得分:1)

您的PHP代码在循环中包含tbody元素。此外,DataTables不支持表体中的colspan属性。

更正了PHP代码:

echo "<table id='myTable' class='display table' width='100%'>";
echo "<thead>";
echo "<tr>";
echo "<th>Foster ID</th>";
echo "<th>Foster's Name</th>";
echo "<th>City</th>";
echo "<th>E-Mail</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
foreach ($result as $results){
    $fosterId = $results->memberId;
    $fosterName = $results->memberName;
    $city = $results->city;
    $email = $results->email;
    echo "<tr>";
    echo "<td>$fosterId</td>";
    echo "<td>$fosterName</td>";
    echo "<td>$city</td>";
    echo "<td>$email</td>";
    echo "</tr>";
}
echo "</tbody>";
echo "</table>";

更正了JavaScript代码:

$(document).ready(function(){
    $('#myTable').DataTable({
        "language": {
            "emptyTable": "No Fosters"
        }
    });
});