slickgrid.js 2.16 KB
var loader = new Slick.Data.RemoteModel($dj.ajax_get_page);

var slickgrid = {
    grid: null
};

var options = {
    editable: false,
    enableAddRow: false,
    enableCellNavigation: false
};

var loadingIndicator = null;

$(function() {
    "use strict";
    slickgrid.grid = new Slick.Grid(
        '#' + slickgrid.grid_element_id, loader.data, slickgrid.columns,
        options);

    slickgrid.grid.onViewportChanged.subscribe(function (e, args) {
        var vp = slickgrid.grid.getViewport();
        loader.ensureData(vp.top, vp.bottom);
    });

    slickgrid.grid.onSort.subscribe(function (e, args) {
        loader.setSort(args.sortCol.field, args.sortAsc ? 1 : -1);
        var vp = slickgrid.grid.getViewport();
        loader.ensureData(vp.top, vp.bottom);
    });

    loader.onDataLoading.subscribe(function () {
        if (!loadingIndicator) {
            loadingIndicator = $(
                "<span class='loading-indicator'><label>Ładowanie..." +
                "</label></span>").appendTo(document.body);
            var $g = $('#' + slickgrid.grid_element_id);

            loadingIndicator
                .css("position", "absolute")
                .css("top", $g.position().top + $g.height() / 2 - loadingIndicator.height() / 2)
                .css("left", $g.position().left + $g.width() / 2 - loadingIndicator.width() / 2);
        }

        loadingIndicator.show();
    });

    loader.onDataLoaded.subscribe(function (e, args) {
        for (var i = args.from; i <= args.to; i++) {
            slickgrid.grid.invalidateRow(i);
        }

        slickgrid.grid.updateRowCount();
        slickgrid.grid.render();

        loadingIndicator.fadeOut();
        layout.adjust_grid_size();
    });

    var text_search = $("#text-search");
    text_search.keyup(function (e) {
        if (e.which === 13) {
            loader.setSearch($(this).val());
            var vp = slickgrid.grid.getViewport();
            loader.ensureData(vp.top, vp.bottom);
        }
    });

    loader.setSearch(text_search.val());
    loader.setSort("create_ts", -1);
    slickgrid.grid.setSortColumn("date", false);

    // load the first page
    slickgrid.grid.onViewportChanged.notify();
});