Jump to content

Truncating Text


LewisP

Recommended Posts

Posted

So I'm trying to truncate text for a datatable connected to a database so that entries that are long don't ruin the styling of the table

Example of untruncated text:

XLSYqsb.png

 

I found this documentation on a div class inside IPS suite for truncating, so I went ahead and edited my pages template to the following:

  	<div class='ipsDataItem_generic ipsDataItem_custom data-ipsTruncate'>
    	{$row['whatever']}
	</div>

And it had no effect (original 160 chars, new div style still 160)

 

Any help would be appreciated, I'm looking to truncate to 20 characters max

Posted
Quote

size
(Mixed; optional; default 100)

Specifies the height the element should be; the excess text is truncated. Three types of value are accepted:

  • Selector, e.g. #someElementID
    When a selector is provided, the height of the widget element will be set to the outerHeight of the first element that matches the selector.
  • Lines value, e.g. 3 lines
    Text can be truncated to a certain number of lines of text, taking into account current formatting applied to the text (Note: lines is always plural).
  • Number e.g. 100
    A simple number value can be provided, which will truncate the text to that number of pixels.

 

So you can't use it to truncate by number of characters. You need to use php code instead if you want to do that.

Posted
8 minutes ago, DawPi said:

So you can't use it to truncate by number of characters. You need to use php code instead if you want to do that.

@DawPi Actually that was what I tried originally

	protected function manage()
	{
		// This is the default method if no 'do' parameter is specified
		$table = new \IPS\Helpers\Table\Db( 'cms_pages', \IPS\Http\Url::internal( 'app=appnamehere&module=modulenamehere&controller=testdev' ) );
		$table->rowsTemplate = array( \IPS\Theme::i()->getTemplate('whatev', 'whateverrr', 'front'), 'rowssss' );
		$table->classes = array('ipsDataList_zebra');
		
		$table->include = array( 'page_id', 'page_seo_name', 'page_type', 'page_theme', 'page_default');
		
		$table->sortBy      = $table->sortBy ?: 'page_id';
		$table->sortDirection = $table->sortDirection ?: 'desc';
		
		$table->parsers = array(


			'page_seo_name'	=> function($val, $chars = 20)
			{
				    if(strlen($val) > $chars) {
        			$val = $val.' ';
        			$val = substr($val, 0, $chars);
        			$val = substr($val, 0, strrpos($val ,' '));
        			$val = $val .'...';
				}
				return $text;
			},
		);
		
		/* Display */
		\IPS\Output::i()->title		= \IPS\Member::loggedIn()->language()->addToStack('aaaa');
		\IPS\Output::i()->output	= \IPS\Theme::i()->getTemplate('donate')->outgoings((string) $table);

	}

 

The result was that it just hid the text entirely. I feel like I'm making a simple mistake here

vZ1dBdB.png

Posted
			'page_seo_name'	=> function($val, $chars = 20)
			{
				    if(strlen($val) > $chars) {
        			$val = $val.' ';
        			$val = substr($val, 0, $chars);
        			$val = substr($val, 0, strrpos($val ,' '));
        			$val = $val .'...';
				}
				return $text;
			},

You're returning $text. Use $val instead. $text variable isn't specified.

return $val; 

 

Posted
1 hour ago, DawPi said:

			'page_seo_name'	=> function($val, $chars = 20)
			{
				    if(strlen($val) > $chars) {
        			$val = $val.' ';
        			$val = substr($val, 0, $chars);
        			$val = substr($val, 0, strrpos($val ,' '));
        			$val = $val .'...';
				}
				return $text;
			},

You're returning $text. Use $val instead. $text variable isn't specified.


return $val; 

 

Oops :tongue:

 

returning $val puts us back to square 1 though

		$table->parsers = array(


			'page_seo_name'	=> function($val, $chars = 20)
			{
				    if(strlen($val) > $chars) {
        			$val = $val.' ';
        			$val = substr($val, 0, $chars);
        			$val = substr($val, 0, strrpos($val ,' '));
        			$val = $val .'...';
				}
				return $val;
			},

ycIPUOR.png

Posted

You are passing $chars=10 as the second parameter, however what is actually passed to the callback function as the second parameter is an array containing the entire row. Instead of trying to define this as a function parameter, you could do something like this instead

$chars = 20;

	$table->parsers = array(
			'page_seo_name'	=> function( $val ) use( $chars )
			{
				    if(strlen($val) > $chars) {
        			$val = $val.' ';
        			$val = substr($val, 0, $chars);
        			$val = substr($val, 0, strrpos($val ,' '));
        			$val = $val .'...';
				}
				return $val;
			},

 

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...