+
{this.props.loadingText}
@@ -477,8 +420,24 @@ export default React.createClass({
)
},
+ // User actions
+ setPage (page) {
+ this.setState({
+ page
+ }, () => {
+ this.fireOnChange()
+ })
+ },
+ nextPage (e) {
+ e.preventDefault()
+ this.setPage(this.state.page + 1)
+ },
+ previousPage (e) {
+ e.preventDefault()
+ this.setPage(this.state.page - 1)
+ },
sortColumn (column, additive) {
- const existingSorting = this.state.sorting || []
+ const existingSorting = this.getSorting()
let sorting = _.clone(this.state.sorting || [])
const existingIndex = sorting.findIndex(d => d.id === column.id)
if (existingIndex > -1) {
@@ -510,95 +469,11 @@ export default React.createClass({
}
}
const page = (existingIndex === 0 || (!existingSorting.length && sorting.length) || !additive) ? 0 : this.state.page
- this.buildData(this.props, Object.assign({}, this.state, {page, sorting}))
- },
- nextPage (e) {
- e.preventDefault()
- this.setPage(this.state.page + 1)
- },
- previousPage (e) {
- e.preventDefault()
- this.setPage(this.state.page - 1)
+ this.setState({
+ page,
+ sorting
+ }, () => {
+ this.fireOnChange()
+ })
}
})
-
-// ########################################################################
-// Utils
-// ########################################################################
-
-function remove (a, b) {
- return a.filter(function (o, i) {
- var r = b(o)
- if (r) {
- a.splice(i, 1)
- return true
- }
- return false
- })
-}
-
-function get (a, b) {
- if (isArray(b)) {
- b = b.join('.')
- }
- return b
- .replace('[', '.').replace(']', '')
- .split('.')
- .reduce(
- function (obj, property) {
- return obj[property]
- }, a
- )
-}
-
-function takeRight (arr, n) {
- const start = n > arr.length ? 0 : arr.length - n
- return arr.slice(start)
-}
-
-function last (arr) {
- return arr[arr.length - 1]
-}
-
-function range (n) {
- const arr = []
- for (let i = 0; i < n; i++) {
- arr.push(n)
- }
- return arr
-}
-
-function orderBy (arr, funcs, dirs) {
- return arr.sort((a, b) => {
- for (let i = 0; i < funcs.length; i++) {
- const comp = funcs[i]
- const ca = comp(a)
- const cb = comp(b)
- const desc = dirs[i] === false || dirs[i] === 'desc'
- if (ca > cb) {
- return desc ? -1 : 1
- }
- if (ca < cb) {
- return desc ? 1 : -1
- }
- }
- return 0
- })
-}
-
-function clone (a) {
- return JSON.parse(JSON.stringify(a, function (key, value) {
- if (typeof value === 'function') {
- return value.toString()
- }
- return value
- }))
-}
-
-// ########################################################################
-// Helpers
-// ########################################################################
-
-function isArray (a) {
- return Array.isArray(a)
-}
diff --git a/src/utils.js b/src/utils.js
new file mode 100644
index 0000000..edee136
--- /dev/null
+++ b/src/utils.js
@@ -0,0 +1,95 @@
+export default {
+ get,
+ takeRight,
+ last,
+ orderBy,
+ range,
+ clone,
+ remove,
+ getFirstDefined
+}
+
+function remove (a, b) {
+ return a.filter(function (o, i) {
+ var r = b(o)
+ if (r) {
+ a.splice(i, 1)
+ return true
+ }
+ return false
+ })
+}
+
+function get (a, b) {
+ if (isArray(b)) {
+ b = b.join('.')
+ }
+ return b
+ .replace('[', '.').replace(']', '')
+ .split('.')
+ .reduce(
+ function (obj, property) {
+ return obj[property]
+ }, a
+ )
+}
+
+function takeRight (arr, n) {
+ const start = n > arr.length ? 0 : arr.length - n
+ return arr.slice(start)
+}
+
+function last (arr) {
+ return arr[arr.length - 1]
+}
+
+function range (n) {
+ const arr = []
+ for (let i = 0; i < n; i++) {
+ arr.push(n)
+ }
+ return arr
+}
+
+function orderBy (arr, funcs, dirs) {
+ return arr.sort((a, b) => {
+ for (let i = 0; i < funcs.length; i++) {
+ const comp = funcs[i]
+ const ca = comp(a)
+ const cb = comp(b)
+ const desc = dirs[i] === false || dirs[i] === 'desc'
+ if (ca > cb) {
+ return desc ? -1 : 1
+ }
+ if (ca < cb) {
+ return desc ? 1 : -1
+ }
+ }
+ return 0
+ })
+}
+
+function clone (a) {
+ return JSON.parse(JSON.stringify(a, function (key, value) {
+ if (typeof value === 'function') {
+ return value.toString()
+ }
+ return value
+ }))
+}
+
+// ########################################################################
+// Helpers
+// ########################################################################
+
+function isArray (a) {
+ return Array.isArray(a)
+}
+
+function getFirstDefined (...args) {
+ for (var i = 0; i < args.length; i++) {
+ if (typeof args[i] !== 'undefined') {
+ return args[i]
+ }
+ }
+}