Yii Framework Forum: Right-alignment for numeric fields - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Right-alignment for numeric fields Rate Topic: -----

#1 User is offline   Milan Babuskov 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 04-November 09

Posted 04 November 2009 - 06:33 PM

Hi,

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.

Thanks,
M.
0

#2 User is offline   Milan Babuskov 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 04-November 09

Posted 04 November 2009 - 06:44 PM

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?
0

#3 User is offline   jayrulez 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 348
  • Joined: 29-July 09

Posted 04 November 2009 - 08:49 PM

View PostMilan Babuskov, on 04 November 2009 - 06:44 PM, said:

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

echo CHtml::activeTextField($model,'cena', 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;}
php:
foreach(array('cat', 'dog', 'cow') as $animal) echo $animal."\n";

python:
[(animal, print(animal)) for animal in ['cat', 'dog', 'cow']]

ruby:
['cat', 'dog', 'cow'].each {|animal| puts animal}


You say Tomato, I say Tomato.
0

#4 User is offline   Milan Babuskov 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 04-November 09

Posted 04 November 2009 - 09:25 PM

View Postjayrulez, on 04 November 2009 - 08:49 PM, said:

apply a class to numberic fields eg: class="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.

Thanks,
M.
0

#5 User is offline   jayrulez 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 348
  • Joined: 29-July 09

Posted 04 November 2009 - 10:24 PM

View PostMilan Babuskov, on 04 November 2009 - 09:25 PM, said:

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.

Thanks,
M.


That's a bit application or individual specific isn't it?
php:
foreach(array('cat', 'dog', 'cow') as $animal) echo $animal."\n";

python:
[(animal, print(animal)) for animal in ['cat', 'dog', 'cow']]

ruby:
['cat', 'dog', 'cow'].each {|animal| puts animal}


You say Tomato, I say Tomato.
0

#6 User is offline   Milan Babuskov 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 04-November 09

Posted 05 November 2009 - 01:24 AM

View Postjayrulez, on 04 November 2009 - 10:24 PM, said:

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.
0

#7 User is offline   PoL 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 506
  • Joined: 05-November 08
  • Location:Buenos Aires, Argentina

Posted 05 November 2009 - 07:03 AM

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
Don't say what you think, think what you say
The problem is communication! Excess of communication!
0

#8 User is offline   lucifurious 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 184
  • Joined: 15-March 09
  • Location:Atlanta, GA.

Posted 09 November 2009 - 12:43 PM

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

#9 User is offline   jonah 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 733
  • Joined: 27-November 08
  • Location:California (US)

Posted 09 November 2009 - 02:15 PM

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
http://php-thoughts.cubedwater.com - my bloggings about Yii
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users