【问题标题】:json jquery grid (using django) - having problems presenting the datajson jquery 网格(使用 django) - 呈现数据时出现问题
【发布时间】:2010-01-20 13:26:53
【问题描述】:

我正在尝试 jqGrid 从 django 获取一些 json 数据的来源:link

不幸的是,jqgrid中没有显示数据,只有一个空的jqgrid。

我正在用这个调用渲染 jqgrid:

<script type="text/javascript">
    $(function () {
        $.getJSON("{% url myapp.views_json.grid_config %}", function(data){
            $("#mygrid")
                .jqGrid(data)
                .navGrid('#pager', 
                    {add: false, edit: false, del: false, view: true},
            {}, // edit options
            {}, // add options
            {}, // del options 
            { multipleSearch:true, closeOnEscape:true }, // search options 
            { jqModal:false, closeOnEscape:true} // view options 
            );
        });
    });
</script>

{% url myapp.views_json.grid_config %} 被解析为 url “projects/examplegrid/cfg/”。当我在浏览器中调用此 URL 时,它会返回 JSON 数据。请点击链接查看。 json config data

这应该没问题。我猜.. 在这个 json 数据文件中,您会看到一个 url:http://127.0.0.1:8000/pm/projects/examplegrid/ 这个 url 也返回 json 数据。这是我要呈现的数据的 json 表示。在此处查看此 json 数据集:link

这是生成的 jqgrid 的屏幕截图。 link 我知道在这里帮助我可能很难。但在我看来,我的问题出在 jqgrid 方面,我认为外面有很多裂缝,谁知道该由谁来处理它。我没有:-)

编辑:未定义的错误消失了。此错误是由于缺少对本地文件的引用:

 <script type="text/javascript" src="http://localhost:8000/media/js/i18n/grid.locale-en.js"></script> 

但下一个错误是,它显示“正在加载”并且没有完成。有谁知道问题出在哪里?

编辑:错误显然是 jqgrid 没有正确初始化。我使用了 CalebD 提出的 wiki 页面,它现在可以工作了。至少 json 数据呈现在网格中。我现在想知道我必须做什么才能更新一行。

<script type="text/javascript">
    jQuery(document).ready(function(){ 
      jQuery("#grid").jqGrid({
          url:'http://127.0.0.1:8000/pm/projects/examplegrid/',
          datatype: "json",
          mtype: 'GET',
          colNames:['id', 'description'],
          colModel:[
                    {name:'id',index:'id', width:55, sortable:false, editable:false, editoptions:{readonly:true,size:10}},
                    {name:'description',index:'description', width:300, editable:true},
               ],
         jsonReader : {
              repeatitems:false
         },
          rowNum:10,
          rowList:[10,20,30],
          pager: jQuery('#gridpager'),
          sortname: 'name',
          viewrecords: true,
          sortorder: "asc",
          caption:"Wines",
          editurl:"http://127.0.0.1:8000/pm/projects/examplegrid/"
     }).navGrid('#gridpager');
    });
</script>

【问题讨论】:

    标签: jquery django jqgrid


    【解决方案1】:

    .jqGrid 的参数不包括描述网格设置的 JS 对象,而不是 JSON 数据。您必须定义数据类型、列和列模型以及其他选项。 jqGrid wiki 有一些例子:

    http://www.trirand.com/jqgridwiki/doku.php?id=wiki:conventions

    如果向下滚动,有一个使用 JSON 的示例。

    【讨论】:

    • 好的,这对我帮助很大!它现在可以工作并显示数据。谢谢!当我想从 jqgrid 编辑数据时,你知道我必须注意什么吗?我总是收到错误状态:''。错误代码: 0 。我的问题是服务器端会发生什么?编码json数据并返回true?
    • 使用 jqGrid,您必须确保下载的版本具有您想要启用的特定编辑选项。您可以在下载页面上执行此操作:trirand.com/blog/?page_id=6 - 服务器端取决于编辑类型,但基本上 jqGrid 将新值发布到特定 URL,并且该 URL 预计在成功时返回 HTTP 200 代码。我认为服务器响应的内容并不重要。这里有很多信息:trirand.com/jqgridwiki/doku.php?id=wiki:common_rules
    • 哇,谢谢。 jqgrid中的json数据是在成功更新一行后自动刷新还是我必须做某种回调?
    • 我不认为 jqGrid 会刷新数据,因为如果编辑成功,那么当前网格中的数据(仅由用户编辑)应该与服务器上的数据匹配。但是,您可以让服务器返回您想要的任何内容,并根据需要使用保存后事件来修改行,例如,如果您有由服务器自动计算的行。由于某些计算字段等原因,这就是我将 jqGrid 连接到 SharePoint 列表所必须做的事情。
    【解决方案2】:

    为了更新一行,应该在配置网格参数中指定editurl。
    在您的示例中,它是在 js 中指定的,但也可以在 JqGrid 类中进行配置。
    您必须使用 url 来处理更新,就像使用 url 来处理分页、排序、...
    我是这样使用它的:

    urls.py:

    
    ...
    (r'^jqgrid/(?P(\w+))/$', 'grid_handler', {}, 'grid_handler'),
    (r'^jqgrid/cfg/(?P(\w+))/$', 'grid_config', {}, 'grid_config'),
    (r'^edit-store/$', 'edit_store'),
    ...
    

    grids.py:

    
    class StoreGrid(JqGrid):
        ...
        url = '/jqgrid/StoreGrid/'
        editurl = '/edit-store/' 
        ...
    

    config 字典应该有 'editurl' 键,将它添加到 get_config() 中:

    
    def get_config(self, as_json=True):
    config = self.get_default_config()
        config.update({
            'url': self.get_url(),
            'editurl': self.get_editurl(), #!
            'caption': self.get_caption(),
            'custom_url': self.get_custom_url()
         })
         ...
    

    views.py:

    
    def grid_handler(request, grid_name):
        grid = class_for_name(grid_name, 'some_namespace.grids')()
        return HttpResponse(grid.get_json(request), mimetype="application/json")
    
    def grid_config(request, grid_name):
        grid = class_for_name(grid_name, 'some_namespace.grids')()
        return HttpResponse(grid.get_config(), mimetype="application/json")
    
    def edit_store(request):
        if request.method == 'POST' and request.POST:
            if request.POST.get('oper') and request.POST['oper'] == 'del':
                try:
                    Store.objects.get(id=request.POST['id']).delete()
                except Exception, ex:
                    logging.debug(ex)
            else:
                try:
                    instance = Store.objects.get(id=request.POST.get('id'))
                    instance.some_field = request.POST.get('some_field')
                    instance.the_other_field = request.POST.get('the_other_field')
                    instance.save()
                except Exception, ex:
                    logging.debug(ex)
            return render_to_response('template.html')
        return HttpResponseNotFound('invalid request')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-11
      • 1970-01-01
      • 2020-08-11
      • 2011-04-30
      • 2020-12-19
      • 1970-01-01
      相关资源
      最近更新 更多