Magento Admin:通过添加列限制网格中的字符

Magento Admin: Limit Characters in Grid via Add Column

我们正在使用 Magento CE 1.9.1。

我似乎找不到这个问题的答案。

我为模块管理网格添加了一列。我想在数据填充单元格时限制字符串的长度。

$this->addColumn 是否有允许这样做的选项?我找到的唯一建议是 string_limit,但没有用。

编辑

这是我看到的。

我想限制此字段中的可见字符,这样我就不会显示整个字符串。

protected function _prepareColumns()中生成此列的代码:

$this->addColumn('testimonial', array(
    'header'       => Mage::helper('testimonial')->__('Testimonial'),
    'align'        => 'left',
    'index'        => 'testimonial',
));

我发现要完成我想要的任务的唯一建议是将 'string_limit' => '{some number}' 添加到 addColumn 选项数组。没用。

您必须覆盖本地文件夹中的文件或仅将 app\code\core\Mage\Adminhtml\Block\Testimonial\Grid.php 复制到 app\code\local\Mage\Adminhtml\Block\Testimonial\Grid.php

$this->addColumn('name', array(
    'header'    => Mage::helper('testimonial')->__('Testimonial'),
    'index'     => 'testimonial'
));

替换为

$this->addColumn('namewithprifx', array(
   'header'    => Mage::helper('testimonial')->__('Testimonial'),
    'index'     => 'testimonial',
    'type'  => 'text',      
    'width' => '250px',
    'sortable'  =>false,
    'filter' => false,                                         
    'renderer' => 'NameSpace_Customergrid_Block_Adminhtml_Renderer_Namewithprifx',          
    ));

在 NameSpace\Customergrid\Block\Adminhtml\Renderer\Namewithprifx 中创建文件。php

    <?php
 class NameSpace_Customergrid_Block_Adminhtml_Renderer_Namewithprifx extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
 {
   public function render(Varien_Object $row)
   {

    //$getData = $row->getData();               
    $str=$row->getData('prefix'). " ". $row->getData('testimonial');
    return $str; //you can use substr or any php function here
  }
}

清除缓存并尝试