Skip to content

Commit 3acbda1

Browse files
authored
feat: add config option columnPreference.filterSortToTop to set column name order in filter dialog (#1884)
1 parent 6314de0 commit 3acbda1

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Parse Dashboard is a standalone dashboard for managing your [Parse Server](https
3737
- [App Background Color Configuration](#app-background-color-configuration)
3838
- [Other Configuration Options](#other-configuration-options)
3939
- [Prevent columns sorting](#prevent-columns-sorting)
40+
- [Custom order in the filter popup](#custom-order-in-the-filter-popup)
4041
- [Running as Express Middleware](#running-as-express-middleware)
4142
- [Deploying Parse Dashboard](#deploying-parse-dashboard)
4243
- [Preparing for Deployment](#preparing-for-deployment)
@@ -294,6 +295,29 @@ You can prevent some columns to be sortable by adding `preventSort` to columnPre
294295
]
295296
```
296297

298+
### Custom order in the filter popup
299+
300+
If you have classes with a lot of columns and you filter them often with the same columns you can sort those to the top by extending the `columnPreference` setting with the `filterSortToTop` option:
301+
302+
```json
303+
"apps": [
304+
{
305+
"columnPreference": {
306+
"_User": [
307+
{
308+
"name": "objectId",
309+
"filterSortToTop": true
310+
},
311+
{
312+
"name": "email",
313+
"filterSortToTop": true
314+
}
315+
]
316+
}
317+
}
318+
]
319+
```
320+
297321
# Running as Express Middleware
298322

299323
Instead of starting Parse Dashboard with the CLI, you can also run it as an [express](https://github.com/expressjs/express) middleware.

src/components/BrowserFilter/BrowserFilter.react.js

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export default class BrowserFilter extends React.Component {
105105
<div onClick={this.toggle} style={{ cursor: 'pointer', width: this.node.clientWidth, height: this.node.clientHeight }}></div>
106106
<div className={styles.body}>
107107
<Filter
108+
className={this.props.className}
108109
blacklist={this.state.blacklistedFilters}
109110
schema={this.props.schema}
110111
filters={this.state.filters}

src/components/Filter/Filter.react.js

+38-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import * as Filters from 'lib/Filters';
99
import { List, Map } from 'immutable';
1010
import PropTypes from 'lib/PropTypes';
1111
import React from 'react';
12+
import stringCompare from 'lib/stringCompare';
13+
import ParseApp from 'lib/ParseApp';
1214

1315
function changeField(schema, filters, index, newField) {
1416
let newFilter = new Map({
@@ -42,7 +44,7 @@ function deleteRow(filters, index) {
4244
return filters.delete(index);
4345
}
4446

45-
let Filter = ({ schema, filters, renderRow, onChange, blacklist }) => {
47+
let Filter = ({ schema, filters, renderRow, onChange, blacklist, className }, context) => {
4648
blacklist = blacklist || [];
4749
let available = Filters.availableFilters(schema, filters);
4850
return (
@@ -56,7 +58,37 @@ let Filter = ({ schema, filters, renderRow, onChange, blacklist }) => {
5658
if (fields.indexOf(field) < 0) {
5759
fields.push(field);
5860
}
59-
fields.sort();
61+
62+
// Get the column preference of the current class.
63+
const currentColumnPreference = context.currentApp.columnPreference[className];
64+
65+
// Check if the preference exists.
66+
if (currentColumnPreference) {
67+
const fieldsToSortToTop = currentColumnPreference
68+
.filter(item => item.filterSortToTop)
69+
.map(item => item.name);
70+
// Sort the fields.
71+
fields.sort((a, b) => {
72+
// Only "a" should sorted to the top.
73+
if (fieldsToSortToTop.includes(a) && !fieldsToSortToTop.includes(b)) {
74+
return -1
75+
}
76+
// Only "b" should sorted to the top.
77+
if (!fieldsToSortToTop.includes(a) && fieldsToSortToTop.includes(b)) {
78+
return 1;
79+
}
80+
// Both should sorted to the top -> they should be sorted to the same order as in the "fieldsToSortToTop" array.
81+
if (fieldsToSortToTop.includes(a) && fieldsToSortToTop.includes(b)) {
82+
return fieldsToSortToTop.indexOf(a) - fieldsToSortToTop.indexOf(b);
83+
}
84+
return stringCompare(a, b);
85+
});
86+
}
87+
// If there's no preference: Use the default sort function.
88+
else {
89+
fields.sort();
90+
}
91+
6092
let constraints = Filters.FieldConstraints[schema[field].type].filter((c) => blacklist.indexOf(c) < 0);
6193
let compareType = schema[field].type;
6294
if (Object.prototype.hasOwnProperty.call(Filters.Constraints[constraint], 'field')) {
@@ -105,3 +137,7 @@ Filter.propTypes = {
105137
'A function for rendering a row of a filter.'
106138
)
107139
};
140+
141+
Filter.contextTypes = {
142+
currentApp: PropTypes.instanceOf(ParseApp)
143+
};

0 commit comments

Comments
 (0)