du8864 2012-12-02 03: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,

  1. $dataProvider=new CSqlDataProvider($query, array(
  2. 'totalItemCount'=>$count,
  3. 'sort'=>array('defaultOrder'=>'caseid DESC',
  4. 'attributes'=>array(
  5. 'caseid',
  6. 'trackid',
  7. 'status',
  8. ),
  9. ),
  10. ));

In view file,

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

1条回答 默认 最新

  • dthjnc306679 2012-12-02 08: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.

    展开全部

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部