display values in XTemplate like textarea format

﹥>﹥吖頭↗ 提交于 2019-12-12 13:16:28

问题


I am using the plugin rowexpander (grid) with the following template.

rowBodyTpl: new Ext.XTemplate('<div>'+
    '<div>'+
        '<b>Vegetables:</b>'+
        '<ul><li>{vegetables_types}</li></ul>'+
    '</div>'+
'</div>')

Displays vegetables types.

Items are sent to the server with a textarea field with each value separated by paragraph, creating a sort of list.

potatos
carrots
pumpkins

If I edit these values with a textarea, they are displayed in the textarea with the same format they were sent to the server (like a list).

However, with the XTemplate I just can display them on a single line.

potatos carrots pumpkins

I would appreciate suggestions to solve

EDIT:01-02-2016

FIDDLE: https://fiddle.sencha.com/#fiddle/14se

FIDDLE (solved): https://fiddle.sencha.com/#fiddle/14sf


回答1:


The XTemplate creates HTML, while the text area uses formatted plain text.

HTML does show line breaks from the code as a single space only. HTML line breaks are made using <br> tag.

So you have to replace \n with <br> in your template for the line breaks to show up:

    var rowBodyTpl = new Ext.XTemplate([
      '<div>',
      '<div style = "white-space: pre-wrap;">',
      '<b>Vegetables:{[this.replaceBR(values.vegetables_types)]}</b>',
      '</div>',
      '</div>',
        {
            replaceBR:function(text) {
                return text.replace(/\n/g,'<br>');
            } 
        }
    ]);



回答2:


I see two ways:

  1. You can use tag <pre>. This tag shows preformatted text as is, with all spaces and new lines. (more see here: http://www.w3schools.com/tags/tag_pre.asp ). Here sample: https://fiddle.sencha.com/#fiddle/14il

  2. You can make string array from vegetables_types string. It's best way I think. After it, you could use <tpl for="vegetables_type"> statement (look here: http://docs.sencha.com/extjs/5.1/5.1.1-apidocs/#!/api/Ext.XTemplate ). Here sample: https://fiddle.sencha.com/#fiddle/14sa



来源:https://stackoverflow.com/questions/35119906/display-values-in-xtemplate-like-textarea-format

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