⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

callbackColumn URL help



GaryW
  • profile picture
  • Member

Posted 16 August 2019 - 00:55 AM

Thank you for reading, I hope you can help, my goal is to change a column to a url.

 

I have two tables, batch and fginventory, I have a filename in the coafile column eg coa1234.pdf inside the batch table.

 

batch:

batch_id

name

coafile

 

fginventory

name

coafile_fk

 

fginventory has a relationship : 

 

$crud->setTable('fginventory');

$crud->setRelation('coafile_fk','batch','coafile');

$crud->callbackColumn('coafile_fk', function($value, $row) {
return "<a href='http://urlsite/". $row->coafile_fk ."' target='blank'>$value</a>";
});

 

I am getting the index key back, not the file file name.

 

To add, if i remove the callbackColumn the filename displays but without a URL link


AndresSEGC
  • profile picture
  • Member

Posted 16 August 2019 - 09:17 AM

You need a custom model: https://www.grocerycrud.com/enterprise/examples-3/create-custom-model/

 

In your controller

        // Please notice that the APPPATH is used only in Codeigniter Framework
        include(APPPATH . 'models/CustomModel.php');
        $db = $this->_getDbData();
        // If we are not using namespaces, we need to make sure that we are starting with "\" as
        // this way we can guarantee that it will not fail in case we will use namespaces at the future
        $model = new \CustomModel($db);
        $crud->setModel($model);

In the custom model

<?php
//CustomersModel.php
use GroceryCrud\Core\Model;
use GroceryCrud\Core\Model\ModelFieldType;
class CustomModel extends Model {
    protected $ci;
    protected $db;
    function __construct($databaseConfig) {
        $this->setDatabaseConnection($databaseConfig);
        $this->ci = & get_instance();
        $this->db = $this->ci->db;
    }
   public function getFieldTypes($tableName)
    {
        $fieldTypes = parent::getFieldTypes($tableName);
        $fullNameFieldType = new ModelFieldType();
        $fullNameFieldType->dataType = 'varchar';
        //$fieldTypes['group_id'] = $fullNameFieldType; 
        return $fieldTypes;
    }    
	public function getList() {
	
		$order_by = $this->orderBy;
		$sorting = $this->sorting;
		// All the custom stuff here
        $this->db->select('*');
        $this->db->join('groups', 'groups.group_id = users.group_id', 'left');
		$this->db->group_by('users.user_id');
		
		if($order_by == 'group_id'){
			$order_by = 'groups`.group_id';
		    $this->db->order_by($order_by. " " . $sorting);			
		}else if ($order_by !== null) {
		    $this->db->order_by($order_by. " " . $sorting);
		}
		
		if (!empty($this->_filters)) {
		    foreach ($this->_filters as $filter_name => $filter_value) {
			    //print_r($filter_name);
			    if($filter_name == 'group_id'){
				    $filter_name = 'description';
				    $filter_value = str_replace("=", "", $filter_value);
			    }
		        $this->db->like($filter_name, $filter_value);
		    }
		}
		
		if (!empty($this->_filters_or)) {
		    foreach ($this->_filters_or as $filter_name => $filter_value) {
		        $this->db->or_like($filter_name, $filter_value);
		    }
		}
		
		$this->db->limit($this->limit, ($this->limit * ($this->page - 1)));
		$results = $this->db->get($this->tableName)->result_array();
		return $results;
	
	}
}


And the callback

	function _crud_users_group($value, $row)
	{
		if($value == 2)
		{
			return "$row[description] <i class='fas fa-portrait' style='color:#348fe2'></i> <a href='".site_url('admin/asignar_clientes_jefe_equipo/edit/'.$row['user_id'])."'>Asignar clientes</a>";
		}
		else
		{
			return $row['description'];
		}
	}