在selectInput下拉菜单中格式化单个选项的文本

时间:2018-10-29 21:55:49

标签: html css r shiny

我想格式化在selectInput()下拉菜单中显示的各个选项的文本。我有一个带有html属性的文本字符串列表:

myChoices_list <- c("<b>choice 1</b>", "<b>choice 2</b>", "<i>choice 3 </i>", "<i>choice 4</i>", "<<p style=\"text-indent: 20px\">choice 5</p>")

html属性要求将粗体,斜体和缩进应用于每个字符串。我尝试通过“选择”选项中的HTML()函数应用属性,但是没有运气。

ui <- fluidPage(
sidebarPanel(
  selectInput(inputID = "myChoice", "Choice:"
             , choices = HTML(myChoices_list))
  )
) 

通过在服务器段的输出中将“转义”选项设置为FALSE,虽然这些格式在mainPanel中可以正常工作,但该选项在selectInput()的下拉菜单中似乎不可用。

我认为解决方案可能与tags $ style有关,但是我对闪亮的结构和文本格式的指定是陌生的。它与How to style an single individual selectInput menu in R Shiny?的不同之处还在于html格式已经是列表的一部分。实际的列表也很大。

1 个答案:

答案 0 :(得分:0)

为什么不使用jQuery?

$('#DropdownSelectID').change(function () {
  var selectedVal = $('#DropdownSelectID :selected').val();

  if (selectedVal == '1') {
     $("#DropdownSelectID").css('cssText', 'font-weight: bold; color: blue');
  }
  else if (selectedVal == '2') {
     $("#DropdownSelectID").css('cssText', 'color: red');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

<select id='DropdownSelectID'>
  <option value='0'>-- Select --</option>
  <option value='1'>Hi</option>
  <option value='2'>Hello</option>
</select>