Search code examples
atk4agiletoolkit

Agile Toolkit manipulating Grid column content


Just started using ATK4 and appreciating it very much so far, but not sure how to do this...

What I am trying to accomplish:

I am outputting a query's results to a grid, one of the fields is 'status', the data will either be '-1' or '1'. Instead of outputting -1 or 1 to the column, how do I output an HTML snippet (or whatever I need to to get what I want) instead that shows a different icon for each value? In short:

In column 'status':

  • if the value is -1, display iconDown.gif;
  • if the value is 1, display iconUp.gif

Code so far:

class page_showlist extends Page {
function init(){
    parent::init();

    $q=$this->api->db->dsql();
    $q->table('remote_system')
            ->join('customers.id','customer_id')
            ->field('customer_id')
            ->field('ip')
            ->field('nickname')
            ->field('name','customers')
            ->field('status')
    ;

    $grid = $this->add('Grid');

    $grid->addColumn('text','status')->makeSortable();
    $grid->addColumn('text','name')->makeSortable();
    $grid->addColumn('text','ip');
    $grid->addColumn('text','nickname');
    $grid->addButton('Reload Grid')->js('click',$grid->js()->reload());
    $grid->addQuickSearch(array('name'));
    $grid->setSource( $q );
    }

}

Any pointers/tips?


Solution

  • To add column with icons in Grid you can use custom template.

    In one of my projects I do like this:

    $url = $this->api->pm->base_url . $this->api->locateURL('template', 'images/');
    $grid->addColumn('template', 'type', false)
        ->setTemplate('<img src="' . $url . 'icon_object_<?$type?>.png">');
    

    It'll use model field named type (in your case use status) and show icons in that column. Icon source URL is generated dynamically and it'll search for image files in your template/images directory named icon_object_XXX.png where XXX value will be taken from field type value.

    In my case type is like this: array('building','apartment','land','garage') etc.

    And one more thing - you should start using Models whenever possible! That way you'll ease your life later when your project becomes bigger. Also can have extra security (conditions, etc.) with them.