How to render html in select2 options

后端 未结 5 967
傲寒
傲寒 2020-12-25 10:46

In this example of data loaded from a remote source I can see images and other html elements rendered as options. I\'d like to accomplish the same thing using data in a loc

5条回答
  •  眼角桃花
    2020-12-25 10:54

    Simply you can add another field with the html to your data array and make your own template using the templateResult option like so

    JSFiddle Demo

    var data = [{
       id: 0,
       text: 'enhancement',
       html: '
    enhancement
    ' }, { id: 1, text: 'bug', html: '
    bug
    This is some small text on a new line
    ' }]; function template(data) { return data.html; } $("select").select2({ data: data, templateResult: template, escapeMarkup: function(m) { return m; } });

提交回复
热议问题