【问题标题】:Unable to sort Dgrid无法对 Dgrid 进行排序
【发布时间】:2014-01-01 11:46:04
【问题描述】:
var CustomGrid = declare([Grid, Keyboard, Selection]);
                    var questionGrid = new CustomGrid({
                        store: questionCacheStore,
                        columns: [
                            editor({
                                label: "Questions",
                                field: "question",
                                editor: "text",
                                editOn: "dblclick",
                                sortable:true})

                        ],
                        selectionMode: "single",
                        cellNavigation: false
                    }, "questions");

我是 Dgrid 的新手。所以,请多多包涵。 我能够用 JsonStore 内容填充 dgrid。但是当我单击“问题”列时,它不会像在本地数据存储中那样排序。而是显示错误Uncaught TypeError: Object [object Object] has no method 'sort'。是否需要定义这样的方法。如果是这样,我应该如何以及在哪里定义它?

【问题讨论】:

  • 使用OnDemandGrid。我遇到了同样的问题,并逐行删除了我的代码,试图找到问题所在。我想出的唯一解决方案是将依赖项从“dgrid/Grid”更改为“dgrid/OnDemandGrid”。
  • 另外,请注意 dgrid 将再次查询其余服务进行排序。例如,如果您的 RESTful Web 服务 url 是 /rest/questions/,并且您单击了 questionId 的列标题,则 dgrid 将查询:/rest/questions/sort(+questionId) 以升序排列。对于降序,您可能会看到/rest/questions/sort(-questionId)。您提供排序逻辑并以 json 格式返回。
  • 你的网格好运吗?
  • 是的,控制台的错误消失了。谢谢。现在你能告诉我应该在哪里定义这种排序方法吗?我也是 J2EE 的新手

标签: json sorting dojo dgrid


【解决方案1】:

我不是回答您的 J2EE 问题的人。我最近问过question。我找到的解决方案是inject the HttpServletRequest directly。这允许我访问查询字符串参数。从那里我能够获得排序方向(升序,降序)和要排序的列。希望下面的 sn-ps 会有所帮助。

示例网格设置

require(["dojo/store/JsonRest", "dojo/store/Memory", "dojo/store/Cache", 
    "dojo/store/Observable", "dgrid/OnDemandGrid", "dojo/_base/declare", "dgrid/Keyboard", 
    "dgrid/Selection", "dojo/domReady!"], 
function(JsonRest, Memory, Cache, Observable, Grid, declare, Keyboard, Selection) {

    var rest = new JsonRest({target:"/POC_Admin/rest/Subcategory/", idProperty: "subcatId"});
    var cache = new Cache(rest, new Memory({ idProperty: "subcatId" }));
    var store = new Observable(cache);
    var CustomGrid = declare([ Grid, Keyboard, Selection ]);

    var grid = new CustomGrid({
        columns: {
            subcatId: "ID",
            name: "Name"
        },
        store: store
    }, "grid");      

    grid.on("dgrid-select", function(event){
        // Report the item from the selected row to the console.
        console.log("Row selected: ", event.rows[0].data);
    });

    grid.startup();

});

Rest GET 示例

    @Context private HttpServletRequest servletRequest;
    @GET
    @Path("")
    @Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
    public String getSubcategories(@QueryParam("name") String name) throws IOException {

        //Respond to a QueryString value.
        if (servletRequest.getQueryString() != null && servletRequest.getQueryString().length() > 0) {

            String querystringKey = servletRequest.getQueryString();
            System.out.println("QSKey = " + querystringKey);
            System.out.println("Substr: " + querystringKey.substring(0, 4));
            if (querystringKey.length()>4) {

                if (querystringKey.substring(0, 4).contains("sort")) {
                    //We have the sort request.
                }
            }
        }

        //Return all results otherwise from your DAO at this point
    }

【讨论】:

    猜你喜欢
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    • 2019-04-20
    • 2020-09-20
    • 2019-08-15
    相关资源
    最近更新 更多