如何对jqgrid中的格式化列值进行本地搜索? [英] how to do local search on formatted column value in jqgrid?

查看:15
本文介绍了如何对jqgrid中的格式化列值进行本地搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 loadonce 预先获取所有数据,然后在本地进行排序和过滤.

I'm using loadonce to get all data up front and then doing sorting and filtering locally.

我的一个列值是一个对象数组.在 colModel 选项中,我使用如下所示的格式化函数:

One of my column values is an array of objects. In the colModel option I use a formatter function that looks like this:

function my_formatter(cellValue) 
{ 
    return $.map(cellValue, function(element) {return element.human_readable_name;}).join(', '); 
}

我还使用了一个自定义排序函数,它只返回数组的长度.

I also use a custom sorting function that simply returns the length of the array.

我遇到的问题是工具栏过滤和多字段对话框过滤不起作用.他们似乎在搜索 [objects].toString() 而不是格式化的值.所以当我搜索[object Object]"时我会得到点击,但当我搜索实际值时不会.

The problem I have is that the toolbar filtering and multi-field dialog filtering aren't working. They seem to be searching on [objects].toString() rather than the formatted value. So I get hits when I search for "[object Object]", but not when I search for the actual values.

有没有办法让本地过滤使用格式化的值?

Is there a way to get the local filtering to use the formatted value?

根据 Oleg 的回复进行

我修改了 Oleg 的代码以添加每列过滤器格式.它似乎运作良好.我删除了 _toStr 替换,因为它似乎没有必要——我认为它是用来修改搜索词的(这在 Oleg 的重音去除案例中是有意义的,但在我的案例中不是).

I adapted Oleg's code to add per-column filter formatting. It seems to work well. I removed the _toStr replacement because it didn't seem necessary -- I think it's used to modify the search term (which makes sense in Oleg's accent-stripping case, but not in mine).

// Causes local filtering to use custom formatters for specific columns.
// formatters is a dictionary of the form:
// { "column_name_1_needing_formatting": "column1FormattingFunctionName",
//   "column_name_2_needing_formatting": "column2FormattingFunctionName" }
// Note that subsequent calls will *replace* all formatters set by previous calls.
function setFilterFormatters(formatters)
{
    function columnUsesCustomFormatter(column_name)
    {
        for (var col in formatters)
        {
            if (col == column_name)
            return true;
        }
        return false;
    }

    var accessor_regex = /jQuery.jgrid.getAccessor(this,'(.+)')/;

    var oldFrom = $.jgrid.from;
    $.jgrid.from = function(source, initialQuery) {
        var result = oldFrom(source, initialQuery);
        result._getStr = function(s) {
            var column_formatter = 'String';

            var column_match = s.match(accessor_regex, '$1');
            if (column_match && columnUsesCustomFormatter(column_match[1]))
            {
                column_formatter = formatters[column_match[1]];
            }

            var phrase=[];
            if(this._trim) {
                phrase.push("jQuery.trim(");
            }
            phrase.push(column_formatter+"("+s+")");
            if(this._trim) {
                phrase.push(")");
            }
            if(!this._usecase) {
                phrase.push(".toLowerCase()");
            }
            return phrase.join("");
        }

        return result;
    }; 
}

它被这样调用:

setFilterFormatters({'column_with_array_of_objects':'my_formatter'});

测试表明这适用于包含"、不包含"、等于"、不等于"(可能还有开头为"和其他简单的字符串比较——但我没有使用它们).

Testing suggests that this works for 'contains', 'does not contain', 'equals', 'does not equal' (and probably 'begins with' and the other simple string comparisons -- but I'm not using them).

谢谢,奥列格.

推荐答案

在我以前的 answer 在 trirand 论坛中,我描述了如何实现自定义本地过滤和排序.演示展示了无重音搜索和排序.如果您使用相同的技术,您可以覆盖一些用于搜索的 jqGrid 函数(例如 _toStr_getStr)并实现它,以便在数组的情况下您将使用自己的它的实现.

In my old answer in the trirand forum I described how to implement custom local filtering and sorting. The demo shows accent free searching and sorting. If you use the same technique you can overwrite some jqGrid functions used for searching (_toStr and _getStr for example) and implement it so that in case of arrays you will use your own it's implementation.

为了让我的回答对 Google 更友好,我添加了小代码片段

To make my answer more Google friendly I include small code fragment

function myAccentRemovement(s) {
    // the s parameter is always string
    s = s.replace(/[àáâãäå]/gi,'a');
    s = s.replace(/[èéêë]/gi,'e');
    s = s.replace(/[ìíîï]/gi,'i');
    s = s.replace(/[òóôõöø]/gi,'o');
    s = s.replace(/[ùúûü]/gi,'u');
    s = s.replace(/[ýÿ]/gi,'y');
    s = s.replace(/æ/gi,'ae');
    s = s.replace(/œ/gi,'oe');
    s = s.replace(/ç/gi,'c');
    s = s.replace(/š/gi,'s');
    s = s.replace(/ñ/gi,'n');
    s = s.replace(/ž/gi,'z');
    return s;
}
//...
var oldFrom = $.jgrid.from;
$.jgrid.from = function(source,initalQuery){
    var result = oldFrom(source,initalQuery);
    var old_toStr = result._toStr;
    result._toStr=function(s) {
        return myAccentRemovement(old_toStr(s));
    };
    result._getStr=function(s) {
        var phrase=[];
        if(this._trim){
            phrase.push("jQuery.trim(");
        }
        phrase.push("myAccentRemovement(String("+s+"))");
        if(this._trim){
            phrase.push(")");
        }
        if(!this._usecase){
            phrase.push(".toLowerCase()");
        }
        return phrase.join("");
    }
    return result;
}

这将解决问题.我无法为您提供更准确的建议,因为您没有发布有关您使用的数据的确切结构的信息.

It will be solve the problem. I can't gives you more exact suggestions because you posted no information about the exact structure of data which you use.

这篇关于如何对jqgrid中的格式化列值进行本地搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆