<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12
        

      JS組件系列——自己動手擴展BootstrapTable的treegrid功能

      前言:上篇  JS組件系列——自己動手封裝bootstrap-treegrid組件 博主自己動手封裝了下treegrid的功能,但畢竟那個組件只是一個單獨針對樹形表格做的,適用性還比較有限。關(guān)注博主的園友應(yīng)該知道,博主的博客里面寫了很多bootstrapTable的擴展,今天打算在直接在bootstrapTable的基礎(chǔ)上擴展一個treegrid的功能,很多長期關(guān)注博主博客的園友一直在問我怎么在bootstrapTable里面直接使用treegrid的功能,所以今天還是帶來點福利。有興趣的可以捧個人場!

      本文原創(chuàng)地址:http://www.rzrgm.cn/landeanfen/p/6924895.html

      一、效果預(yù)覽

      全部折疊

      展開一級

      全部展開

      二、代碼示例

      怎么樣?效果還行吧。給出js的源碼供大家參考。

      (function ($) {
          'use strict';
      
          var sprintf = function (str) {
              var args = arguments,
                  flag = true,
                  i = 1;
      
              str = str.replace(/%s/g, function () {
                  var arg = args[i++];
      
                  if (typeof arg === 'undefined') {
                      flag = false;
                      return '';
                  }
                  return arg;
              });
              return flag ? str : '';
          };
      
          var getFieldIndex = function (columns, field) {
              var index = -1;
      
              $.each(columns, function (i, column) {
                  if (column.field === field) {
                      index = i;
                      return false;
                  }
                  return true;
              });
              return index;
          };
      
          var calculateObjectValue = function (self, name, args, defaultValue) {
              var func = name;
      
              if (typeof name === 'string') {
                  var names = name.split('.');
      
                  if (names.length > 1) {
                      func = window;
                      $.each(names, function (i, f) {
                          func = func[f];
                      });
                  } else {
                      func = window[name];
                  }
              }
              if (typeof func === 'object') {
                  return func;
              }
              if (typeof func === 'function') {
                  return func.apply(self, args);
              }
              if (!func && typeof name === 'string' && sprintf.apply(this, [name].concat(args))) {
                  return sprintf.apply(this, [name].concat(args));
              }
              return defaultValue;
          };
      
          var getItemField = function (item, field) {
              var value = item;
      
              if (typeof field !== 'string' || item.hasOwnProperty(field)) {
                  return item[field];
              }
              var props = field.split('.');
              for (var p in props) {
                  value = value[props[p]];
              }
              return value;
          };
      
          var getParent = function (node, source, field) {
              var data = [];
              var items = $.grep(source, function (item, index) {
                  return node.ParentId == item[field];
              });
              $.each(items, function (index, item) {
                  data.splice(0, 0, item);
                  var child = getParent(item, source, field);
                  $.each(child, function (i, n) {
                      data.splice(0, 0, n);
                  });
              });
              return data;
          };
      
          var getChild = function (node, source, field) {
              var data = [];
              var items = $.grep(source, function (item, index) {
                  return item.ParentId == node[field];
              });
      
              $.each(items, function (index, item) {
                  data.push(item);
                  var child = getChild(item, source, field);
                  $.each(child, function (i, n) {
                      data.push(n);
                  });
              });
              return data;
          };
      
          //調(diào)用bootstrapTable組件的構(gòu)造器得到對象
          var BootstrapTable = $.fn.bootstrapTable.Constructor,
              _initData = BootstrapTable.prototype.initData,
              _initPagination = BootstrapTable.prototype.initPagination,
              _initBody = BootstrapTable.prototype.initBody;
      
          //重寫bootstrapTable的initData方法
          BootstrapTable.prototype.initData = function () {
              _initData.apply(this, Array.prototype.slice.apply(arguments));
              var that = this;
              if (that.options.treeView && this.data.length > 0) {
                  var rows = [];
                  var roots = $.grep(this.data, function (row, index) {
                      return row.Level == that.options.treeRootLevel;
                  });
                  $.each(roots, function (index, item) {
                      rows.push(item);
                      var child = getChild(item, that.data, that.options.treeId);
                      $.each(child, function (i, n) {
                          if (that.options.treeCollapseAll) {
                              n.hidden = true;
                          }
                          rows.push(n);
                      });
                  });
                  that.options.data = that.data = rows;
              }
          };
      
          //重寫bootstrapTable的initPagination方法
          BootstrapTable.prototype.initPagination = function () {
              //理論情況下,treegrid是不支持分頁的,所以默認分頁參數(shù)為false
              this.options.pagination = false;
               //調(diào)用“父類”的“虛方法”
              _initPagination.apply(this, Array.prototype.slice.apply(arguments));
          };
      
          //重寫bootstrapTable的initBody方法
          BootstrapTable.prototype.initBody = function (fixedScroll) {
              var that = this,
                  html = [],
                  data = this.getData();
      
              this.trigger('pre-body', data);
      
              this.$body = this.$el.find('tbody');
              if (!this.$body.length) {
                  this.$body = $('<tbody></tbody>').appendTo(this.$el);
              }
      
              if (!this.options.pagination || this.options.sidePagination === 'server') {
                  this.pageFrom = 1;
                  this.pageTo = data.length;
              }
      
              for (var i = this.pageFrom - 1; i < this.pageTo; i++) {
                  var key,
                      item = data[i],
                      style = {},
                      csses = [],
                      data_ = '',
                      attributes = {},
                      htmlAttributes = [];
                  if (item.hidden) continue;
      
                  style = calculateObjectValue(this.options, this.options.rowStyle, [item, i], style);
      
                  if (style && style.css) {
                      for (key in style.css) {
                          csses.push(key + ': ' + style.css[key]);
                      }
                  }
      
                  attributes = calculateObjectValue(this.options,
                      this.options.rowAttributes, [item, i], attributes);
      
                  if (attributes) {
                      for (key in attributes) {
                          htmlAttributes.push(sprintf('%s="%s"', key, escapeHTML(attributes[key])));
                      }
                  }
      
                  if (item._data && !$.isEmptyObject(item._data)) {
                      $.each(item._data, function (k, v) {
                          if (k === 'index') {
                              return;
                          }
                          data_ += sprintf(' data-%s="%s"', k, v);
                      });
                  }
      
                  html.push('<tr',
                      sprintf(' %s', htmlAttributes.join(' ')),
                      sprintf(' id="%s"', $.isArray(item) ? undefined : item._id),
                      sprintf(' class="%s"', style.classes || ($.isArray(item) ? undefined : item._class)),
                      sprintf(' data-index="%s"', i),
                      sprintf(' data-uniqueid="%s"', item[this.options.uniqueId]),
                      sprintf('%s', data_),
                      '>'
                  );
      
                  if (this.options.cardView) {
                      html.push(sprintf('<td colspan="%s">', this.header.fields.length));
                  }
      
                  if (!this.options.cardView && this.options.detailView) {
                      html.push('<td>',
                          '<a class="detail-icon" href="javascript:">',
                          sprintf('<i class="%s %s"></i>', this.options.iconsPrefix, this.options.icons.detailOpen),
                          '</a>',
                          '</td>');
                  }
      
                  $.each(this.header.fields, function (j, field) {
                      var text = '',
                          value = getItemField(item, field),
                          type = '',
                          cellStyle = {},
                          id_ = '',
                          class_ = that.header.classes[j],
                          data_ = '',
                          rowspan_ = '',
                          title_ = '',
                          column = that.columns[getFieldIndex(that.columns, field)];
      
                      if (!column.visible) {
                          return;
                      }
      
                      style = sprintf('style="%s"', csses.concat(that.header.styles[j]).join('; '));
      
                      value = calculateObjectValue(column,
                          that.header.formatters[j], [value, item, i], value);
      
                      if (item['_' + field + '_id']) {
                          id_ = sprintf(' id="%s"', item['_' + field + '_id']);
                      }
                      if (item['_' + field + '_class']) {
                          class_ = sprintf(' class="%s"', item['_' + field + '_class']);
                      }
                      if (item['_' + field + '_rowspan']) {
                          rowspan_ = sprintf(' rowspan="%s"', item['_' + field + '_rowspan']);
                      }
                      if (item['_' + field + '_title']) {
                          title_ = sprintf(' title="%s"', item['_' + field + '_title']);
                      }
                      cellStyle = calculateObjectValue(that.header,
                          that.header.cellStyles[j], [value, item, i], cellStyle);
                      if (cellStyle.classes) {
                          class_ = sprintf(' class="%s"', cellStyle.classes);
                      }
                      if (cellStyle.css) {
                          var csses_ = [];
                          for (var key in cellStyle.css) {
                              csses_.push(key + ': ' + cellStyle.css[key]);
                          }
                          style = sprintf('style="%s"', csses_.concat(that.header.styles[j]).join('; '));
                      }
      
                      if (item['_' + field + '_data'] && !$.isEmptyObject(item['_' + field + '_data'])) {
                          $.each(item['_' + field + '_data'], function (k, v) {
                              if (k === 'index') {
                                  return;
                              }
                              data_ += sprintf(' data-%s="%s"', k, v);
                          });
                      }
      
                      if (column.checkbox || column.radio) {
                          type = column.checkbox ? 'checkbox' : type;
                          type = column.radio ? 'radio' : type;
      
                          text = [that.options.cardView ?
                              '<div class="card-view">' : '<td class="bs-checkbox">',
                              '<input' +
                              sprintf(' data-index="%s"', i) +
                              sprintf(' name="%s"', that.options.selectItemName) +
                              sprintf(' type="%s"', type) +
                              sprintf(' value="%s"', item[that.options.idField]) +
                              sprintf(' checked="%s"', value === true ||
                              (value && value.checked) ? 'checked' : undefined) +
                              sprintf(' disabled="%s"', !column.checkboxEnabled ||
                              (value && value.disabled) ? 'disabled' : undefined) +
                              ' />',
                              that.header.formatters[j] && typeof value === 'string' ? value : '',
                              that.options.cardView ? '</div>' : '</td>'
                          ].join('');
      
                          item[that.header.stateField] = value === true || (value && value.checked);
                      } else {
                          
                          value = typeof value === 'undefined' || value === null ?
                              that.options.undefinedText : value;
                          var indent, icon;
                          if (that.options.treeView && column.field == that.options.treeField) {
                              var indent = item.Level == that.options.Level ? '' : sprintf('<span style="margin-left: %spx;"></span>', (item.Level - that.options.treeRootLevel) * 15);
                              var child = $.grep(data, function (d, i) {
                                  return d.ParentId == item[that.options.treeId] && !d.hidden;
                              });
                              icon = sprintf('<span class="tree-icon %s" style="cursor: pointer; margin: 0px 5px;"></span>', child.length > 0 ? that.options.expandIcon : that.options.collapseIcon);
                              //icon = sprintf('<span class="tree-icon %s" style="cursor: pointer; margin: 0px 5px;"></span>', child.length > 0 ? that.options.expandIcon : "");
                          }
                          text = that.options.cardView ? ['<div class="card-view">',
                              that.options.showHeader ? sprintf('<span class="title" %s>%s</span>', style,
                                  getPropertyFromOther(that.columns, 'field', 'title', field)) : '',
                              sprintf('<span class="value">%s</span>', value),
                              '</div>'
                          ].join('') : [sprintf('<td%s %s %s %s %s %s>', id_, class_, style, data_, rowspan_, title_),
                              indent,
                              icon,
                              value,
                              '</td>'
                          ].join('');
      
                          if (that.options.cardView && that.options.smartDisplay && value === '') {
                              text = '';
                          }
                      }
      
                      html.push(text);
                  });
      
                  if (this.options.cardView) {
                      html.push('</td>');
                  }
      
                  html.push('</tr>');
              }
      
              if (!html.length) {
                  html.push('<tr class="no-records-found">',
                      sprintf('<td colspan="%s">%s</td>',
                          this.$header.find('th').length, this.options.formatNoMatches()),
                      '</tr>');
              }
      
              this.$body.html(html.join(''));
      
              if (!fixedScroll) {
                  this.scrollTo(0);
              }
      
              this.$body.find('> tr[data-index] > td').off('click dblclick').on('click dblclick', function (e) {
                  var $td = $(this),
                      $tr = $td.parent(),
                      item = that.data[$tr.data('index')],
                      index = $td[0].cellIndex,
                      field = that.header.fields[that.options.detailView && !that.options.cardView ? index - 1 : index],
                      column = that.columns[getFieldIndex(that.columns, field)],
                      value = getItemField(item, field);
      
                  if ($td.find('.detail-icon').length) {
                      return;
                  }
      
                  that.trigger(e.type === 'click' ? 'click-cell' : 'dbl-click-cell', field, value, item, $td);
                  that.trigger(e.type === 'click' ? 'click-row' : 'dbl-click-row', item, $tr);
      
                  if (e.type === 'click' && that.options.clickToSelect && column.clickToSelect) {
                      var $selectItem = $tr.find(sprintf('[name="%s"]', that.options.selectItemName));
                      if ($selectItem.length) {
                          $selectItem[0].click();
                      }
                  }
              });
      
              this.$body.find('> tr[data-index] > td > .detail-icon').off('click').on('click', function () {
                  debugger;
                  var $this = $(this),
                      $tr = $this.parent().parent(),
                      index = $tr.data('index'),
                      row = data[index]; 
      
                  if ($tr.next().is('tr.detail-view')) {
                      $this.find('i').attr('class', sprintf('%s %s', that.options.iconsPrefix, that.options.icons.detailOpen));
                      $tr.next().remove();
                      that.trigger('collapse-row', index, row);
                  } else {
                      $this.find('i').attr('class', sprintf('%s %s', that.options.iconsPrefix, that.options.icons.detailClose));
                      $tr.after(sprintf('<tr class="detail-view"><td colspan="%s">%s</td></tr>',
                          $tr.find('td').length, calculateObjectValue(that.options,
                              that.options.detailFormatter, [index, row], '')));
                      that.trigger('expand-row', index, row, $tr.next().find('td'));
                  }
                  that.resetView();
              });
      
              this.$body.find('> tr[data-index] > td > .tree-icon').off('click').on('click', function (e) {
                  debugger;
                  e.stopPropagation();
                  var $this = $(this),
                      $tr = $this.parent().parent(),
                      index = $tr.data('index'),
                      row = data[index];
                  var icon = $(this);
                  var child = getChild(data[index], data, that.options.treeId);
                  $.each(child, function (i, c) {
                      $.each(that.data, function (index, item) {
                          if (item[that.options.treeId] == c[that.options.treeId]) {
                              item.hidden = icon.hasClass(that.options.expandIcon);
                              that.uncheck(index);
                              return;
                          }
                      });
                  });
                  if (icon.hasClass(that.options.expandIcon)) {
                      icon.removeClass(that.options.expandIcon).addClass(that.options.collapseIcon);
                  } else {
                      icon.removeClass(that.options.collapseIcon).addClass(that.options.expandIcon);
                  }
                  that.options.data = that.data;
                  that.initBody(true);
              });
      
              this.$selectItem = this.$body.find(sprintf('[name="%s"]', this.options.selectItemName));
              this.$selectItem.off('click').on('click', function (event) {
                  event.stopImmediatePropagation();
      
                  var $this = $(this),
                      checked = $this.prop('checked'),
                      row = that.data[$this.data('index')];
      
                  if (that.options.maintainSelected && $(this).is(':radio')) {
                      $.each(that.options.data, function (i, row) {
                          row[that.header.stateField] = false;
                      });
                  }
      
                  row[that.header.stateField] = checked;
      
                  if (that.options.singleSelect) {
                      that.$selectItem.not(this).each(function () {
                          that.data[$(this).data('index')][that.header.stateField] = false;
                      });
                      that.$selectItem.filter(':checked').not(this).prop('checked', false);
                  }
      
                  that.updateSelected();
                  that.trigger(checked ? 'check' : 'uncheck', row, $this);
              });
      
              $.each(this.header.events, function (i, events) {
                  if (!events) {
                      return;
                  }
                  if (typeof events === 'string') {
                      events = calculateObjectValue(null, events);
                  }
      
                  var field = that.header.fields[i],
                      fieldIndex = $.inArray(field, that.getVisibleFields());
      
                  if (that.options.detailView && !that.options.cardView) {
                      fieldIndex += 1;
                  }
      
                  for (var key in events) {
                      that.$body.find('tr').each(function () {
                          var $tr = $(this),
                              $td = $tr.find(that.options.cardView ? '.card-view' : 'td').eq(fieldIndex),
                              index = key.indexOf(' '),
                              name = key.substring(0, index),
                              el = key.substring(index + 1),
                              func = events[key];
      
                          $td.find(el).off(name).on(name, function (e) {
                              var index = $tr.data('index'),
                                  row = that.data[index],
                                  value = row[field];
      
                              func.apply(this, [e, value, row, index]);
                          });
                      });
                  }
              });
      
              this.updateSelected();
              this.resetView();
      
              this.trigger('post-body');
          };
      
      
          //給組件增加默認參數(shù)列表
          $.extend($.fn.bootstrapTable.defaults, {
              treeView: false,//treeView視圖
              treeField: "id",//treeView視圖字段
              treeId: "id",
              treeRootLevel: 0,//根節(jié)點序號
              treeCollapseAll: false,//是否全部展開
              collapseIcon: "glyphicon glyphicon-chevron-right",//折疊樣式
              expandIcon: "glyphicon glyphicon-chevron-down"//展開樣式
          });
      })(jQuery);

      組件的使用如下:

      1、首先引用這個js文件。

      2、然后初始化組件

               $('#tb').bootstrapTable({
                          url: ActionUrl + 'GetMenuList',
                          toolbar: '#toolbar',
                          sidePagination: 'client',
                          pagination: false,
                          treeView: true,
                          treeId: "Id",
                          treeField: "Name",
                          treeRootLevel: 1,
                          clickToSelect: true,//collapseIcon: "glyphicon glyphicon-triangle-right",//折疊樣式
                          //expandIcon: "glyphicon glyphicon-triangle-bottom"http://展開樣式
                      });

      treeView:true表示啟用樹表格模式;

      treeId:'Id'表示每一行tree的id;

      treeField:'Name'表示要對那一列進行展開;

      treeRootLevel:1表示樹根的級別。

      還有一個地方需要注意,要建立記錄之間的父子級關(guān)系,必然后有一個ParentId的概念,所以在從后端返回的結(jié)果集里面,每條記錄勢必有一個ParentId的屬性,如果是根節(jié)點,ParentId為null。比如我們后臺得到的結(jié)果集的json格式如下:

      [{Id: 1, Name: "系統(tǒng)設(shè)置", Url: null, ParentId: null, Level: 1, CreateTime: null, Status: 1, SortOrder: 1,…},
      {Id: 2, Name: "菜單管理", Url: "/Systems/Menu/Index", ParentId: 1, Level: 2, CreateTime: null, Status: 1,…},
      {Id: 3, Name: "訂單管理", Url: null, ParentId: null, Level: 1, CreateTime: "2017-05-31 17:05:27",…},
      {Id: 4, Name: "基礎(chǔ)數(shù)據(jù)", Url: null, ParentId: null, Level: 1, CreateTime: "2017-05-31 17:05:55",…},
      {Id: 5, Name: "新增訂單", Url: "/order/add", ParentId: 3, Level: 2, CreateTime: "2017-05-31 17:07:03",…}]

      三、組件需要完善的地方

      上述封裝給大家提供一個擴展bootstrapTable組件treeview功能,還有很多地方需要完善,比如:

      1、我們的葉子節(jié)點前面的圖標可以去掉;

      2、增加展開所有、折疊所有的功能;

      3、Level字段可以去掉,通過ParentId為null來確定根節(jié)點。

      有興趣的園友可以自己試試。

      四、總結(jié)

      至此本文就結(jié)束了,這篇針對上篇做了一個補充,使得我們可以根據(jù)項目的需求自己選擇用哪種方式,如果我們項目使用的是bootstrapTable作為數(shù)據(jù)展示的組件,可以考慮上述擴展;如果沒有使用bootstrapTable,可以試試上篇的組件。如果你覺得本文能夠幫助你,可以右邊隨意 打賞 博主,打賞后可以獲得博主永久免費的技術(shù)支持。

      源碼下載

      本文原創(chuàng)出處:http://www.rzrgm.cn/landeanfen/

      歡迎各位轉(zhuǎn)載,但是未經(jīng)作者本人同意,轉(zhuǎn)載文章之后必須在文章頁面明顯位置給出作者和原文連接,否則保留追究法律責任的權(quán)利

      posted @ 2017-05-31 18:42  懶得安分  閱讀(29437)  評論(56)    收藏  舉報
      主站蜘蛛池模板: 国产精品综合一区二区三区| 欧美高清一区三区在线专区 | 中日韩黄色基地一二三区| 久久精品无码鲁网中文电影| 沾益县| 国产美女69视频免费观看| 忘忧草www日本韩国| 精品九九人人做人人爱| 精品国产AV无码一区二区三区| 欧美黑人性暴力猛交在线视频 | 国产精品成人久久电影| 国精品无码一区二区三区在线蜜臀 | 国产成人精彩在线视频| 精品国产熟女一区二区三区| 日韩在线视频一区二区三| 日本不卡一区二区三区在线| 国产目拍亚洲精品二区| 久久国产一区二区三区| 亚洲gay片在线gv网站| 高尔夫| 亚洲日本精品国产第一区| 狠狠躁夜夜躁无码中文字幕| 国产女同一区二区在线| 色悠久久网国产精品99| 久久天天躁狠狠躁夜夜躁2012 | 九九热在线视频精品免费| 98久久人妻少妇激情啪啪| 熟妇人妻无码中文字幕老熟妇| 国产片AV国语在线观看手机版| 亚洲av无码精品蜜桃| 国产午夜91福利一区二区| 国产乱子伦无套一区二区三区| 亚洲熟女精品一区二区| 色偷偷久久一区二区三区| 九九热免费公开视频在线| 熟女人妇 成熟妇女系列视频| 宝鸡市| 日韩免费无码视频一区二区三区| 欧洲精品码一区二区三区| 老司机精品成人无码av| 精品国产免费人成在线观看|