Skip to content

Commit

Permalink
Allow conditionally specify columns
Browse files Browse the repository at this point in the history
Fix: #1317
  • Loading branch information
kant2002 committed May 13, 2017
1 parent 47f6e34 commit 9b748df
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
16 changes: 15 additions & 1 deletion src/BootstrapTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class BootstrapTable extends Component {

const isKeyFieldDefined = typeof keyField === 'string' && keyField.length;
React.Children.forEach(props.children, column => {
if (column === null || column === undefined) {
// Skip null and undefined value
return;
}
if (column.props.isKey) {
if (keyField) {
throw new Error('Error. Multiple key column be detected in TableHeaderColumn.');
Expand Down Expand Up @@ -141,11 +145,21 @@ class BootstrapTable extends Component {
getColumnsDescription({ children }) {
let rowCount = 0;
React.Children.forEach(children, (column) => {
if (column === null || column === undefined) {
// Skip null and undefined value
return;
}

if (Number(column.props.row) > rowCount) {
rowCount = Number(column.props.row);
}
});
return React.Children.map(children, (column, i) => {
if (column === null || column === undefined) {
// Return null for empty objects
return null;
}

const rowIndex = column.props.row ? Number(column.props.row) : 0;
const rowSpan = column.props.rowSpan ? Number(column.props.rowSpan) : 1;
if ((rowSpan + rowIndex) === (rowCount + 1)) {
Expand Down Expand Up @@ -1250,7 +1264,7 @@ class BootstrapTable extends Component {
}
}
} else {
React.Children.forEach(this.props.children, (child, i) => {
React.Children.forEach(this.props.children.filter(_ => !!_), (child, i) => {
if (child.props.width) {
header[i].style.width = `${child.props.width}px`;
header[i].style.minWidth = `${child.props.width}px`;
Expand Down
4 changes: 2 additions & 2 deletions src/TableBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class TableBody extends Component {
keyBoardNav.customStyle :
null;
const ExpandColumnCustomComponent = this.props.expandColumnOptions.expandColumnComponent;
let expandColSpan = this.props.columns.filter(col => !col.hidden).length;
let expandColSpan = this.props.columns.filter(col => col && !col.hidden).length;
if (isSelectRowDefined && !this.props.selectRow.hideSelectColumn) {
expandColSpan += 1;
}
Expand All @@ -53,7 +53,7 @@ class TableBody extends Component {
}

let tableRows = this.props.data.map(function(data, r) {
const tableColumns = this.props.columns.map(function(column, i) {
const tableColumns = this.props.columns.filter(_ => _ != null).map(function(column, i) {
const fieldValue = data[column.name];
const isFocusCell = r === y && i === x;
if (column.name !== this.props.keyField && // Key field can't be edit
Expand Down
6 changes: 5 additions & 1 deletion src/TableHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class TableHeader extends Component {
}, this.props.tableHeaderClass);

const rowCount = Math.max(...React.Children.map(this.props.children, elm =>
elm.props.row ? Number(elm.props.row) : 0
(elm && elm.props.row) ? Number(elm.props.row) : 0
));

const rows = [];
Expand All @@ -66,6 +66,10 @@ class TableHeader extends Component {
const { sortIndicator, sortList, onSort, reset } = this.props;

React.Children.forEach(this.props.children, (elm) => {
if (elm === null || elm === undefined) {
// Skip null or undefined elements.
return;
}
const { dataField, dataSort } = elm.props;
const sort = getSortOrder(sortList, dataField, dataSort);
const rowIndex = elm.props.row ? Number(elm.props.row) : 0;
Expand Down

0 comments on commit 9b748df

Please sign in to comment.