forked from shuxuecode/bootstrap-table
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request wenzhixin#3525 from foreveryang321/develop
Add treegrid extension
- Loading branch information
Showing
4 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Table Treegrid | ||
|
||
Use Plugin: [bootstrap-table-treegrid](https://github.com/wenzhixin/bootstrap-table/tree/master/src/extensions/treegrid) | ||
|
||
## Usage | ||
|
||
```html | ||
<script src="extensions/treegrid/bootstrap-table-treegrid.js"></script> | ||
``` | ||
|
||
## Options | ||
|
||
### treeShowField | ||
|
||
* type: String | ||
* description: | ||
* default: `` | ||
|
||
### parentIdField | ||
|
||
* type: String | ||
* description: | ||
* default: `pid` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* @author: YL | ||
* @version: v1.0.0 | ||
*/ | ||
!function ($) { | ||
'use strict'; | ||
$.extend($.fn.bootstrapTable.defaults, { | ||
treeShowField: null, | ||
idField: 'id', | ||
parentIdField: 'pid', | ||
onGetNodes: function (row, data) { | ||
var that = this; | ||
var nodes = []; | ||
$.each(data, function (i, item) { | ||
if (row[that.options.idField] === item[that.options.parentIdField]) { | ||
nodes.push(item); | ||
} | ||
}); | ||
return nodes; | ||
}, | ||
onCheckRoot: function (row, data) { | ||
var that = this; | ||
return !row[that.options.parentIdField]; | ||
} | ||
}); | ||
|
||
var BootstrapTable = $.fn.bootstrapTable.Constructor, | ||
_initRow = BootstrapTable.prototype.initRow, | ||
_initHeader = BootstrapTable.prototype.initHeader; | ||
|
||
// td | ||
BootstrapTable.prototype.initHeader = function () { | ||
var that = this; | ||
_initHeader.apply(that, Array.prototype.slice.apply(arguments)); | ||
var treeShowField = that.options.treeShowField; | ||
if (treeShowField) { | ||
$.each(this.header.fields, function (i, field) { | ||
if (treeShowField === field) { | ||
that.treeEnable = true; | ||
return false; | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
var initTr = function (item, idx, data, parentDom) { | ||
var that = this; | ||
var nodes = that.options.onGetNodes.apply(that, [item, data]); | ||
item._nodes = nodes; | ||
parentDom.append(_initRow.apply(that, [item, idx, data, parentDom])); | ||
|
||
// init sub node | ||
var len = nodes.length - 1; | ||
for (var i = 0; i <= len; i++) { | ||
var node = nodes[i]; | ||
node._level = item._level + 1; | ||
node._parent = item; | ||
if (i === len) | ||
node._last = 1; | ||
// jquery.treegrid.js | ||
that.options.rowStyle = function (item, idx) { | ||
var id = item[that.options.idField] ? item[that.options.idField] : 0; | ||
var pid = item[that.options.parentIdField] ? item[that.options.parentIdField] : 0; | ||
return { | ||
classes: 'treegrid-' + id + ' treegrid-parent-' + pid | ||
}; | ||
}; | ||
initTr.apply(that, [node, $.inArray(node, data), data, parentDom]); | ||
} | ||
}; | ||
|
||
// tr | ||
BootstrapTable.prototype.initRow = function (item, idx, data, parentDom) { | ||
var that = this; | ||
if (that.treeEnable) { | ||
// init root node | ||
if (that.options.onCheckRoot.apply(that, [item, data])) { | ||
if (item._level === undefined) { | ||
item._level = 0; | ||
} | ||
// jquery.treegrid.js | ||
that.options.rowStyle = function (item, idx) { | ||
var x = item[that.options.idField] ? item[that.options.idField] : 0; | ||
return { | ||
classes: 'treegrid-' + x | ||
}; | ||
}; | ||
initTr.apply(that, [item, idx, data, parentDom]); | ||
return true; | ||
} | ||
return false; | ||
} | ||
return _initRow.apply(that, Array.prototype.slice.apply(arguments)); | ||
}; | ||
}(jQuery); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "treegrid", | ||
"version": "1.0.0", | ||
"description": "Plugin to support the jquery treegrid.", | ||
"url": "https://github.com/wenzhixin/bootstrap-table/tree/master/src/extensions/treegrid", | ||
"example": "https://github.com/wenzhixin/bootstrap-table-examples/blob/master/extensions/treegrid.html", | ||
|
||
"plugins": [{ | ||
"name": "bootstrap-table-treegrid", | ||
"url": "https://github.com/wenzhixin/bootstrap-table/tree/master/src/extensions/treegrid" | ||
}], | ||
|
||
"author": { | ||
"name": "foreveryang321", | ||
"image": "https://avatars0.githubusercontent.com/u/5868190" | ||
} | ||
} |