django、slickgrid 和 json 加载

发布于 2024-12-01 03:58:08 字数 2780 浏览 2 评论 0原文

我正在尝试学习如何在 Django 中使用基于 javascript 的可编辑网格系统。对于刚接触 javascript 以及 django 中的 ajax 和 json 处理的人来说,这是相当令人困惑的。

我一直在查看 SlickGrid 因为它似乎也能实现我想要的功能,主要是可编辑字段快速排序、过滤和搜索。我愿意接受具有类似功能的其他软件包。

我一直坚持用我的 json 填充网格。我什至不确定是 js 还是 json 的格式有问题。

这是一个简化的示例。

我使用以下视图“提供”json:

from django.core.serializers import serialize

def json_testing(request):
    json = serialize("json", FooBar.objects.all())
    return HttpResponse(json, mimetype='application/json')
# urls.py is configured to access this at /json_testing/

这是 www.example.com/json_testing/ 上的 json 输出:

[
  - {
        pk: 1
        model: "myapp.foobar"
      - fields: {
            foo: "test"
            bar: "test"
        }
    },
  - {
        pk: 2
        model: "myapp.foobar"
      - fields: {
            foo: "test2"
            bar: "test2"
        }
    }
]    

这是我的模板:

{% extends 'base.html' %}
{% block head %}
<title>SlickGrid example 1: Basic grid</title>
<link rel="stylesheet" href="/static/SlickGrid/slick.grid.css" type="text/css" media="screen" charset="utf-8" />
<link rel="stylesheet" href="/static/SlickGrid/css/smoothness/jquery-ui-1.8.5.custom.css" type="text/css" media="screen" charset="utf-8" />
<link rel="stylesheet" href="/static/SlickGrid/examples/examples.css" type="text/css" media="screen" charset="utf-8" />
{% endblock %}
{% block content %}
<div width="100%">
    <div id="myGrid" style="width:600px;height:500px;display:none;"></div>
</div>
<script src="/static/SlickGrid/lib/jquery-1.4.3.min.js"></script>
<script src="/static/SlickGrid/lib/jquery.event.drag-2.0.min.js"></script>
<script src="/static/SlickGrid/slick.core.js"></script>
<script src="/static/SlickGrid/slick.grid.js"></script>

<script type="text/javascript">
var grid;

var columns = [
    {id:"foo", name:"Foo", field:"foo"},
    {id:"bar", name:"Bar", field:"bar"}
];

var options = {
    enableCellNavigation: true,
    enableColumnReorder: false
};

$(function() {
    var data = [];
    for (var i = 0, i < 50; i++) {
        data[i] = {
            foo: "foo_" + i,
            bar: "bar_" + i,
        };
    }

// I want to replace the loop to generate code above with json
// I think something along the lines of this
//    $.get_json('/json_testing/')

    grid = new Slick.Grid("#myGrid", data, columns, options);

    $("#myGrid").show();
})
</script>
{% endblock %}

我知道这个问题的答案可能非常基本。也许它是如此基础,这就是为什么很难在网络上找到现有答案的原因。

我从其他答案、博客等中尝试过的每种加载 json 的方法都会导致呈现空白网格。 我需要做什么才能加载我的 json?

I'm trying to learn how to use a javascript based editable grid system with Django. It's pretty confusing for someone brand new to javascript as well as ajax and json handling within django.

I've been looking at SlickGrid because it seems to do what I want it too, mainly editable fields that are quickly sorted, filtered, and searched. I'd be open to other packages with similar functionality.

I'm stuck on populating the grid with my json. I'm not even sure it's the js or the format of the json which is the problem.

Here is a simplified example.

I'm 'serving' the json with the following view:

from django.core.serializers import serialize

def json_testing(request):
    json = serialize("json", FooBar.objects.all())
    return HttpResponse(json, mimetype='application/json')
# urls.py is configured to access this at /json_testing/

Here's the output of the json at www.example.com/json_testing/ :

[
  - {
        pk: 1
        model: "myapp.foobar"
      - fields: {
            foo: "test"
            bar: "test"
        }
    },
  - {
        pk: 2
        model: "myapp.foobar"
      - fields: {
            foo: "test2"
            bar: "test2"
        }
    }
]    

And here's my template:

{% extends 'base.html' %}
{% block head %}
<title>SlickGrid example 1: Basic grid</title>
<link rel="stylesheet" href="/static/SlickGrid/slick.grid.css" type="text/css" media="screen" charset="utf-8" />
<link rel="stylesheet" href="/static/SlickGrid/css/smoothness/jquery-ui-1.8.5.custom.css" type="text/css" media="screen" charset="utf-8" />
<link rel="stylesheet" href="/static/SlickGrid/examples/examples.css" type="text/css" media="screen" charset="utf-8" />
{% endblock %}
{% block content %}
<div width="100%">
    <div id="myGrid" style="width:600px;height:500px;display:none;"></div>
</div>
<script src="/static/SlickGrid/lib/jquery-1.4.3.min.js"></script>
<script src="/static/SlickGrid/lib/jquery.event.drag-2.0.min.js"></script>
<script src="/static/SlickGrid/slick.core.js"></script>
<script src="/static/SlickGrid/slick.grid.js"></script>

<script type="text/javascript">
var grid;

var columns = [
    {id:"foo", name:"Foo", field:"foo"},
    {id:"bar", name:"Bar", field:"bar"}
];

var options = {
    enableCellNavigation: true,
    enableColumnReorder: false
};

$(function() {
    var data = [];
    for (var i = 0, i < 50; i++) {
        data[i] = {
            foo: "foo_" + i,
            bar: "bar_" + i,
        };
    }

// I want to replace the loop to generate code above with json
// I think something along the lines of this
//    $.get_json('/json_testing/')

    grid = new Slick.Grid("#myGrid", data, columns, options);

    $("#myGrid").show();
})
</script>
{% endblock %}

I know the answer to this is probably pretty basic. Maybe it's so basic that's why it's hard to find an existing answer on the web.

Every method for loading the json that I've tried from other answers, blogs, etc has resulted in a blank grid being rendered. What do I need to do to get my json loaded?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乜一 2024-12-08 03:58:08

我在 Ajax + slickgrid 方面遇到了很多麻烦,直到我开始使用 slickgrid 分支,该分支已更新到较新的 jQuery 版本(即使用较新的 ajax 调用),并且有更好的使用 ajax 的文档。

请参阅我对另一个问题的回答:简单的 jQuery SlickGrid JSON 示例或文档

I had a lot of trouble with Ajax + slickgrid until I started using a slickgrid fork that had been updated to newer jQuery versions (i.e. using the newer ajax calls) and had slightly better docs for using ajax.

See my answer to another question here: Simple jQuery SlickGrid JSON example or documentation

你在我安 2024-12-08 03:58:08

在尝试了 slickgrid 和 dojo & 之后dojango,我最终在 JQuery 和 django-jqgrid 上取得了成功(尽管并非没有挫败感)。

虽然这不是问题的答案,但对我来说这是解决这个问题的方法。我说的是解决方案,而不是解决方法,因为我不需要 slickgrid,只需要任何可编辑的网格。

After trying slickgrid and dojo & dojango, I ended up finally finding success with JQuery and django-jqgrid (although not without frustration).

While this is not an answer to the question, it was a solution to this problem for me. I say solution, not workaround, because I didn't need slickgrid, just any editable grid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文