【问题标题】:How to use jqGrid in django frame work如何在 django 框架中使用 jqGrid
【发布时间】:2010-04-18 08:40:40
【问题描述】:

有谁尝试过在 django 中使用 JQGrid 一个 Jquery 插件?

请分享您的知识/代码示例

【问题讨论】:

    标签: jquery django jqgrid


    【解决方案1】:

    django-jqgrid 看起来很有希望。

    【讨论】:

      【解决方案2】:

      我确实在我的项目中使用了 django-jqgrid,我对结果非常满意:

      jQuery 就绪函数:

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

      html代码:

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

      在 urls.py 中

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

      在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
      

      我的 JqGrid 类

      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')
      

      您可以在 jqGrid 类中使用任何模型,它是自动配置的!

      解决方案非常优雅。 为了显示特殊格式(日期时间、文件大小和布尔值),我使用自定义 json_encode 方法在发送 ajax 响应之前格式化这些类型。

      【讨论】:

      • 我不知道我是否必须在这里问,但是,JqGrid类所在的jqgrid文件应该在INSTALLED_APPS上吗?我正在尝试,但出现列表索引错误
      • 我个人将 jqgrid 文件完全集成到我的项目 util 包中,并进行了一些调整和修改。由于其中没有模型,我不需要将其添加到我的INSTALLED_APPS 中。我只是导入 util.jqgrid 并使用它,如您在此示例中所见。
      • 你知道,当我尝试这个时,我得到这个列表索引超出范围错误。知道为什么吗?我不明白
      猜你喜欢
      • 1970-01-01
      • 2020-03-22
      • 1970-01-01
      • 2021-12-28
      • 2018-04-08
      • 2021-12-05
      • 1970-01-01
      • 1970-01-01
      • 2017-02-22
      相关资源
      最近更新 更多