Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advanced filtering added #177

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# EditorConfig is awesome: http://EditorConfig.org

root = true

[*]
end_of_line = lf
insert_final_newline = true

[*.{js,jsx}]
charset = utf-8
indent_style = space
indent_size = 4
334 changes: 183 additions & 151 deletions build/griddle.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,49 @@ return /******/ (function(modules) { // webpackBootstrap
var RowProperties = __webpack_require__(5);
var _ = __webpack_require__(3);


var filter_arr = (function () {
function filter_data(results, filter, column) {
return _.filter(results, function (item) {
var arr = column ? [item[column]] : _.values(item);
for (var i = 0, len = arr.length; i < len; i++) {
if (_.isRegExp(filter)) {
if (filter.test(arr[i])) {
return true;
}
} else if (!_.isEmpty(arr[i])) {
if ((arr[i] || "").toString().toLowerCase().indexOf(filter.toLowerCase()) >= 0) {
return true;
}
}
}
return false;
});
}
function filter_obj(results, filter) {
var outputResults = results;
_.forEach(filter, function (filterEntry, column) {
if (!_.isEmpty(filterEntry)) {
outputResults = filter_data(outputResults, filterEntry, column);
}
});
return outputResults;
}
return function (results, filter) {
var outputResults = results;
_.forEach(filter, function (filterEntry) {
if (!_.isEmpty(filterEntry)) {
if (_.isString(filterEntry)) {
outputResults = filter_data(outputResults, filterEntry);
} else if (_.isPlainObject(filterEntry)) {
outputResults = filter_obj(outputResults, filterEntry);
}
}
});
return outputResults;
};
})();

var Griddle = React.createClass({
displayName: "Griddle",
statics: {
Expand Down Expand Up @@ -189,34 +232,23 @@ return /******/ (function(modules) { // webpackBootstrap
}

var that = this,
updatedState = {
page: 0,
filter: filter
};

// Obtain the state results.
updatedState.filteredResults = _.filter(this.props.results, function (item) {
var arr = _.values(item);
for (var i = 0; i < arr.length; i++) {
if ((arr[i] || "").toString().toLowerCase().indexOf(filter.toLowerCase()) >= 0) {
return true;
}
}

return false;
});

// Update the max page.
updatedState.maxPage = that.getMaxPage(updatedState.filteredResults);

filteredResults = null;
//if filter is null or undefined reset the filter.
if (_.isUndefined(filter) || _.isNull(filter) || _.isEmpty(filter)) {
updatedState.filter = filter;
updatedState.filteredResults = null;
if (!_.isEmpty(filter)) {
// Obtain the state results.
if (!_.isArray(filter)) {
filter = [filter];
}
filteredResults = filter_arr(this.props.results, filter);
}

// Set the state.
that.setState(updatedState);
that.setState({
page: 0,
filter: filter,
filteredResults: filteredResults,
maxPage: that.getMaxPage(filteredResults)
});

this._resetSelectedRows();
},
Expand Down Expand Up @@ -1725,147 +1757,147 @@ return /******/ (function(modules) { // webpackBootstrap
var powerPick = __webpack_require__(16);

var GridRow = React.createClass({
displayName: "GridRow",
getDefaultProps: function () {
return {
isChildRow: false,
showChildren: false,
data: {},
columnSettings: null,
rowSettings: null,
hasChildren: false,
useGriddleStyles: true,
useGriddleIcons: true,
isSubGriddle: false,
paddingHeight: null,
rowHeight: null,
parentRowCollapsedClassName: "parent-row",
parentRowExpandedClassName: "parent-row expanded",
parentRowCollapsedComponent: "▶",
parentRowExpandedComponent: "▼",
onRowClick: null,
multipleSelectionSettings: null
};
},
handleClick: function (e) {
if (this.props.onRowClick !== null && _.isFunction(this.props.onRowClick)) {
this.props.onRowClick(this, e);
} else if (this.props.hasChildren) {
this.props.toggleChildren();
}
},
handleSelectionChange: function (e) {
//hack to get around warning that's not super useful in this case
return;
},
handleSelectClick: function (e) {
if (this.props.multipleSelectionSettings.isMultipleSelection) {
if (e.target.type === "checkbox") {
this.props.multipleSelectionSettings.toggleSelectRow(this.props.data, this.refs.selected.getDOMNode().checked);
} else {
this.props.multipleSelectionSettings.toggleSelectRow(this.props.data, !this.refs.selected.getDOMNode().checked);
}
}
},
verifyProps: function () {
if (this.props.columnSettings === null) {
console.error("gridRow: The columnSettings prop is null and it shouldn't be");
}
},
render: function () {
var _this = this;
this.verifyProps();
var that = this;
var columnStyles = null;

if (this.props.useGriddleStyles) {
columnStyles = {
margin: "0",
padding: that.props.paddingHeight + "px 5px " + that.props.paddingHeight + "px 5px",
height: that.props.rowHeight ? this.props.rowHeight - that.props.paddingHeight * 2 + "px" : null,
backgroundColor: "#FFF",
borderTopColor: "#DDD",
color: "#222"
};
}
displayName: "GridRow",
getDefaultProps: function () {
return {
isChildRow: false,
showChildren: false,
data: {},
columnSettings: null,
rowSettings: null,
hasChildren: false,
useGriddleStyles: true,
useGriddleIcons: true,
isSubGriddle: false,
paddingHeight: null,
rowHeight: null,
parentRowCollapsedClassName: "parent-row",
parentRowExpandedClassName: "parent-row expanded",
parentRowCollapsedComponent: "▶",
parentRowExpandedComponent: "▼",
onRowClick: null,
multipleSelectionSettings: null
};
},
handleClick: function (e) {
if (this.props.onRowClick !== null && _.isFunction(this.props.onRowClick)) {
this.props.onRowClick(this, e);
} else if (this.props.hasChildren) {
this.props.toggleChildren();
}
},
handleSelectionChange: function (e) {
//hack to get around warning that's not super useful in this case
return;
},
handleSelectClick: function (e) {
if (this.props.multipleSelectionSettings.isMultipleSelection) {
if (e.target.type === "checkbox") {
this.props.multipleSelectionSettings.toggleSelectRow(this.props.data, this.refs.selected.getDOMNode().checked);
} else {
this.props.multipleSelectionSettings.toggleSelectRow(this.props.data, !this.refs.selected.getDOMNode().checked);
}
}
},
verifyProps: function () {
if (this.props.columnSettings === null) {
console.error("gridRow: The columnSettings prop is null and it shouldn't be");
}
},
render: function () {
var _this = this;
this.verifyProps();
var that = this;
var columnStyles = null;

var columns = this.props.columnSettings.getColumns();
if (this.props.useGriddleStyles) {
columnStyles = {
margin: "0",
padding: that.props.paddingHeight + "px 5px " + that.props.paddingHeight + "px 5px",
height: that.props.rowHeight ? this.props.rowHeight - that.props.paddingHeight * 2 + "px" : null,
backgroundColor: "#FFF",
borderTopColor: "#DDD",
color: "#222"
};
}

// make sure that all the columns we need have default empty values
// otherwise they will get clipped
var defaults = _.object(columns, []);
var columns = this.props.columnSettings.getColumns();

// creates a 'view' on top the data so we will not alter the original data but will allow us to add default values to missing columns
var dataView = Object.create(this.props.data);
// make sure that all the columns we need have default empty values
// otherwise they will get clipped
var defaults = _.object(columns, []);

_.defaults(dataView, defaults);
// creates a 'view' on top the data so we will not alter the original data but will allow us to add default values to missing columns
var dataView = Object.create(this.props.data);

var data = _.pairs(powerPick(dataView, columns));
_.defaults(dataView, defaults);

var nodes = data.map(function (col, index) {
var returnValue = null;
var meta = _this.props.columnSettings.getColumnMetadataByName(col[0]);
var data = _.pairs(powerPick(dataView, columns));

//todo: Make this not as ridiculous looking
var firstColAppend = index === 0 && _this.props.hasChildren && _this.props.showChildren === false && _this.props.useGriddleIcons ? React.createElement(
"span",
{ style: _this.props.useGriddleStyles ? { fontSize: "10px", marginRight: "5px" } : null },
_this.props.parentRowCollapsedComponent
) : index === 0 && _this.props.hasChildren && _this.props.showChildren && _this.props.useGriddleIcons ? React.createElement(
"span",
{ style: _this.props.useGriddleStyles ? { fontSize: "10px" } : null },
_this.props.parentRowExpandedComponent
) : "";
var nodes = data.map(function (col, index) {
var returnValue = null;
var meta = _this.props.columnSettings.getColumnMetadataByName(col[0]);

if (index === 0 && _this.props.isChildRow && _this.props.useGriddleStyles) {
columnStyles = _.extend(columnStyles, { paddingLeft: 10 });
}
//todo: Make this not as ridiculous looking
var firstColAppend = index === 0 && _this.props.hasChildren && _this.props.showChildren === false && _this.props.useGriddleIcons ? React.createElement(
"span",
{ style: _this.props.useGriddleStyles ? { fontSize: "10px", marginRight: "5px" } : null },
_this.props.parentRowCollapsedComponent
) : index === 0 && _this.props.hasChildren && _this.props.showChildren && _this.props.useGriddleIcons ? React.createElement(
"span",
{ style: _this.props.useGriddleStyles ? { fontSize: "10px" } : null },
_this.props.parentRowExpandedComponent
) : "";

if (_this.props.columnSettings.hasColumnMetadata() && typeof meta !== "undefined") {
var colData = typeof meta.customComponent === "undefined" || meta.customComponent === null ? col[1] : React.createElement(meta.customComponent, { data: col[1], rowData: dataView, metadata: meta });
returnValue = meta == null ? returnValue : React.createElement(
"td",
{ onClick: _this.handleClick, className: meta.cssClassName, key: index, style: columnStyles },
colData
);
}
if (index === 0 && _this.props.isChildRow && _this.props.useGriddleStyles) {
columnStyles = _.extend(columnStyles, { paddingLeft: 10 });
}

return returnValue || React.createElement(
"td",
{ onClick: _this.handleClick, key: index, style: columnStyles },
firstColAppend,
col[1]
);
});
if (_this.props.columnSettings.hasColumnMetadata() && typeof meta !== "undefined") {
var colData = typeof meta.customComponent === "undefined" || meta.customComponent === null ? col[1] : React.createElement(meta.customComponent, { data: col[1], rowData: dataView, metadata: meta });
returnValue = meta == null ? returnValue : React.createElement(
"td",
{ onClick: _this.handleClick, className: meta.cssClassName, key: index, style: columnStyles },
colData
);
}

if (nodes && this.props.multipleSelectionSettings && this.props.multipleSelectionSettings.isMultipleSelection) {
var selectedRowIds = this.props.multipleSelectionSettings.getSelectedRowIds();

nodes.unshift(React.createElement(
"td",
{ key: "selection", style: columnStyles },
React.createElement("input", {
type: "checkbox",
checked: this.props.multipleSelectionSettings.getIsRowChecked(dataView),
onChange: this.handleSelectionChange,
ref: "selected" })
));
}
return returnValue || React.createElement(
"td",
{ onClick: _this.handleClick, key: index, style: columnStyles },
firstColAppend,
col[1]
);
});

if (nodes && this.props.multipleSelectionSettings && this.props.multipleSelectionSettings.isMultipleSelection) {
var selectedRowIds = this.props.multipleSelectionSettings.getSelectedRowIds();

nodes.unshift(React.createElement(
"td",
{ key: "selection", style: columnStyles },
React.createElement("input", {
type: "checkbox",
checked: this.props.multipleSelectionSettings.getIsRowChecked(dataView),
onChange: this.handleSelectionChange,
ref: "selected" })
));
}

//Get the row from the row settings.
var className = that.props.rowSettings && that.props.rowSettings.getBodyRowMetadataClass(that.props.data) || "standard-row";
//Get the row from the row settings.
var className = that.props.rowSettings && that.props.rowSettings.getBodyRowMetadataClass(that.props.data) || "standard-row";

if (that.props.isChildRow) {
className = "child-row";
} else if (that.props.hasChildren) {
className = that.props.showChildren ? this.props.parentRowExpandedClassName : this.props.parentRowCollapsedClassName;
if (that.props.isChildRow) {
className = "child-row";
} else if (that.props.hasChildren) {
className = that.props.showChildren ? this.props.parentRowExpandedClassName : this.props.parentRowCollapsedClassName;
}
return React.createElement(
"tr",
{ onClick: this.props.multipleSelectionSettings && this.props.multipleSelectionSettings.isMultipleSelection ? this.handleSelectClick : null, className: className },
nodes
);
}
return React.createElement(
"tr",
{ onClick: this.props.multipleSelectionSettings && this.props.multipleSelectionSettings.isMultipleSelection ? this.handleSelectClick : null, className: className },
nodes
);
}
});

module.exports = GridRow;
Expand Down
Loading