浏览代码

Merge pull request #232 from ice5050/feature/AddRowAttribute

Feature/add row attributes
文翼 11 年之前
父节点
当前提交
d76cded120
共有 2 个文件被更改,包括 23 次插入1 次删除
  1. 9 0
      docs/docs.js
  2. 14 1
      src/bootstrap-table.js

+ 9 - 0
docs/docs.js

@@ -446,6 +446,15 @@ $(function () {
                     description_zh: '行样式格式化方法,有两个参数:<br>row: 行记录的数据。<br>index: 行数据的 index。<br>支持 classes 或者 css.',
                     'default': '{}',
                     example: 'classes-table'
+                },
+                {
+                    name: 'rowAttributes',
+                    attribute: 'data-row-attributes',
+                    type: 'Function',
+                    description: 'The row attribute formatter function, take two parameters: <br>row: the row record data.<br>index: the row index.<br>Support all custom attributes',
+                    description_zh: '该行的属性格式化功能,需要两个参数:<br>行:行记录数据。<br>指数:该行的索引。<br>支持所有的自定义属性。',
+                    'default': '{}',
+                    example: ''
                 }
             ]
         });

+ 14 - 1
src/bootstrap-table.js

@@ -156,6 +156,8 @@
 
         rowStyle: function (row, index) {return {};},
 
+        rowAttributes: function (row, index) {return {};},
+
         formatLoadingMessage: function () {
             return 'Loading, please wait…';
         },
@@ -874,7 +876,9 @@
         for (var i = this.pageFrom - 1; i < this.pageTo; i++) {
             var item = data[i],
                 style = {},
-                csses = [];
+                csses = [],
+                attributes = {},
+                htmlAttributes = [];
 
             style = calculateObjectValue(this.options, this.options.rowStyle, [item, i], style);
 
@@ -884,7 +888,16 @@
                 }
             }
 
+            attributes = calculateObjectValue(this.options, this.options.rowAttributes, [item, i], attributes);
+
+            if (attributes) {
+                for (var key in attributes) {
+                    htmlAttributes.push(key + '="' + attributes[key] + '"')
+                }
+            }
+
             html.push('<tr',
+                sprintf(' %s', htmlAttributes),
                 sprintf(' id="%s"', item._id),
                 sprintf(' class="%s"', style.classes || item._class),
                 sprintf(' data-index="%s"', i),