1

I'm building a value to display in a dropdown Select box. This value consists of a currency value, then a username. What I would like is that the usernames all start with the same indent.

Ex (what I have currently):

$1,000  UserA
$100  UserB
$10  UserC

What I would like:

$1,000  UserA
$100    UserB
$10     UserC

or even better:

$1,000  UserA
  $100  UserB
   $10  UserC

As I see the third list as the most readable.

Is there an easy way to do this without writing a function where I examine the max length, and then format it manually or a "cheat" to accomplish this?

Edit: How I generate the ViewModel items (what I pass to my Webpage)

    public ActionResult SearchCollections()
    {
        SearchCollectionGridViewModel scgvm = new SearchCollectionGridViewModel();

        var UserNamesWithValues = (from ud in db.userdetails  //build the dataset to pass to the View
                                   join usr in db.my_aspnet_users on ud.IdUsers equals usr.id                                       
                                   select new { 
                                       UserName = usr.name, 
                                       TradeCollectionValue = ud.TradeCollectionValue,
                                       NumberOfTrades = ud.NumberOfTrades,
                                       DollarValueTraded = ud.TradeValue
                                        }).ToList();

        scgvm.TradeCollectionValues = (from c in UserNamesWithValues
                                       where c.TradeCollectionValue > 0
                                       orderby c.TradeCollectionValue descending
                                       select new SelectListItem
                                       {
                                           Text = String.Format("{0,5} {1}", c.TradeCollectionValue, c.UserName).Replace(" ", "\xA0"),
                                           Value = c.UserName
                                       }).ToList();

         return View(scgvm);
     }

Edit #2: Answer found /figured out

I used the answer below plus the following two links to generate a string that was accurate in the Selectbox: (Monospace font will be a key as well)

Answer 1

Answer 2

Community
  • 1
  • 1
Mark
  • 3,123
  • 4
  • 20
  • 31
  • 1
    normally we have to insert some `tab` characters, however this will work only if we use `fixed-size` font. – King King Sep 14 '13 at 00:43
  • 1
    @KingKing As this is a Web App I'm not sure I can completely control that requirement. – Mark Sep 14 '13 at 00:44
  • are you using a web form control? – Daniel A. White Sep 14 '13 at 00:45
  • @DanielA.White It is MVC 4 so it's a Razor Helper Function that I populate via the View Model, Ex: `@Html.DropDownListFor(x => x.DollarValueTraded, Model.DollarValueTraded, "Dollar Value Traded...", new { @class = "DropDownLists"})` – Mark Sep 14 '13 at 00:47
  • you might consider another control such as select2. – Daniel A. White Sep 14 '13 at 00:49
  • @Mark if you deal with web, you should use `Table` layout, it's right for this purpose. – King King Sep 14 '13 at 00:49
  • @KingKing This isn't a CSS layout issue, but a string formatting issue, (unless I'm way out to lunch) where I want the values inside the Select box to be as indicated in the 2nd and 3rd example above. Going to test Jan's solution. – Mark Sep 14 '13 at 00:51
  • @Mark `table layout` is not part of `CSS`, it's `HTML` stuff. All the layout related to `alignment` should use `Table layout`. – King King Sep 14 '13 at 00:52

2 Answers2

1
String.Format("{0,5} {1}", money, username)

Or, HTML-encoded,

Replace(String.Format("{0,5} {1}", money, username), " ", " ")

So in MVC4 it would look "somewhat like" this:

@Html.Raw(
    Replace(
        String.Format(
            "{0,5} {1}",
            money,
            HttpServerUtility.HtmlEncode(username)
        ),
        " ",
        " "
    )
)

(make sure to set the font to something monospaced)

Jan Van Herck
  • 2,254
  • 17
  • 15
  • How would I generate such a format when I am building the string on the controller (server side). See the edit above. – Mark Sep 14 '13 at 01:13
  • Figured it out with a bit of help from a couple of other SO answers, thanks for your help :) – Mark Sep 14 '13 at 01:29
0

Why don't you use a tab with \t?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445