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

查看:51
本文介绍了如何在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).

谢谢,奥列格.

推荐答案

在我以前的演示显示了重音免费搜索和排序.如果使用相同的技术,则可以覆盖一些用于搜索的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天全站免登陆