slickgrid +搜索多列

时间:2012-02-27 10:15:27

标签: slickgrid

如何在grails应用程序中使用slickGrid一次搜索多列?

https://github.com/mleibman/SlickGrid/tree/master/examples

3 个答案:

答案 0 :(得分:7)

以下是搜索多列的代码。

var dataView = new Slick.Data.DataView();
var slickGrid = new Slick.Grid($('#grid_div'), dataView, gridColumns, gridOptions);

$('#search').keyup(function(e) {
   // clear on Esc
   if (e.which == 27) {
      this.value = "";
       }

    var searchList = $.trim(this.value.toLowerCase()).split(' ');
    dataView.setFilter(gridFilter);  
    slickGrid.invalidate();
    this.focus();

});

function gridFilter (rec) {
                var found;

                for (i = 0; i < gridSearchList.length; i += 1) {
                    found = false;
                    $.each(rec, function(obj, objValue) {
                        if (typeof objValue !== 'undefined' && objValue != null 
                        && objValue.toString().toLowerCase().indexOf(gridSearchList[i]) != -1) {
                            found = true;
                            return false; //this breaks the $.each loop
                        }
                    });
                    if (!found) {
                        return false;
                    }
                }

                return true;
            }

答案 1 :(得分:1)

对于在多列中搜索(对于通过ajax从服务器检索的数据),我已完成以下操作:

使用过滤器代码,如下所示: http://mleibman.github.com/SlickGrid/examples/example4-model.html,但将“myFilter”函数中的代码更改为:

function myFilter(item, args) {
    if (args.searchString != "" && item["field1"].indexOf(args.searchString) == -1 &&
                                   item["field2"].indexOf(args.searchString) == -1 &&
                                   item["field3"].indexOf(args.searchString) == -1)                                         
    {
        return false;
    }
    return true;

}

等等......希望这会有所帮助!

答案 2 :(得分:0)

这适用于空间分隔符:

var grid;
var dataView;
var searchList = [];

function myFilter(item, args) {
    var mnull = 0, optnull = 0, l = searchList.length, len = grid.getColumns().length;
    for (var i = 0; i < l; i++) {
        for (var j = 0; j < len; j++) {
            if (item[grid.getColumns()[j].field].toLowerCase().indexOf(searchList[i]) == -1) {mnull++}
        }
        if (mnull == len) {optnull++}
        mnull = 0;
    }
    /* For searching like with operator "OR" 
    if (optnull == l && optnull != 0) {return false;}
    */
    if (optnull != 0) {return false;}
    return true;
}

jQuery(function ($) {
    dataView = new Slick.Data.DataView();
    grid = new Slick.Grid($("#container"), dataView, columns, options);

    $("#txtSearch").keyup(function (e) {
        // clear on Esc
        if (e.which == 27) {
            this.value = "";
        }
        searchList = $.trim(this.value.toLowerCase()).split(' ');
        dataView.refresh();
    });

    dataView.beginUpdate();
    dataView.setItems(data);
    dataView.setFilter(myFilter);
    dataView.endUpdate();
});