浏览代码

Added headerStyle option

zhixin 6 年之前
父节点
当前提交
f72734c2ee
共有 3 个文件被更改,包括 45 次插入4 次删除
  1. 27 0
      site/docs/api/table-options.md
  2. 16 2
      src/bootstrap-table.js
  3. 2 2
      src/constants/index.js

+ 27 - 0
site/docs/api/table-options.md

@@ -69,6 +69,33 @@ The table options are defined in `jQuery.fn.bootstrapTable.defaults`.
 
 - **Example:** [Thead Classes](https://examples.bootstrap-table.com/#options/thead-classes.html)
 
+## headerStyle
+
+- **Attribute:** `data-header-style`
+
+- **Type:** `Function`
+
+- **Detail:**
+
+  The header style formatter function, takes one parameter:
+
+  * `column`: the column object.
+
+  Support `classes` or `css`. Example usage:
+
+  {% highlight javascript %}
+  function headerStyle(column) {
+    return {
+      css: { 'font-weight': 'normal' },
+      classes: 'my-class'
+    }
+  }
+  {% endhighlight %}
+
+- **Default:** `{}`
+
+- **Example:** [Header Style](https://examples.bootstrap-table.com/#options/header-style.html)
+
 ## rowStyle
 
 - **Attribute:** `data-row-style`

+ 16 - 2
src/bootstrap-table.js

@@ -252,6 +252,20 @@ class BootstrapTable {
           return
         }
 
+        const headerStyle = Utils.calculateObjectValue(null, this.options.headerStyle, [column])
+        const csses = []
+        let classes = ''
+
+        if (headerStyle && headerStyle.css) {
+          for (const [key, value] of Object.entries(headerStyle.css)) {
+            csses.push(`${key}: ${value}`)
+          }
+        }
+        if (headerStyle && headerStyle.classes) {
+          classes = Utils.sprintf(' class="%s"', column['class'] ?
+            [column['class'], headerStyle.classes].join(' ') : headerStyle.classes)
+        }
+
         if (typeof column.fieldIndex !== 'undefined') {
           this.header.fields[column.fieldIndex] = column.field
           this.header.styles[column.fieldIndex] = align + style
@@ -278,8 +292,8 @@ class BootstrapTable {
         html.push(`<th${Utils.sprintf(' title="%s"', column.titleTooltip)}`,
           column.checkbox || column.radio
             ? Utils.sprintf(' class="bs-checkbox %s"', column['class'] || '')
-            : class_,
-          Utils.sprintf(' style="%s"', halign + style),
+            : classes || class_,
+          Utils.sprintf(' style="%s"', halign + style + csses.join('; ')),
           Utils.sprintf(' rowspan="%s"', column.rowspan),
           Utils.sprintf(' colspan="%s"', column.colspan),
           Utils.sprintf(' data-field="%s"', column.field),

+ 2 - 2
src/constants/index.js

@@ -112,7 +112,7 @@ const DEFAULTS = {
   height: undefined,
   classes: 'table table-bordered table-hover',
   theadClasses: '',
-  headerStyle (row, index) {
+  headerStyle (column) {
     return {}
   },
   rowStyle (row, index) {
@@ -188,7 +188,7 @@ const DEFAULTS = {
   customSearch: undefined,
   showHeader: true,
   showFooter: false,
-  footerStyle (row, index) {
+  footerStyle (column) {
     return {}
   },
   showColumns: false,