blue-sky
blue-sky

Reputation: 53876

Text being ignored when searching - datatable

I'm reducing the length of a string to 20 characters prior to adding the data to a datatable - http://www.datatables.net/. When I search the datatable(by typing into the search box) the truncated data is no longer searchable. Is it possible to add truncated data to a datatable without causing the datatable to ignore the truncated text when searching.

This is the code im using to add data to the datatable :

if(data.length > 20){
data= data.substring(0 , 20);
}
$('#myTable').dataTable().fnAddData( [
data            
]
); 

Problem is the data that is truncated is not searchable.

Upvotes: 0

Views: 783

Answers (1)

Tats_innit
Tats_innit

Reputation: 34107

Hiya Working Demo : http://jsfiddle.net/Gnazp/5/

search for foobar1 or foobar2 etc which are hidden on the table display but they are part of the column as you can see in jsfiddle code.

You can read the comment in the code but essentially the trick that code is doing is to bind the data with the filter first so that the filter has info about your full text and then truncate the string, so it appears as ou want it yo appear: (If you will truncate the string before filter it will forget about the data you got in your substring, hope the sample code will exaplain better in jsfiddle)

Note: Please let me know I can copy paste all the code here in my post.

Explanation

in this case:

<td class="truncatethis">test yes you can, foobar foonewbar.</td>

Only displays: test yes y on the datatable screen but if you search for foobar it will give you the correct results.

To display the truncated screen I have called the substring method after the filter is called: Look for the code at the bottom of that jsfiddle:

$(".truncatethis").each(function(){

    if($(this).text().length > 10){
               //alert($(this).html());
               $(this).html($(this).text().substring(0 , 10));
        }

});​

Hope this helps, Cheers!

Upvotes: 2

Related Questions