使用 CArrayDataProvider 在 CGridView 中对日期列进行排序 [英] Sorting a date column in CGridView with CArrayDataProvider

查看:31
本文介绍了使用 CArrayDataProvider 在 CGridView 中对日期列进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网站上使用 CGridView 来显示通过 CArrayDataProvider 提供的数据.这样做的原因是,数据在不同的表中累积,并增加了额外的计算列.使用此代码进行排序和过滤通常可以正常工作(...省略列):

I'm using a CGridView on a website to display data provided via a CArrayDataProvider. Reason for this is, that the data is cumulated across different tables and enriched with additional computed columns. Sorting and Filtering is generally working fine with this code (... are omitted columns):

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'produkt-grid',
    'dataProvider'=>new CArrayDataProvider($betroffeneProdukte, array(
            'sort' => array(
                'attributes' => array(
                    'bezeichnung',
                    'datum',
                    ...
                ),
            ),
            'pagination' => false,
        )),
    'enableSorting' => true,
        'columns'=>array(
            array(
                'name'=>'bezeichnung',
                'header'=>'Bezeichnung',
                'value'=>'$data["bezeichnung"]',
            ),
            array(
                'name'=>'datum',
                'header'=>'datum',
                'value'=>'$data["datum"]',
            ),
            ...
        ),

));

问题是日期列.它按这样的字符串排序(德语格式):01.10.2012, 03.08.2012, 10.01.2012, 15.02.2012, ...

Problem is the date column. It is sorting as a string like this (German format): 01.10.2012, 03.08.2012, 10.01.2012, 15.02.2012, ...

我当然想像这样正确排序:10.01.2012, 15.02.2012, 03.08.2012, 01.10.2012, ...

I would like to sort it correctly like this of course: 10.01.2012, 15.02.2012, 03.08.2012, 01.10.2012, ...

有人给我出主意吗?

推荐答案

我认为您需要将原始日期/日期时间字符串从数据源发送到 CArrayDataProvider,然后使用 CGridView 对其进行格式化:

I think you'll need to send in a raw date / datetime string to your CArrayDataProvider from your data source and then format it with the CGridView instead:

array(
    'name'=>'datum',
    'header'=>'datum',
    'value'=>'$data["datum"]',
    'type'=>'date',
),

看起来您的问题是 Yii 认为您的日期是一个字符串(而不是日期).如果您转向为 DataProvider 提供日期,排序可能会自行解决.

It looks like your problem is that Yii thinks your date is a string (instead of a date). If you shift to providing the DataProvider a date, the sorting may work itself out.

这篇关于使用 CArrayDataProvider 在 CGridView 中对日期列进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆