1

My Ajax request to update the tables is not working. Attached is the screenshot. It simply displays "processing" but nothing happens. The JSON object that is returned (if i visit the URL -{% url 'search_list_json' %} --used in AjaxSource of Js code below) seems to be correct. but still the mainpage is not displaying the JSON content in table. Here it is:

{"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 My 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)

The js-code is below:

$(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 ] } ]
    } );
} );

The HTML is:

<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>
4

0 回答 0