ATK4 How to set up custom formatters?

末鹿安然 提交于 2019-12-11 11:32:17

问题


I'm trying to pretty-up a dropdown field, and I'm not sure I'm taking the right approach. I have this model for which the field 'type' is to be a dropdown that displays a string, but stores an integer. To format the output, I've subclassed Grid as

class Model_Member extends MVCTable {
    function init() {
        parent::init();

        $this->addField('type')
            ->type('list')
            ->display(array('grid'=>'mbrtype'))
            ->caption('Member Type')
            ->listData(array('Guest','Associate','Full'))
            ;
    }
}

To format the output, I've subclassed Grid as

class Grid extends Grid_Basic {
    function format_mbrtype($field) {
        $mt=array('Guest','Associate','Full');
        $this->current_row[$field]=$mt[$this->current_row[$field]];
    }
}

When I load Member records my admin CRUD, I see this field populated with the numbers instead of the formatted data. I had expected the Controller to pick up the values passed into display() and execute my formatter instead of using the standard one.

Am I missing something here? I poked through the source, and my best guess as to the spot where things are going wrong is in MVCGrid.php, line 45. The call to formatType() on the Controller object does not include the field name as the third argument, which causes it to ignore the field's display collection's mappings and fall back to the Controller's $type_correspondence array.

Of course, I've only been working with ATK4 for a couple weeks now, so I may have just tied things together wrong. Is this the correct way of implementing a custom formatter?


回答1:


there is a problem in ATK 4.1.3:

There was actually a problem. You'll need to patch atk4-addons:

diff --git a/mvc/MVCGrid.php b/mvc/MVCGrid.php
index 90bd365..0fb0c0b 100644
--- a/mvc/MVCGrid.php
+++ b/mvc/MVCGrid.php
@@ -42,7 +42,7 @@ class MVCGrid extends Grid{
                $field=$this->getController()->getModel()->getField($field_name);
                if(is_null($field))throw new Exception_InitError("Field '$field_name' is not defined in the ".
                        get_class($this->getController()->getModel())." model");
-               if(is_null($type))$type=$this->getController()->formatType($field->datatype(),'grid');
+               if(is_null($type))$type=$this->getController()->formatType($field->datatype(),'grid',$field_name);
                if($field_name=='locked')return
                        parent::addColumn('locked','locked','');
         if($field_name=='id')$type='text';

Or open atk4-addons/mvc/MVCGrid.php, and on line 45, add 3rd argument to the call of formatType(....., $field_name);

and that should work.

Alternatively

Upgrade atk4-addons to the "master" in github, which is still on 4.1 branch.

Demo: http://codepad.agiletoolkit.org/myformat

The correct way to defining "display" field is through array('grid'=>'myfield');

Thanks for noticing the problem!



来源:https://stackoverflow.com/questions/9898718/atk4-how-to-set-up-custom-formatters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!