浏览代码

Fix #10: add rowStyle option.

zhixin 11 年之前
父节点
当前提交
c43ebeaba4
共有 3 个文件被更改,包括 45 次插入13 次删除
  1. 6 0
      docs/docs.js
  2. 16 11
      docs/documentation.html
  3. 23 2
      src/bootstrap-table.js

+ 6 - 0
docs/docs.js

@@ -174,6 +174,12 @@ $(function () {
                     type: 'Boolean',
                     description: 'True to select checkbox or radiobox when click rows.',
                     'default': 'false'
+                },
+                {
+                    name: 'rowStyle',
+                    type: 'Function',
+                    description: 'The row formatter function, take two parameters: <br>row: the row record data.<br>index: the row index.<br>Support classes or css, code example:<br>{<br>classes: "red", <br>css: {background: "red", color: "white"}<br>}',
+                    'default': '{}'
                 }
             ]
         });

+ 16 - 11
docs/documentation.html

@@ -112,7 +112,8 @@
                 </div>
                 <div>
 <pre><code>&lt;script&gt;
-    $.fn.bootstrapTable.defaults = {
+    $.extend($.fn.bootstrapTable.defaults, {
+        bootstrapVersion: 3,
         classes: 'table table-hover',
         height: undefined,
         undefinedText: '-',
@@ -123,7 +124,8 @@
         data: [],
         method: 'get',
         url: undefined,
-        queryParams: function(pageSize, pageNumber, searchText) {return {};},
+        contentType: 'application/json',
+        queryParams: function(params) {return {};}, // pageSize, pageNumber, searchText
         pagination: false,
         sidePagination: 'client', // client or server
         totalRows: 0, // server side need to set
@@ -138,20 +140,22 @@
         cardView: false,
         clickToSelect: false,
 
+        rowStyle: function(row, index) {return {};},
+
         formatLoadingMessage: function() {
-            return 'Loading, please wait…';
+        return 'Loading, please wait…';
         },
         formatRecordsPerPage: function(pageNumber) {
-            return sprintf('%s records per page', pageNumber);
+        return sprintf('%s records per page', pageNumber);
         },
         formatShowingRows: function(pageFrom, pageTo, totalRows) {
-            return sprintf('Showing %s to %s of %s rows', pageFrom, pageTo, totalRows);
+        return sprintf('Showing %s to %s of %s rows', pageFrom, pageTo, totalRows);
         },
         formatSearch: function() {
-            return 'Search';
+        return 'Search';
         },
         formatNoMatches: function() {
-            return 'No matching records found';
+        return 'No matching records found';
         },
 
         onClickRow: function(item) {return false;},
@@ -161,10 +165,11 @@
         onCheckAll: function() {return false;},
         onUncheckAll: function() {return false;},
         onLoadSuccess: function(data) {return false;},
-        onLoadError: function(status) {return false;}
-    };
+        onLoadError: function(status) {return false;},
+        onBeforeLoad: function(res) {return res;}
+    });
 
-    $.fn.bootstrapTable.columnDefaults = {
+    $.extend($.fn.bootstrapTable.columnDefaults, {
         radio: false,
         checkbox: false,
         field: undefined,
@@ -177,7 +182,7 @@
         visible: true,
         formatter: undefined,
         sorter: undefined
-    };
+    });
 &lt;/script&gt;
 </code></pre>
                 </div>

+ 23 - 2
src/bootstrap-table.js

@@ -83,6 +83,8 @@
         cardView: false,
         clickToSelect: false,
 
+        rowStyle: function(row, index) {return {};},
+
         formatLoadingMessage: function() {
             return 'Loading, please wait…';
         },
@@ -570,9 +572,28 @@
         }
 
         for (var i = this.pageFrom - 1; i < this.pageTo; i++) {
-            var item = data[i];
+            var item = data[i],
+                style = {},
+                csses = [];
+
+            if (typeof this.options.rowStyle === 'function') {
+                style = this.options.rowStyle(item, i);
+            } else if (typeof this.options.rowStyle === 'string') {
+                style = eval(this.options.rowStyle + '(item, i)');
+            }
+
+            if (style.css) {
+                for (var key in style.css) {
+                    csses.push(key + ': ' + style.css[key]);
+                }
+            }
 
-            html.push('<tr' + ' data-index="' + i + '">');
+            html.push('<tr',
+                sprintf(' class="%s"', style.classes),
+                sprintf(' style="%s"', csses.join('; ')),
+                sprintf(' data-index="%s"', i),
+                '>'
+            );
 
             if (this.options.cardView) {
                 html.push(sprintf('<td colspan="%s">', this.header.fields.length));