【问题标题】:color code bootstrap buttons customization in javascript array listjavascript数组列表中的颜色代码引导按钮自定义
【发布时间】:2021-03-19 07:47:13
【问题描述】:

我想使用引导程序 btn-success(绿色)和 btn-danger(红色)为特定的按钮阵列自定义按钮。 在这种情况下,我希望 excel 为绿色,pdf 为红色。

我生成按钮的代码如下:

    var buttons = new $.fn.dataTable.Buttons(datatable, {
        buttons: [
            { 
                extend:'excelHtml5',
                footer: true,
                title: exportTitle(),
                messageTop: exportInfo()                    
            },
            { 
                extend:'pdf',
                footer: true ,
                title: exportTitle(),
                messageTop: exportInfo()
            }
        ]
    }).container().appendTo($("#divbuttons"))
    $("#divbuttons").find('button').removeClass()
    $("#divbuttons").find('button').addClass("btn btn-primary report_date") <-- here is where I set the css but it applies to both the buttons. How can I specifically say excel is btn-success and pdf is btn-danger

目前它看起来像上面的代码。

这是在浏览器检查中生成的 html 代码

如何专门为 excel 按钮的颜色编码是 btn-success 而 pdf 是 btn-danger ?

【问题讨论】:

    标签: javascript css button twitter-bootstrap-3 colors


    【解决方案1】:

    您可以轻松地遍历按钮数组。

    创建一个如下所示的数组:

    let buttons = [
     {
      name: "Find"
      class: "btn-primary"
     },
     {
      name: "Excel",
      class: "btn-success"
     },
     {
      name: "PDF"
      class: "btn-danger"
     }
    ]
    
    for(const button in buttons) {
     $("#divbuttons").find('button').addClass(`btn ${button.class} report_date`)
    }
    
    

    这将遍历数组并将属性button.class 添加到类名中。

    【讨论】:

    • 它不起作用。我把它放在外观 -> console.log(button.class);它说未定义
    • @Axil 在每个不同的按钮上创建一个 id,然后将该 id 作为属性添加到数组中的每个对象。然后你可以做类似 $(#${buttton.id}).addClass(btn ${button.class} report_date)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 2018-07-05
    • 1970-01-01
    相关资源
    最近更新 更多