django-datatables-view不显示从AJAX请求返回的内容?

时间:2014-04-09 21:38:53

标签: django django-views django-ajax-selects django-datatable

我的Ajax request更新表格无效。附件是截图。它只是显示"处理"但没有任何反应。返回的JSON object(如果我访问URL - {% url 'search_list_json' %} - 在下面的Js代码的AjaxSource中使用)似乎是正确的。但是主页仍然没有在表格中显示JSON内容。 这是:

{"result": "ok", 
 "iTotalRecords": 1, "aaData": [["<center><a href=\"/search/1\"><font color=\"red\">Mazda=>626:2012-1986</font></a>\n                </center>", "04/07/2014", "10000", "1000", "<center><a href='/search/update/1/'><img src='/static/images/icons/icon_changelink.gif'></a>&nbsp;<a href='/search/delete/1/'><img src='/static/images/icons/icon_deletelink.gif'></a></center>"]], 
  "sEcho": 0, 
  "iTotalDisplayRecords": 1}

enter image description here 我的views.py

class SearchListJson(BaseDatatableView):
    model = Search

    columns=['title','created','max_price', 'min_price','actions']
    order_columns = ['created', 'max_price']
    max_display_length = 500

    def render_column(self, row, column):
        user = self.request.user
        url_edit=static('images/icons/icon_changelink.gif')
        url_delete=static('images/icons/icon_deletelink.gif')

        #print url_edit, url_delete

        if column == 'title':
            value = '{0}=>{1}:{2}-{3}'.format(row.vehicle_make,row.vehicle_model,
                                          row.max_year,row.min_year)
            edit_url = reverse('search_detail', args=(row.id,))
            #print self.get_value_cell_style(edit_url, value,'red')
            return self.get_value_cell_style(edit_url, value,'red')

        elif column == 'max_price':
            #print '%s' %row.max_price
            return '%s' %row.max_price
        elif column == 'min_price':
            #print '%s' %row.min_price
            return '%s' %row.min_price
        elif column == 'created':
            #print row.created.strftime('%m/%d/%Y')
            return row.created.strftime('%m/%d/%Y')
        elif column == 'actions':
            print "in columns actions"
            edit_link = """<a href='%s'><img src='%s'></a>""" %(\
                reverse('search_update', args=(row.id,)),url_edit)
            delete_link = """<a href='%s'><img src='%s'></a>""" %(\
                reverse('search_delete', args=(row.id,)),url_delete)
            print "edit_link", edit_link
            print "delete_link",delete_link
            return '<center>%s&nbsp;%s</center>' % (edit_link, delete_link)
        else:
            return super(SearchListJson, self).render_column(row, column)

    def get_value_cell_style(self, url, value, color=None):
        style = '''<center><a href="%s">%s</a></center>''' % (url, value)
        if color:
            style = '''<center><a href="%s"><font color="%s">%s</font></a>
                </center>''' % (url, color, value)

        return style

    def get_initial_queryset(self):
        """
        Filter records to show only entries from the currently logged-in user.
        """
        #print "get intial queryset called"
        #print Search.objects.filter(user=self.request.user)
        return Search.objects.filter(user=self.request.user)

js-code位于以下位置:

$(document).ready(function() {
var oTable = $('#search_table').dataTable( {
        "sDom": 'T<"clear">lrtip',
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "{% url 'search_list_json' %}",
        "aaSorting": [ [1,'desc'], [2,'desc'] ],
        // Disable sorting for the Actions column.
        "aoColumnDefs": [ { "bSortable": false, "aTargets": [ 4 ] } ]
    } );
} );

HTML是:

<div class="container">
    <div class="row">
        <div class="col-sm-12 col-md-12">
            <div class="well">
                <table id="search_table">
                    <thead>
                        <tr>
                            <th width="10%"><center>Title</center></th>
                            <th width="15%">Date Created</th>
                            <th width="15%">Min Price</th>
                            <th width="15%">Max Price</th>
                            <th width="10%"></th>
                        </tr>
                    </thead>
                </table><br>
            </div>
        </div>
    </div>

0 个答案:

没有答案