最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Forcing a new line for text in one <td> - Stack Overflow

programmeradmin4浏览0评论

I have been searching for about 20 minutes, with no luck on answering this question. I would very much appreciate anyone's input on this.

I have one cell (td) inside of a table row, and that cell will contain one line of text most of the time, but will occasionally contain two lines (the exact text that it contains depends on server-side processing). I don't mind so much if the text of one line is mushed together and wraps around, but I need to be able to start a new line if a second line is added. Hence my attempts to use the newline character below.

I can't really break it up into two <td> elements, and I can't seem to get \n or <br/> to work when I include them in the text server-side. If I do that, they are just rendered as plain text inside the cell.

Essentially, I want to be able to avoid analyzing the text with javascript client-side to add special characters. I am not even sure if that would work. Is there some css/html/javascript/jquery trick that might do the job?

The C# code that builds the text is simple enough, here's one example:

    caseDueDate = "Reading Response due: " + readingDueDate.ToString();

The Razor code for the field looks like this:

    <td>@Model.patients.ElementAt(ii).CaseDue</td>

The correctness of the results ing back is just fine. T'is only the formatting... =)

I have been searching for about 20 minutes, with no luck on answering this question. I would very much appreciate anyone's input on this.

I have one cell (td) inside of a table row, and that cell will contain one line of text most of the time, but will occasionally contain two lines (the exact text that it contains depends on server-side processing). I don't mind so much if the text of one line is mushed together and wraps around, but I need to be able to start a new line if a second line is added. Hence my attempts to use the newline character below.

I can't really break it up into two <td> elements, and I can't seem to get \n or <br/> to work when I include them in the text server-side. If I do that, they are just rendered as plain text inside the cell.

Essentially, I want to be able to avoid analyzing the text with javascript client-side to add special characters. I am not even sure if that would work. Is there some css/html/javascript/jquery trick that might do the job?

The C# code that builds the text is simple enough, here's one example:

    caseDueDate = "Reading Response due: " + readingDueDate.ToString();

The Razor code for the field looks like this:

    <td>@Model.patients.ElementAt(ii).CaseDue</td>

The correctness of the results ing back is just fine. T'is only the formatting... =)

Share Improve this question edited Jan 7, 2014 at 21:54 justian17 asked Jan 7, 2014 at 21:39 justian17justian17 5551 gold badge7 silver badges17 bronze badges 1
  • 6 Please post some code. – j08691 Commented Jan 7, 2014 at 21:43
Add a ment  | 

3 Answers 3

Reset to default 1

Sounds like something is converting your <br /> tags to htmlentities at some point in your code, as these should not render as plain text in html output.

Is your output wrapped in an htmlentities() call or something similar?

BR tags withing TDs should work... the server-side code is probably automatically escaping the html to prevent cross-site scripting. There are different ways to selectively disable this depending on the specific server software being used.

You're using ASP.NET MVC on the server, right?

Two options spring to mind, if you want to manually control the use of line breaks on the server side:

(1) You could write a display template (partial view) for CaseDue property. If you mark your model with the UIHint attribute, like so:

[UIHint("MyPartialView")]
public string CaseDue { ... }

... and then use an HtmlHelper in your view, e.g.:

@Html.DisplayFor(m => m.patients.ElementAt(index).CaseDue)

(2) You could write an extension method for the HtmlHelper class which creates and returns an instance of MvcHtmlString. Razor doesn't encode these, so you could include whatever tags you want in the return value. e.g., roughly:

public static MvcHtmlString TdForCaseDue(this HtmlHelper html, string[] lines) {
    TagBuilder td = new TagBuilder("td");
    td.InnerHtml = lines[0];
    for(int i = 1; i < lines.Length; i++) {
        td.InnerHtml += (new TagBuilder("br").ToString());
        td.InnerHtml += lines[i];
    }
    return new MvcHtmlString(td.ToString());
}
发布评论

评论列表(0)

  1. 暂无评论