From d2b78543ac58dd9af28e3e0b81a1b8197e0c395f Mon Sep 17 00:00:00 2001 From: Dexter Haslem Date: Mon, 14 Mar 2016 12:05:18 -0600 Subject: [PATCH] example on how to use floats in grid it was initially unclear to me how to get floats to behave properly with sort + filter, so hopefully this helps someone --- examples/js/column-filter/demo.js | 10 +++++ examples/js/column-filter/float-filter.js | 54 +++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 examples/js/column-filter/float-filter.js diff --git a/examples/js/column-filter/demo.js b/examples/js/column-filter/demo.js index 625cbcbf8..506a9ed8a 100644 --- a/examples/js/column-filter/demo.js +++ b/examples/js/column-filter/demo.js @@ -11,6 +11,7 @@ import DateFilter from './date-filter'; import DateFilterWithDefaultValue from './date-filter-with-default-value'; import CustomFilter from './custom-filter'; import AllFilter from './all-filters'; +import FloatFilter from './float-filter'; class Demo extends React.Component { render() { @@ -61,6 +62,15 @@ class Demo extends React.Component { +
+
+
Floating point number Filter Example
+
+
Source in /examples/js/column-filter/float-filter.js
+ +
+
+
Number Filter With Default Value Example
diff --git a/examples/js/column-filter/float-filter.js b/examples/js/column-filter/float-filter.js new file mode 100644 index 000000000..dce58602e --- /dev/null +++ b/examples/js/column-filter/float-filter.js @@ -0,0 +1,54 @@ +'use strict'; +import React from 'react'; +import {BootstrapTable, TableHeaderColumn} from 'react-bootstrap-table'; + +var products = []; + +function addProducts(quantity) { + var startId = products.length; + for (var i = 0; i < quantity; i++) { + var id = startId + i; + products.push({ + id: id, + name: "Item name " + id, + price: (i*2 + Math.random()).toFixed(2), + }); + } +} + +function formatFloat(cell, row) { + console.log('formatFloat:', cell); + return parseFloat(cell); +} + +var sortFloat = function(l, r, order) { + var left = parseFloat(l.price); + var right = parseFloat(r.price); + if (order === 'desc') { + return right - left; + } + else { + return left - right; + } +}; + +addProducts(5); + +export default class FloatFilter extends React.Component{ + render(){ + return ( + + Product ID + Product Name + ", "<"]}} + dataSort={true} + dataFormat={formatFloat} + filterFormatted={true} + sortFunc={sortFloat}> + Product Price + + + ); + } +};