Get Filtered data from Dataview in Slickgrid

2019-08-04 18:07发布

问题:

I am using dataView property of slick grid to set data in slick grid. I applied header row filter. I want fetch filtered data on external button click. I am able to get all data by using dataview.getItems(), but when I filter data on header row and use same property dataview.getItems() it return all rows not the filter row data.

function bindSlickGrid(myOBJ) {

            var options = {
                enableCellNavigation: true,
                enableColumnReorder: true, multiColumnSort: true, showHeaderRow: true, headerRowHeight: 30
                , explicitInitialization: true

            };






            dataView = new Slick.Data.DataView();


            grid = new Slick.Grid("#myGrid", dataView, myColList, options);


            dataView.onRowCountChanged.subscribe(function (e, args) {
                grid.updateRowCount();

                grid.render();
            });

            dataView.onRowsChanged.subscribe(function (e, args) {
                grid.invalidateRows(args.rows);
                grid.render();




            });


            $(grid.getHeaderRow()).delegate(":input", "change keyup", function (e) {
                var columnId = $(this).data("columnId");
                if (columnId != null) {
                    columnFilters[columnId] = $.trim($(this).val());
                    dataView.refresh();


                }


            });



            grid.onHeaderRowCellRendered.subscribe(function (e, args) {

                $(args.node).empty();
                $("<input type='text'>")
           .data("columnId", args.column.id)
           .val(columnFilters[args.column.id])
           .appendTo(args.node);
            });



            grid.init();



            dataView.beginUpdate();
            dataView.setItems(myOBJ);
            dataView.setFilter(function (item) {
                for (var columnId in columnFilters) {
                    if (columnId !== undefined && columnFilters[columnId] !== "") {
                        var c = grid.getColumns()[grid.getColumnIndex(columnId)];
                        if (item[c.field] != columnFilters[columnId]) {
                            return false;
                        }
                    }
                }
                return true;
            });
            dataView.endUpdate();




            //Sort data 
            grid.onSort.subscribe(function (e, args) {
                var cols = args.sortCols;

                myOBJ.sort(function (dataRow1, dataRow2) {
                    for (var i = 0, l = cols.length; i < l; i++) {
                        var field = cols[i].sortCol.field;
                        var sign = cols[i].sortAsc ? 1 : -1;
                        var value1 = dataRow1[field], value2 = dataRow2[field];
                        var result = (value1 == value2 ? 0 : (value1 > value2 ? 1 : -1)) * sign;
                        if (result != 0) {
                            return result;
                        }
                    }
                    return 0;
                });
                grid.invalidate();
                grid.render();


**console.log(dataview.getItems() )**
            });

回答1:

This post is old, but i was also looking for a solution like this.

However almost every post i came across pointed me to your solution as well.

I got it to work by adding a getter for the filtered rows:

function getFilteredItems(){
    return filteredItems;
}

theres a variable called filteredItems in the dataview. remember to also add

"getFilteredItems" : getFilteredItems

under

$.extend(this, { ...