Cut cell at certain character limit in results view

drashrafsabry

Well-known member
dadabik pro 9.1

i have lots of txt in some fields of my record , so when it shows as result view , the record is very long and the fields with the txt fill up the page to an extent i have to scroll down a lot to get the end of it till the next record.... is there a way to limit the number of characters in the a cell in results view so that it cuts or ends with ....(More) . and i would know that there is more and open the record
 

eugenio

Administrator
Staff member
Hello,
you can use a custom formatting function, something like

return substr_custom($value, 0, 300).'...';

returns only the first 300 chars for example. You would get the text cut in the details page too, though, which probably is not what you want.
You can therefore put your code in an if statement like the following one

if ($GLOBALS['function'] === 'search'){

(not tested but it is supposed to apply the function only to the results grid.

Best,
 

drashrafsabry

Well-known member
ok i figured out how to return the result as whole in edit view


but now the problem is when i use the above , it looses line breaks !! , how can i solve this , the text shows as one part not with the original line breaks
 

drashrafsabry

Well-known member
is there any ways that in the else part it would return as a whole with the same formatting and line breaks and everything , i dont want to use the substr_custom in the else part


function dadabik_truncate($value){


if ($GLOBALS['function'] === 'search'){
return substr_custom($value, 0, 200).'<b>...MORE</b>';
}
else
{
return substr_custom($value);

}

}
 

eugenio

Administrator
Staff member
The edit view the value is not impacted by the custom formatting function so I am not sure why you need that. Maybe you are referring to the details page?

To return the originale value

return $value;
 

drashrafsabry

Well-known member
function dadabik_truncate($value){


if ($GLOBALS['function'] === 'search'){
return substr_custom($value, 0, 200).'<b>...MORE</b>';
}
else
{
return $value;

}

}

stll looses LINE BREAKS in details page
 

eugenio

Administrator
Staff member
You need nl2br:

if ($GLOBALS['function'] === 'search'){
return nl2br(substr_custom($value, 0, 200).'<b>...MORE</b>');
}
else
{
return nl2br($value);
}
 

drashrafsabry

Well-known member
A new issue
The function works great in all conditions , but now if by change i didn't enter long txt in the field , it still shows the ...More


Is there a way to show the ...More if the txt is only above 200 characters?
 

eugenio

Administrator
Staff member
You can add a condition that check the length:

if ($GLOBALS['function'] === 'search' && strlen_custom($value) > 200){
 

drashrafsabry

Well-known member
Umm a new issue is there

If i display the same table inside another details page MASTER/VIEW the truncation does not work ... but if i display it from outside alone in result form it works
 

eugenio

Administrator
Staff member
Yes, that's right, the thing is that when you are in a master/details view the main function is "details" and not "search".

Unfortunately I don't have an easy solution for this case.

Best,
 

eugenio

Administrator
Staff member
Hello,
to cover the results grid in a details page this should work:

if ($GLOBALS['function'] === 'details' && isset($GLOBALS['is_items_table']) && $GLOBALS['is_items_table'] === 1 && strlen_custom($value) > 200){

it's just a hint, I haven't tested it and without carefully analyze the code I can't guarantee on side effects. In your case, since you are just cutting chars for displaying purposes, the worst that can happen should be that you cut also in cases where you don't want.

Best,
 

eugenio

Administrator
Staff member
The code I provided was for the results grid in the master/details form, it doesn't work?
 

eugenio

Administrator
Staff member
I've just tested the solution and I confirm it works (I still cannot guarantee about side effects), please double check the code.

Best,
 
Top