Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 0.9.3

Request #6720 formatter with array parameters
Submitted: 2006-02-07 23:50 UTC
From: chrismir Assigned: olivierg
Status: Closed Package: Structures_DataGrid (version 0.6.3)
PHP Version: 4.4.0 OS: Suse 10.0
Roadmaps: (Not assigned)    

 [2006-02-07 23:50 UTC] chrismir
Description: ------------ I guess I'm pushing the limits of the formatter functionality, or I do not see how it will be possible with the current code. I would like to be able to pass an (associative) array as parameter to the formatter. This would allow me to pick the correct text to be printed in the column based on record data which can be referenced to the array. Test script: --------------- $list=array('active','open','closed'); $column->setFormatter('printAssoc', $list); function printAssoc($params) { extract ($params); print $list[$record[$fieldName]]; }


 [2006-02-08 14:52 UTC] at php dot net
Using array is indeed not possible yet. Wouldn't a "global $list;" in printAssoc() or defining $list in printAssoc() solve your problem?
 [2006-02-08 14:54 UTC] wiesemann
Just for the records: The last comment came from me.
 [2006-02-08 21:20 UTC] chrismir
"...or defining $list in printAssoc() solve your problem?" That is about how I do it now, although I have to make specific formatters for each column that need to display an associative name (e.g. _formatYesNo or _formatStatus). 'printAssoc' would be a more generic library class function. p.s. I noticed you also include fieldName, columnName etc to the paramList. Great! This obviously is needed for creating generic formatters where the field name is not known.
 [2006-02-08 21:29 UTC] wiesemann
Okay, maybe we can make the formatter more flexible in the future. For now, the work on the refactoring of the rendering layer is more important.
 [2006-05-25 18:57 UTC] olivierg at php dot net (Olivier Guilyardi)
I agree with Chrisje about this. This second argument would make setFormatter() ressemble call_user_func().
 [2006-05-25 23:25 UTC] olivierg at php dot net (Olivier Guilyardi)
This is fixed in CVS. You can now do : $data = array(...); $column->setFormatter($callback, $data); Or : $column = new Structures_DataGrid_Column(null, null, null, null, null, $callback, $data); Thanks for your report
 [2006-08-24 07:35 UTC] riku dot tuominen at benchmarking dot fi (rippe)
Any confirmation on that this works? I cant get my test code to work. $dg->addColumn(new Structures_DataGrid_Column('Description', 'description', 'description', null, null, 'printTest', array(length=>150))); function printTest($params) { extract($params); echo "length: " . $length; }
 [2006-08-24 07:40 UTC] riku dot tuominen at benchmarking dot fi (rippe)
Forgot to mension that $length paramether return some reason empty string.
 [2006-08-24 08:49 UTC] wiesemann (Mark Wiesemann)
rippe, at first: array(length=>150) should be array('length' => 150), but that's not the problem here, as PHP generates only a notice and is smart enough to guess that you mean the string 'length' instead of the undefined constant length. The problem is your formatter. This should work: function printTest($params, $args) { extract($args); echo "length: " . $length; } User defined arguments like your array with the length information are available via the new second parameter that is passed to the formatter callbacks.
 [2006-08-24 09:32 UTC] riku dot tuominen at benchmarking dot fi (Rippe)
Excellent wiesemann! I would newer have quesses how to fix it using my litle knowledge or the offical pear documentation. I found on Internet old example which I changed so it is compatiple with new datagrid version. It would be nice if the offical datagrid example could be reedited to include a extra $formatterArg example. That would help others a lot. This works for me, hopefully this helps some one struling with same task. $dg->addColumn(new Structures_DataGrid_Column('Description', 'description', 'description', null, null, 'printDesc', array('length'=>15))); function printDesc($params, $args) { extract ($args); extract ($params); if (strlen($record['description']) > $length) { return nl2br(substr($record['description'], 0, $length)) . '...'; } else { return nl2br($record['description']); } } Thanks again wiesemannn for the fast fix!
 [2006-08-24 11:43 UTC] wiesemann (Mark Wiesemann)
> Excellent wiesemann! You may call me Mark. ;-) > I would newer have quesses how to fix it using my litle > knowledge or the offical pear documentation. Good point, thanks for your example code. I've add an item on my ToDo list about updating the example, or adding another one. This might take some time, but I won't forget it. BTW: Although this was directly related to this request, please ask support questions on Thanks and regards, Mark
 [2006-08-24 18:04 UTC] riku dot tuominen at benchmarking dot fi (rippe)
OK thanks mark ;-)