Customizing the display of content types

Posted on December 15, 2019

The administration page of the content table has new ways now to customize the display of the content. Until version 1.11.2 you could add some javascript as a text in table schema and then the front-end would run it with eval() function.

But now you can create function in javascript file and return the hypertext you want to display in a cell.

Include the javascript file

In the same path with schema file you create the js file with the same basename. For example, if schema file is mytable.php you js file should be mytable.js

If the table already exists from another package, you can add the js file on the schema from your load.php file:

gila::contentInit('mytable', function(&$table){
  $table['js'] = array_merge($table['js'], ['mypackage/js/custome.js']);
});

 

Case 1: Display a thumbnail

For a field wih name that has for value url of images:

gtableFieldDisplay.thumbnail = function(row) {
  if(row.thumbnail==null) return '';
  return '<img class="lazy" data-src="lzld/thumb?f='+row.thumbnail+'"></img>';
}

 

Case 2: Colorize numbers

You want to color green a positive number and red a negative:

gtableFieldDisplay.percentChange = function(row) {
  if(row.percentChange>0) return '<span style="color:green">'+row.percentChange+'</span>';
  if(row.percentChange<0) return '<span style="color:red">-'+row.percentChange+'</span>';
  return '<span> '+row.percentChange+'</span>';
}