如何在使用stwe / DatatablesBundle生成的表格中为行着色?

时间:2017-05-05 09:15:49

标签: symfony datatables bundle

如何为使用stwe / DatatablesBundle生成的表格中的行着色?我希望在status.id === 1时实现像绿色标记的整行(添加适当的类?),并在status.id === 2时标记为绿色。我想使用LineFormatter({{3} }) - 不回调。请注意,status.status_id列不可见(它只与数据库中的status.name相关)。

这是我的Datatable.php文件:

public function buildDatatable(array $options = array())
{

    $this->features->set(array(
        'auto_width' => true,
        'defer_render' => false,
        'info' => true,
        'jquery_ui' => false,
        'length_change' => true,
        'ordering' => true,
        'paging' => true,
        'processing' => true,
        'scroll_x' => false,
        'scroll_y' => '',
        'searching' => true,
        'state_save' => false,
        'delay' => 0,
        'extensions' => array(),
        'highlight' => false,
        'highlight_color' => 'red'
    ));

    $this->ajax->set(array(
        'url' => $this->router->generate('proposal_results'),
        'type' => 'GET',
        'pipeline' => 0
    ));

    $this->options->set(array(
        'display_start' => 0,
        'defer_loading' => -1,
        'dom' => 'lfrtip',
        'length_menu' => array(10, 25, 50, 100),
        'order_classes' => true,
        'order' => array(array(0, 'asc')),
        'order_multi' => true,
        'page_length' => 10,
        'paging_type' => Style::FULL_NUMBERS_PAGINATION,
        'renderer' => '',
        'scroll_collapse' => false,
        'search_delay' => 0,
        'state_duration' => 7200,
        'stripe_classes' => array(),
        'class' => Style::BOOTSTRAP_3_STYLE,
        'individual_filtering' => false,
        'individual_filtering_position' => 'head',
        'use_integration_options' => true,
        'force_dom' => false,
        'row_id' => 'id'
    ));

    $this->columnBuilder
        ->add('id', 'column', array(
            'title' => 'ID',
        ))
        ->add('client.name', 'column', array(
            'title' => 'Client name',
        ))
        ->add('client.surname', 'column', array(
            'title' => 'Client surname',
        ))
        ->add('client.code', 'column', array(
            'title' => 'Client Code',
        ))
        ->add('date_add', 'datetime', array(
            'title' => 'Date added',
            'date_format' => 'LLL'
        ))
        ->add('status.name', 'column', array(
            'title' => 'Status',
        ));
//            ->add('status.status_id', 'column', array(
//                'title' => 'Status Status_id',
//            ))
}

1 个答案:

答案 0 :(得分:2)

您可以通过将getLineFormatter添加到Datatable.php文件来解决此问题:

public function getLineFormatter()
{
    $formatter = function ($line) {
        if($line['status_id'] == 1) {
           $line['DT_RowClass'] = 'class_name';
        }
        return $line;
    };
    return $formatter;
}

为了使其正常工作,您需要将status_id添加为列或隐藏列。