Right-alignment for numeric fields


I’m a complete Yii newbie (started using it yesterday). I created some forms using crud command and now I want all the numeric fields to have content aligned to the right. This is done via CSS text-alignment: right but I cannot find how do to this with CHtml::activeTextField.

I even tried to google for about half an hour, learned a lot of other stuff about Yii, but not this.



I just discovered I can add any HTML attribute, so I can do it like this:

echo CHtml::activeTextField($model,‘price’, array(‘style’=>‘text-align: right’));

However, it is very cumbersome to have to do this for every numeric field. Since Yii already knows which fields are numerical (specified in rules), it would be nice to have them all aligned to right.

I hope this has already been done, but I’m failing to discover it?

apply a class to numberic fields eg: class=>"numeric" and specify a css rule like .numeric {test-align:right;}

This is exactly what I’m trying to avoid. Since Yii already knows which fields are numeric, it would be nice if it would set this class automatically. Or, at least, if “crud” command of yiic shell would add the class to generated view code. Of course, I would then specify CSS style for that class.

Can I submit a feature request somewhere? I believe this would be a useful feature for all Yii users.



That’s a bit application or individual specific isn’t it?

I don’t think so. Don’t you have numeric fileds in your databases? How do you align them in visual representation?

It is very common for numbers to be right-aligned. Take a look at Excel or OpenOffice and how they format numbers by default. Even this forum right-aligns the number of posts in each thread. Any tabular representation also has numbers right-aligned. PHP function sprintf also defaults to right-align when you specify numeric format like ‘%8d’ for example - it pads to 8 characters with spaces prepended.

BTW, same applies to "admin" view that is generated by "crud" command. All numerical columns should be right-aligned by default.

You must separate two worlds here.

The only way to align at right in html is throught some atribute (css or style or the ‘old way’ align=“right”)

Now, because it’s true that yii knows what fields are numeric, you can extend the command crud and take kare of them adding a class o whatever you want. (so when you use the extended crud, this class will be applied to al numeric fields and will respond to your css)

Yii can do magic… but you are the responsible to teach the magic!

How to extend yiic shell commands

The simplest solution is to subclass the view renderer to add the class for you in the render methods.

Scaffolding is usually only used for prototyping, so I think it’s best to keep it as simple as possible. I would recommend extending yiic by yourself - I’m sure you’ll find other things you’d like to adjust in it too and you’ll find it worth your time.

xx666xx’s method might be easier, but it has a performance hit