How to assign unique id attribute to each table row of a CGridView?

醉酒当歌 提交于 2019-12-07 07:16:27

CGridView have an option called 'rowHtmlOptionsExpression' , you can declare like the followings to assign row an id

'rowHtmlOptionsExpression' => 'array("id"=>$data->id)',

It's better than hacking into 'rowCssClassExpression'

Good luck !

Modern solution (since Yii 1.1.13)

This is now possible to do using the rowHtmlOptionsExpression attribute, which allows assigning arbitrary HTML attributes to each rendered table row. For example:

'rowHtmlOptionsExpression' => '["id" => $data->id]'

Original answer (earlier versions)

Not directly possible because CGridView does not support it, but there are a couple of straightforward solutions that you can try.

Subclass CGridView (good)

Simply create your own class MyGridView extends CGridView and override the renderTableRow method to spit out ids on every row. Have a look at the stock implementation, which does for the class attribute exactly what you 'd like to do for the id attribute.

Use a CSS class instead (not so good)

Speaking of class attributes, the rowCssClassExpression property can be used to dynamically generate classes out of the box. IMHO this is a bad workaround, but it's there.

You could extend CGridView to add that functionality.

or be a bit hacky with rowCssClassExpression.

'rowCssClassExpression' => '\'" data-id="\' . $data->rowID'
acorncom

Try the information I posted here: How to set key value in CGrideView when grid is populated from table-view

In essence, as long as your dataprovider to the CGridview provides the data->id in a form that it understands, it will auto handle the $data->id stuff for you automatically so that it's easily available to javascript.

user3541170

CGridView.rowHtmlOptionsExpression is undefined

I don't think that we can use rowHtmlOptionsExpression

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!