[Django]-How to use jqGrid in django frame work

5👍

django-jqgrid looks pretty promising.

3👍

I do use django-jqgrid in my project and I am very happy with the results:

the jQuery ready function:

$(document).ready(function() {


$.getJSON("/accounts/documents_recents_config", function(data){
    // On ajoute le lien vers le document
    data['onSelectRow'] = function(id){ window.open("/fichier/lecture/"+id,"_self"); };
    data['gridComplete'] = function(id){ jQuery("#documents_recents_count").html(jQuery("#list_doc").jqGrid('getGridParam', 'records' )); };
    $("#list_doc")
        .jqGrid(data)
        .navGrid('#pager_doc', 
            {add: true, edit: false, del: false, view: false},
    {}, // edit options
    {}, // add options
    {}, // del options 
    { multipleSearch:true, closeOnEscape:false }, // search options 
    { jqModal:false, closeOnEscape:true} // view options 
    );
});

});

The html code:

<table id="list_doc"></table>
<div id="pager_doc"></div>

in urls.py

(r'^documents_recents_config/$', 'document_recents_config'),
(r'^documents_recents/', 'document_recents_handler'),

in views.py

@login_required
def document_recents_handler(request):
    # handles pagination, sorting and searching
    if request.user and (request.user.is_staff == False):
        grid = DocumentRecentsGrid(request)
        return HttpResponse(grid.get_json(request), mimetype="application/json")
    else:
        raise Http404

My JqGrid class

class DocumentRecentsGrid(JqGrid):
    fields = ("nom","themes", "description", "nom_du_fichier", "taille_du_fichier", "public", "cree_le")
    queryset = None
    url = "/accounts/documents_recents/"
    caption = force_unicode('Mes documents personnels récents') # optional
    colmodel_overrides = {
        'description': { 'editable': False, 'width':240 },
        'nom_du_fichier': { 'editable': False, 'width':120 },
        'taille_du_fichier': { 'editable': False, 'width':90 },
        'public': { 'editable': False, 'width':50 },
        'cree_le': { 'editable': False, 'width':125 },
    }
def __init__(self, request):
    super(DocumentRecentsGrid, self).__init__()
    self.queryset = Lecture.objects\
        .filter(salarie__username__exact=request.user.username)\
        .filter(consulte=False).order_by('-cree_le')

You can use any model in the jqGrid class, it’s auto-configurated!

The solution is very elegant.
In order to display the special format (datetime, filesize and boolean), I use a custom json_encode method which format those types before sending the ajax response.

Leave a comment