bootstrap-table-flatJSON.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * bootstrap-table-flatJSON.js
  3. * @version: v1.0.1
  4. * @author: Dennis Hernández
  5. * @webSite: http://djhvscf.github.io/Blog
  6. *
  7. * Created by Dennis Hernández on 01/Nov/2014.
  8. *
  9. * Copyright (c) 2014 Dennis Hernández http://djhvscf.github.io/Blog
  10. *
  11. * The MIT License (http://www.opensource.org/licenses/mit-license.php)
  12. *
  13. * Permission is hereby granted, free of charge, to any person
  14. * obtaining a copy of this software and associated documentation
  15. * files (the "Software"), to deal in the Software without
  16. * restriction, including without limitation the rights to use,
  17. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. * copies of the Software, and to permit persons to whom the
  19. * Software is furnished to do so, subject to the following
  20. * conditions:
  21. *
  22. * The above copyright notice and this permission notice shall be
  23. * included in all copies or substantial portions of the Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  27. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  29. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  30. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  31. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  32. * OTHER DEALINGS IN THE SOFTWARE.
  33. */
  34. (function ($) {
  35. 'use strict';
  36. $.extend($.fn.bootstrapTable.defaults, {
  37. flat: false
  38. });
  39. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  40. _initData = BootstrapTable.prototype.initData;
  41. BootstrapTable.prototype.initData = function () {
  42. _initData.apply(this, Array.prototype.slice.apply(arguments));
  43. //If the flat is true
  44. if (this.options.flat) {
  45. this.options.data = sd.flatHelper(this.options.data);
  46. }
  47. if (this.options.sidePagination === 'server') {
  48. this.data = this.options.data;
  49. }
  50. };
  51. //Main functions
  52. var sd = {
  53. flat: function (element) {
  54. var result = {};
  55. function recurse(cur, prop) {
  56. if (Object(cur) !== cur) {
  57. result[prop] = cur;
  58. } else if ($.isArray(cur)) {
  59. for (var i = 0, l = cur.length; i < l; i++) {
  60. recurse(cur[i], prop ? prop + "." + i : "" + i);
  61. if (l == 0) {
  62. result[prop] = [];
  63. }
  64. }
  65. } else {
  66. var isEmpty = true;
  67. for (var p in cur) {
  68. isEmpty = false;
  69. recurse(cur[p], prop ? prop + "." + p : p);
  70. }
  71. if (isEmpty) {
  72. result[prop] = {};
  73. }
  74. }
  75. }
  76. recurse(element, "");
  77. return result;
  78. },
  79. flatHelper: function (data) {
  80. var flatArray = [],
  81. arrayHelper = [];
  82. if (!$.isArray(data)) {
  83. arrayHelper.push(data);
  84. data = arrayHelper;
  85. }
  86. $.each(data, function (i, element) {
  87. flatArray.push(sd.flat(element));
  88. });
  89. return flatArray;
  90. }
  91. };
  92. })(jQuery);