Ajax响应追加到表中

时间:2018-10-15 03:27:20

标签: html ajax django

我正在做一个django项目,在ajax中,我正在调用函数并在数组中获取响应,然后将这些响应附加到html表中。

<script>
        $(document).ready(function(){
            $.ajax({
                method : 'POST',
                url : '/home',
                data: {
                csrfmiddlewaretoken: '{{ csrf_token }}'
                },
                success: function(data){
                var res = $.parseJSON(data);
                var trHTML = '';
                if (res.code == '200'){
                    for(i in res.data){
                    console.log(res.data)
                    console.log(res.data[i].id);
                    trHTML +=
                            '<tr><td><div><img src='
                            + res.data[i].poster_url + '>'
                            + '</td><td><div>'
                            + res.data[i].name
                            + '</div><div></div></td><td>'
                            + res.data[i].content_types_id
                            + '</td><td>'
                            + 'Africa'
                            + '</td><td><ul>'
                            + '</ul><td>';

                    }
                } else {
                    alert('Cart is empty');
                }
                $('#table-list').append(trHTML);


                },
                error: function(data){
                    console.log(data);

                },


            })
        })

    </script>

html代码

<table id="table-list" class="col-md-12 table-bordered table-striped table-condensed cf">
                        <thead class="cf">
                            <tr>
                                <th width="10%">&nbsp;</th>
                                <th width="30%" class="numeric">File Name</th>
                                <th width="15%" class="numeric">Content Type</th>
                                <th width="16%" class="numeric">Geo Rights</th>
                                <th width="16%" class="numeric">License</th>
                                <th width="13%" class="numeric">Action</th>

                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td data-title=""><div class="product"><img src="{% static 'images/p1.jpg' %}"></div></td>
                                <td data-title="File Name">
                                <div class="fl">The Lost World: Jurassic Park</div>
                                <div><button type="button" class="btn btn-white"><i class="fas fa-upload"></i> Upload Video</button></div>
                                </td>
                                <td data-title="Content Type">Video</td>
                                <td data-title="Geo Rights">Asia Pacific, Africa</td>
                                <td data-title="License">Active Licenses:16</td>
                                <td align="left" valign="middle" data-title="Action">
                                <ul class="action_icon">
                                    <li><i class="fas fa-pencil-alt"></i></li>
                                    <li><i class="fas fa-trash-alt"></i></li>
                                    <li><i class="fas fa-ban"></i></li>
                                </ul>
                                </td>

                            </tr>

但是问题是当我在前端附加内容时,它只显示那些列,而我希望按钮部分和操作部分保持完整。但是我无法实现它,我该怎么做?

1 个答案:

答案 0 :(得分:0)

JS

<script>
    $(document).ready(function(){
        $.ajax({
            method : 'POST',
            url : '/home',
            data: {
            csrfmiddlewaretoken: '{{ csrf_token }}'
            },
            success: function(data){
                var res = $.parseJSON(data);
                var trHTML = '';
                if (res.code == '200'){
                    for(i in res.data){
                        console.log(res.data)
                        console.log(res.data[i].id);
                        trHTML = $(`<tr>
                            <td><div><img src= ${res.data[i].poster_url}></td> 
                            <td><div>${res.data[i].name}</div><div></div></td> 
                            <td>${res.data[i].content_types_id}</td>
                            <td>Africa</td>
                            <td><ul>
                                  <li><i class="fas fa-pencil-alt"></i></li>
                                  <li><i class="fas fa-trash-alt"></i></li>
                                  <li><i class="fas fa-ban"></i></li>
                                 </ul>
                            <td></tr>`);
                         $('#table-list').append(trHTML);
                    }
                } else {
                    alert('Cart is empty');
                }
            },
            error: function(data){
                console.log(data);
            },
        })
    })
</script>

HTML

<table class="col-md-12 table-bordered table-striped table-condensed cf">
    <thead class="cf">
         <tr>
            <th width="30%" class="numeric">File Name</th>
            <th width="15%" class="numeric">Content Type</th>
            <th width="16%" class="numeric">Geo Rights</th>
            <th width="16%" class="numeric">License</th>
            <th width="13%" class="numeric">Action</th>
          </tr>
     </thead>
     <tbody id="table-list"></tbody>
</table>