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 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集