jQuery Datatable无法在symfony中工作

时间:2017-07-24 11:22:10

标签: javascript jquery ajax symfony datatable

我已经完成了所有必需的步骤,但我不知道为什么我的数据表没有显示。我添加了jquery.dataTables.cssjquery.dataTables.js

**view.html.twig**

{% extends 'base.html.twig' %}

{% block stylesheets %}
   <link rel="stylesheet" href="{{ asset('bundles/app/css/style.css') }}" >
   <link rel="stylesheet" href="{{ asset('bundles/app/css/bootstrap.css') }}" >
   <link rel="stylesheet" href="{{ asset('bundles/app/css/bootstrap-grid.css') }}" >
   <link rel="stylesheet" href="{{ asset('bundles/app/css/bootstrap-theme.css') }}" >
   <link rel="stylesheet" href="{{ asset('bundles/js/media/css/dataTables.bootstrap.css') }}" >
   <link rel="stylesheet" href="{{ asset('bundles/js/media/css/jquery.dataTables.css') }}" >
{% endblock %}

{% block javascripts %}
   <script src="{{ asset('bundles/app/js/bootstrap.min.js') }}"></script>
   <script src="{{ asset('bundles/app/js/jquery-3.2.1.min.js') }}"></script>
   <script src="{{ asset('bundles/app/js/jquery-ui.js') }}"></script>
   <script src="{{ asset('bundles/app/js/demo.js') }}"></script>
   <script src="{{ asset('bundles/js/media/js/jquery.dataTables.js') }}"></script>
   <script src="{{ asset('bundles/js/media/js/dataTables.bootstrap.js') }}"></script>
   <script src="{{ asset('bundles/js/media/js/jquery.dataTables.js') }}"></script>
{% endblock %}

{% block body %}
    <h1>STUDENT INFORMATION FORM</h1>
    <table class="container" id="table_view" border="1"> 
        <thead>
            <tr>
                <th>USER NAME</th>
                <th>FIRST NAME</th>
                <th>LAST NAME</th>
                <th>EMAIL ID</th>
                <th>PASSWORD</th>
                <th>EDIT</th>
                <th>DELETE</th>
            </tr>
        </thead>
        <tbody>
        {% for key in view %}
            <tr>
                <td>{{  key.UserName }}</td>
                <td>{{  key.FirstName }}</td>
                <td>{{  key.LastName }}</td>
                <td>{{  key.EmailId }}</td>
                <td>{{  key.Password }}</td>
                <td><span class="link"><a href="/my_project/web/app_dev.php/edit/{{ key.id }}" class="btn btn-primary btn-lg">Edit</a><span></td>
                <td><span class="link"><a href="/my_project/web/app_dev.php/delete/{{ key.id }}" class="deleteUser btn btn-danger btn-lg">Delete</a><span></td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
    <script>
          $(document).ready(function(){
          $('#table_view').DataTable();
          });
    </script>


{% endblock %}



        **demo.js**

这是我使用Ajax的demo.js文件:

    $(document).ready(function(){
    $("#create").click(function(){
    alert('You Can See Student Information Form');

        //var formURL = "{{ path('form') }}";
        //var formData = new FormData(this);

            $.ajax({
            type: "POST",
            url: formURL,
            dataType: "json",
            data: $("#form_person_edit").serialize(),
            success: function(res){
            $(".ajax_response").html();

            if(res.status == 'success'){
            window.location.href = viewURL;
            }
            }   
    });
    });
    });

我不知道我在哪里犯了错误。

1 个答案:

答案 0 :(得分:0)

默认情况下,块javascripts会在html的末尾呈现。所以在你的情况下你调用$('#table_view')。DataTable();太早了。 移动:

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

在块javascripts结束时。在你的base.html.twig中,确保在块体之后定义块javascripts。