浏览代码

Merge pull request #223 from djhvscf/master

Added new extension bootstrap-table-flatJSON
文翼 11 年之前
父节点
当前提交
45f7d2f400
共有 3 个文件被更改,包括 177 次插入0 次删除
  1. 19 0
      docs/data5.json
  2. 65 0
      docs/extensions.html
  3. 93 0
      src/extensions/flatJSON/bootstrap-table-flatJSON.js

+ 19 - 0
docs/data5.json

@@ -0,0 +1,19 @@
+[
+	{
+		"id": 
+		{
+			"id2":'1'
+		},
+		"name": 
+		{
+			"name2": 'Item 1'
+		},
+		"price": 
+		{
+			"price2":
+			{
+				"price3":'$90'
+			}
+	   }
+   }
+]

+ 65 - 0
docs/extensions.html

@@ -255,6 +255,70 @@
                 </div>
 
                 <div class="highlight"><pre><code class="language-html"></code></pre></div>
+				
+				<div class="page-header">
+                    <h1 id="filter">Flat JSON object</h1>
+                </div>
+
+                <p data-es="Use el plugin: <a href='https://github.com/djhvscf/bootstrap-table-flatJSON' target='_blank'>bootstrap-table-flatJSON</a>">
+                    Use Plugin: <a href="https://github.com/djhvscf/bootstrap-table-flatJSON" target="_blank">bootstrap-table-flatJSON</a>
+                </p>
+
+                <div class="page-header">
+                    <h2 id="filter-usage" data-es="Uso">Usage</h2>
+                </div>
+
+                <div class="highlight highlight-html"><pre><span class="nt">&lt;script </span><span class="na">src=</span><span class="s">"extensions/filter/bootstrap-table-flatJSON"</span><span class="nt">&gt;&lt;/script&gt;</span>
+</pre></div>
+
+                <div class="page-header">
+                    <h2 id="filter-options" data-es="Opciones">Options</h2>
+                </div>
+
+                <table data-toggle="table" data-show-toggle="true" class="table">
+                    <thead>
+                    <tr>
+                        <th>Name</th>
+                        <th>Attribute</th>
+                        <th>Type</th>
+                        <th>Description</th>
+                        <th>Default</th>
+                    </tr>
+                    </thead>
+                    <tbody>
+                    <tr>
+                        <td>flat</td>
+                        <td>data-flat</td>
+                        <td>Boolean</td>
+                        <td>set <code>true</code> to obtains a flat JSON object.</td>
+                        <td><code>false</code></td>
+                    </tr>
+                    </tbody>
+                </table>
+
+                <div class="page-header">
+                    <h2 id="filter-demos" data-zh="示例" data-es="Ejemplos">Examples</h2>
+                </div>
+
+                <div class="bs-example">
+                    <table id="tblflat"
+                           data-toggle="table"
+                           data-height="299"
+                           data-url="data5.json"
+                           data-show-toggle="true"
+                           data-show-columns="true"
+                           data-flat="true">
+                        <thead>
+                        <tr>
+                            <th data-field="id.id2" data-align="right" data-sortable="true">Item ID</th>
+                            <th data-field="name.name2" data-align="center" data-sortable="true">Item Name</th>
+                            <th data-field="price.price2.price3" data-align="" data-sortable="true">Item Price</th>
+                        </tr>
+                        </thead>
+                    </table>
+                </div>
+
+                <div class="highlight"><pre><code class="language-html"></code></pre></div>
             </div>
         </div>
     </div>
@@ -305,6 +369,7 @@
 <!-- extensions -->
 <script src="../src/extensions/export/bootstrap-table-export.js"></script>
 <script src="../src/extensions/filter/bootstrap-table-filter.js"></script>
+<script src="../src/extensions/flatJSON/bootstrap-table-flatJSON.js"></script>
 <!-- others -->
 <script src="common.js"></script>
 <script src="//wenzhixin.net.cn/js/analytics.js"></script>

+ 93 - 0
src/extensions/flatJSON/bootstrap-table-flatJSON.js

@@ -0,0 +1,93 @@
+/ /**
+ * bootstrap-table-flatJSON.js
+ * @version: v1.0.0
+ * @author: Dennis Hernández
+ * @webSite: http://djhvscf.github.io/Blog
+ *
+ * Created by Dennis Hernández on 01/Nov/2014.
+ *
+ * Copyright (c) 2014 Dennis Hernández http://djhvscf.github.io/Blog
+ *
+ * The MIT License (http://www.opensource.org/licenses/mit-license.php)
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+ 
+(function ($) {
+    'use strict';
+
+    $.extend($.fn.bootstrapTable.defaults, {
+        flat: false
+    });
+
+    var BootstrapTable = $.fn.bootstrapTable.Constructor,
+        _initData = BootstrapTable.prototype.initData;
+
+    BootstrapTable.prototype.initData = function () {
+	
+        _initData.apply(this, Array.prototype.slice.apply(arguments));
+		var that = this;
+		
+		//If the flat is true
+		if(that.options.flat) {
+			that.options.data = sd.flatHelper(that.options.data);
+		}
+    };
+	
+	//Main functions
+	var sd = {
+			flat: function(element) {
+				var result = {};
+				function recurse (cur, prop) {
+					if (Object(cur) !== cur) {
+						result[prop] = cur;
+					} else if (Array.isArray(cur)) {
+						for(var i = 0, l = cur.length; i < l; i++) {
+							recurse(cur[i], prop ? prop+"."+i : ""+i);
+							if (l == 0) {
+								result[prop] = [];
+							}
+						}
+					} else {
+						var isEmpty = true;
+						for (var p in cur) {
+							isEmpty = false;
+							recurse(cur[p], prop ? prop+"."+p : p);
+						}
+						if (isEmpty) {
+							result[prop] = {};
+						}
+					}
+				}
+				recurse(element, "");
+				return result;
+			},
+			
+			flatHelper: function (data) {
+				var flatArray = [];
+				$.each(data, function(i, element) {
+					flatArray.push(sd.flat(element));
+				});
+				return flatArray;
+			}
+		};
+})(jQuery);