可以slickgrid页面并显示json

发布于 2024-10-02 06:21:27 字数 246 浏览 0 评论 0原文

我们希望在请求页面时向客户端加载数千条记录,并显示前 25 条记录。然后,用户应该能够翻阅记录或按列重新排列列表或按各列中的数据进行过滤。我们选择一次性将数据加载到客户端,因为我们希望页面请求负载更重,并在之后查看或编辑数据时获得更快的性能。我在 SlickGrid 网站上看不到任何分页示例。 SlickGrid 是否内置了分页功能,或者它是如此轻量级,我必须自己实现它?有没有人有任何链接或示例可以分享,可以让我领先?

我们将使用的数据将是 json 数据。

We want to load several thousand records to the client when the page is requested and have the first 25 records displayed. The user should then be able to page through the records or resort the list by column or filter by data in various columns. We're opting to load the data to the client in one lump sum because we'd rather have a heavier load in the page request and faster performance when viewing or editing data after. I can't see any example for paging on the SlickGrid site. Does SlickGrid have paging baked in or is it so lightweight I'd have to implement this myself? Does anyone have any links or examples to share that would give me a headstart?

The data we'll be using will be json data.

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

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

发布评论

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

评论(5

眉目亦如画i 2024-10-09 06:21:27

SlickGrid/slick.dataview.js 中有分页支持,

dataView = new Slick.Data.DataView();
grid = new Slick.Grid("#myGrid", dataView, columns, options);
dataView.setPagingOptions({
   pageSize: 25,
});
var pager = new Slick.Controls.Pager(dataView, grid, $("#myPager"));

您还需要在页面上的某个位置进行渲染。

<div id="myPager"></div>

There is paging support in SlickGrid/slick.dataview.js

dataView = new Slick.Data.DataView();
grid = new Slick.Grid("#myGrid", dataView, columns, options);
dataView.setPagingOptions({
   pageSize: 25,
});
var pager = new Slick.Controls.Pager(dataView, grid, $("#myPager"));

You will also want somewhere on your page for it to render.

<div id="myPager"></div>
想念有你 2024-10-09 06:21:27

我编写了一个分页器插件,SlickGrid 增强分页器,它使 SlickGrid 分页使用起来更加容易。完全支持json。请参阅 github 上的这个项目

I have written a pager plugin, the SlickGrid Enhancement Pager, that makes SlickGrid pagination use much easier. It fully supports json. Please see this project on github.

老街孤人 2024-10-09 06:21:27
//set columns
var columns = [
    {
        id: "mid",
        name: "MID",
        field: "mid",
        cssClass: "cell-title",
        sortable: true,
        sorter:comparer
    },
    {
        id: "id",
        name: "ID",
        field: "id",
        cssClass: "cell-title",
        sortable: true,
        sorter:NumericSorter
    },

在上面设置排序器

var sortcol = "title";
var sortdir = 1;
var percentCompleteThreshold = 0;
var searchString = "";

this.grid.onSort = function(sortCol, sortAsc) {
    sortdir = sortAsc ? 1 : -1;
    sortcol = sortCol.field;
    this.dataView.sort(sortCol.sorter);
    this.grid.render();

}.bind( this );

function comparer(a,b) {
    var x = a[sortcol], y = b[sortcol];
    return sortdir * (x == y ? 0 : (x > y ? 1 : -1));
}

// Define some sorting functions
function NumericSorter(a, b) {
  return sortdir *  (a[sortcol]-b[sortcol]);
}
//set columns
var columns = [
    {
        id: "mid",
        name: "MID",
        field: "mid",
        cssClass: "cell-title",
        sortable: true,
        sorter:comparer
    },
    {
        id: "id",
        name: "ID",
        field: "id",
        cssClass: "cell-title",
        sortable: true,
        sorter:NumericSorter
    },

In the above set the sorter

var sortcol = "title";
var sortdir = 1;
var percentCompleteThreshold = 0;
var searchString = "";

this.grid.onSort = function(sortCol, sortAsc) {
    sortdir = sortAsc ? 1 : -1;
    sortcol = sortCol.field;
    this.dataView.sort(sortCol.sorter);
    this.grid.render();

}.bind( this );

function comparer(a,b) {
    var x = a[sortcol], y = b[sortcol];
    return sortdir * (x == y ? 0 : (x > y ? 1 : -1));
}

// Define some sorting functions
function NumericSorter(a, b) {
  return sortdir *  (a[sortcol]-b[sortcol]);
}
淡写薰衣草的香 2024-10-09 06:21:27

这也让我困惑了一段时间,直到我让下面的代码起作用:

grid = new Slick.Grid("#myGrid", data, columns, options);
grid.onSort.subscribe(function(e,args) {
    sortcol = args.sortCol;
    sortAsc = args.sortAsc;

    alert("Sort On " + sortcol.field);

    if (sortAsc == true) {
        data.sort(function (a, b) {
            var nameA = a[sortcol.field].toLowerCase(), nameB = b[sortcol.field].toLowerCase()
            if (nameA < nameB) //sort string ascending
                return -1
            if (nameA > nameB)
                return 1
            return 0 //default return value (no sorting)
        });
    } else {
        data.reverse(function (a, b) {
            var nameA = a[sortcol.field].toLowerCase(), nameB = b[sortcol.field].toLowerCase()
            if (nameA < nameB) //sort string ascending
                return -1
            if (nameA > nameB)
                return 1
            return 0 //default return value (no sorting)
        });
    }

    grid.invalidateAllRows();
    grid.render();
});

This puzzled me for a while too, until I got the code below to work:

grid = new Slick.Grid("#myGrid", data, columns, options);
grid.onSort.subscribe(function(e,args) {
    sortcol = args.sortCol;
    sortAsc = args.sortAsc;

    alert("Sort On " + sortcol.field);

    if (sortAsc == true) {
        data.sort(function (a, b) {
            var nameA = a[sortcol.field].toLowerCase(), nameB = b[sortcol.field].toLowerCase()
            if (nameA < nameB) //sort string ascending
                return -1
            if (nameA > nameB)
                return 1
            return 0 //default return value (no sorting)
        });
    } else {
        data.reverse(function (a, b) {
            var nameA = a[sortcol.field].toLowerCase(), nameB = b[sortcol.field].toLowerCase()
            if (nameA < nameB) //sort string ascending
                return -1
            if (nameA > nameB)
                return 1
            return 0 //default return value (no sorting)
        });
    }

    grid.invalidateAllRows();
    grid.render();
});
蘑菇王子 2024-10-09 06:21:27

我已经用 slickgrid 编码大约一周了,发现我必须自己编写排序和过滤代码。查看源代码,我没有看到任何表明分页是内置的。您将花费大量时间为其编写代码,但这似乎是值得的。

我使用 ajax/json 加载了 30,000 行数据,加载和排序时间不到 1 秒。我不知道这是否有任何帮助,但这是我调用来加载网格的方法。它在客户端排序并在服务器上过滤:

$.getJSON(baseUrl + '/_GetNewHires?filter=' + filter, function (data) {
    grid = new Slick.Grid($("#NewHiresGrid"), data, columns, options);

    grid.onSort = function (sortCol, sortAsc) {
        sortdir = sortAsc ? 1 : -1;
        sortcol = sortCol.field;

        if (sortAsc == true)
            data.sort(compare);
        else
            data.reverse(compare);

        grid.render();
    };
});

当调用排序方法时,绑定到网格(数据)的数组将被重新排列,然后使用 .render() 方法重新加载网格。为了进行分页,您必须拥有一个包含所有数据的数组和一个显示数据的数组。

他确实有一个分页示例 这里,但作为 javascript 业余爱好者,我是的,我很难跟上。

I've been coding with slickgrid for about a week now and found that I had to write sort and filter code myself. Looking through the source, I don't see anything that indicates paging is built in. You'll spend a good amount of time writing code for it, but it seems to be worth it.

I loaded 30,000 rows of data using ajax/json and it loads and sorts in less than 1 second. I don't know if it will be any help, but this is the method I call to load my grid. It sorts on the client and filters on the server:

$.getJSON(baseUrl + '/_GetNewHires?filter=' + filter, function (data) {
    grid = new Slick.Grid($("#NewHiresGrid"), data, columns, options);

    grid.onSort = function (sortCol, sortAsc) {
        sortdir = sortAsc ? 1 : -1;
        sortcol = sortCol.field;

        if (sortAsc == true)
            data.sort(compare);
        else
            data.reverse(compare);

        grid.render();
    };
});

When the sort method is called, the array bound to the grid (data) is rearranged, and then the grid is reloaded using the .render() method. In order to page, you'll have to have an array of all data, and an array of displayed data.

He does have an example of paging here, but being the javascript amateur I am, it's difficult for me to follow.

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