0

我按照这个例子:http ://www.datatables.net/development/filtering

但它不起作用。如果您输入“a”作为搜索键盘,所有行仍会显示在表中,因为它会搜索 html 标记<a>。我错过了什么?

<!doctype html>
<html>
<head>
<link rel="stylesheet" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css">
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script>

<script>
        $.fn.dataTableExt.ofnSearch['string'] = function ( sData )
        {
            return sData.replace(/\n/g," ").replace( /<.*?>/g, "" );
        };
        $(document).ready
        (
            function()
            {
                $('#search').dataTable
                (
                    {
                        aoColumns:[ 
                                    {'sType': 'string'}
                                ]
                    }
                );
            }
        );
</script>


</head>
<body>

<table id="search">
    <thead>
        <tr>
            <th>search</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><a href="www.google.com">b</a></td>
        </tr>
        <tr>
            <td><a href="www.google.com">c</a></td>
        </tr>
        <tr>
            <td><a href="www.google.com">d</a></td>
        </tr>
        <tr>
            <td><a href="www.google.com">e</a></td>
        </tr>
        <tr>
            <td><a href="www.google.com">f</a></td>
        </tr>
        <tr>
            <td><a href="www.google.com">g</a></td>
        </tr>
    </tbody>
</table>
</body>
</html>
4

2 回答 2

2

从源代码中得到这个

* Note that as of v1.9, it is typically preferable to use <i>mData</i> to prepare data for
* the different uses that DataTables can put the data to. Specifically <i>mData</i> when
* used as a function will give you a 'type' (sorting, filtering etc) that you can use to 
* prepare the data as required for the different types. As such, this method is deprecated.
*  @type object
*  @default {}
*  @deprecated
*
*  @example
*    $.fn.dataTableExt.ofnSearch['title-numeric'] = function ( sData ) {
*      return sData.replace(/\n/g," ").replace( /<.*?>/g, "" );
*    }
*/
"ofnSearch": {},

所以我想出了这个

var filterFunc = function ( sData )
{
    return sData.replace(/\n/g," ").replace( /<.*?>/g, "" );
};
$(document).ready
(

function()
{
    $('#search').dataTable
    (
        {
            aoColumns:[ 
                {
                    'mData': function(source, type, val){
                        if (type === 'set') {
                          source.value = val;
                          source.value_display = val;
                          source.value_filter  = val=="" ? "" : filterFunc(val);
                          return;
                        }
                        else if (type === 'display') {
                          return source.value_display;
                        }
                        else if (type === 'filter') {
                          return source.value_filter;
                        }
                        // 'sort', 'type' and undefined all just use the integer
                        return source.value;
                    }

                }
            ]
        }
    );
}
);

这是小提琴http://jsfiddle.net/pJG9f/1/

于 2013-05-28T04:24:36.800 回答
0

可能你应该为你的“a”或者$selector.find (a).text()

于 2013-05-28T03:18:14.887 回答