du8864 2012-12-02 11:46
浏览 43
已采纳

Yii,CsqlDataProvider中的排序和列名称

I had trying to use cgridview with csqldataprovider.

  1. my sql query takes data from different tables. I had used alias in query. So in gridview when i click on header value, it takes column name for sorting. i.e, column name = Track it will create query with ... order by Track but actually it should create ...order by t.trackname.
  2. To add more complexity i have column name in table as table name. E.g In same db with table name Store and col name trackid i have another table in same db with table name trackid.
  3. I had checked solution. Here in cgridview when I specify my column attribute it is not becoming sort-able (no anchor tags). Without column attribute it is sort-able.

In controller file,

$dataProvider=new CSqlDataProvider($query, array(
        'totalItemCount'=>$count,
        'sort'=>array('defaultOrder'=>'caseid DESC',
            'attributes'=>array(
             'caseid',
             'trackid',
             'status',
            ),
        ),
        ));

In view file,

$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'ajaxUpdate'=>true,
'columns'=>array(
    array('header'=>'Case','name'=>'Case','value'=>'$data[caseid]'),
    array('header'=>'Track','name'=>'Track','value'=>'$data[trackid]'),
    array('header'=>'Status','name'=>'Status','value'=>'$data[status]'),
),
  • 写回答

1条回答 默认 最新

  • dthjnc306679 2012-12-02 16:28
    关注

    You have to specify the name for each column of CGridView correctly. In your sample code it is incorrect, you have used 'name'=>'Case' whereas it should be 'name'=>'caseid' , i.e the value of name should be the exact value of the alias you specify in your sql.

    Update:

    1. Also note that incase you are not doing any formatting, or modifying the value retrieved from db, you do not need the value attribute. The name attribute will take care of getting the correct value from the db result set. The header will take care of displaying the column header name, which in your case is Case.

      Example:

      'columns'=>array(
          array('header'=>'Case','name'=>'caseid'), // 'value'=>'$data[caseid]'),
          array('header'=>'Track','name'=>'trackid'), // 'value'=>'$data[trackid]'),
          array('header'=>'Status','name'=>'status'), // 'value'=>'$data[status]'),
      ),
      
    2. To handle situations 1 and 2 use alias(es) in your sql, and use those aliases' values in sort's attributes array, and also in name.

      Sql example:

      select t.trackname as some_alias ...
      

      Dataprovider:

      'sort'=>array(
          'attributes'=>array(
              'some_alias',
              // ... more attributes ...
          ),
          // ... more options ...
      ),
      

      CGridView:

      'columns'=>array(
          array(
              'header'=>'Some Header Name',
              'name'=>'some_alias',
              'value'=>'$data["some_alias"]' // as mentioned above this is unnecessary if you are not modifying the value
          )
      )
      

      So when you sort, queries generated will be like : order by some_alias, which is perfectly fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化