-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.html
141 lines (139 loc) · 5.31 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>QueryBuilder react integration example</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://rawgit.com/mistic100/jQuery-QueryBuilder/master/dist/js/query-builder.standalone.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link href="http://querybuilder.js.org/dist/jQuery-QueryBuilder/dist/css/query-builder.default.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js"></script>
<script src="https://fb.me/react-0.14.7.js"></script>
<script src="https://fb.me/react-dom-0.14.7.js"></script>
<script type="text/babel">
const defaultRules = {
condition: 'AND',
rules: [{
id: 'price',
operator: 'less',
value: 10.25
}, {
condition: 'OR',
rules: [{
id: 'category',
operator: 'equal',
value: 2
}, {
id: 'category',
operator: 'equal',
value: 1
}]
}]
};
function initializeQueryBuilder(element, newRules) {
const filters = [{
id: 'name',
label: 'Name',
type: 'string'
}, {
id: 'category',
label: 'Category',
type: 'integer',
input: 'select',
values: {
1: 'Books',
2: 'Movies',
3: 'Music',
4: 'Tools',
5: 'Goodies',
6: 'Clothes'
},
operators: ['equal', 'not_equal', 'in', 'not_in', 'is_null', 'is_not_null']
}, {
id: 'in_stock',
label: 'In stock',
type: 'integer',
input: 'radio',
values: {
1: 'Yes',
0: 'No'
},
operators: ['equal']
}, {
id: 'price',
label: 'Price',
type: 'double',
validation: {
min: 0,
step: 0.01
}
}, {
id: 'id',
label: 'Identifier',
type: 'string',
placeholder: '____-____-____',
operators: ['equal', 'not_equal'],
validation: {
format: /^.{4}-.{4}-.{4}$/
}
}];
const rules = newRules ? newRules : defaultRules;
$(element).queryBuilder({filters, rules});
}
class QueryBuilderWrapper extends React.Component {
constructor(props) {
super(props);
this.state = {
rules: {}
};
}
componentDidMount() {
const element = this.refs.queryBuilder;
initializeQueryBuilder(element);
}
componentWillUnmount() {
$(this.refs.queryBuilder).queryBuilder('destroy');
}
shouldComponentUpdate() {
return false;
}
// get data from jQuery Query Builder and pass to the react component
handleGetRulesClick() {
const rules = $(this.refs.queryBuilder).queryBuilder('getRules');
this.setState({rules: rules});
this.forceUpdate();
}
// reinitialize jQuery Query Builder based on react state
handleSetRulesClick() {
const newRules = {...defaultRules};
newRules.rules[0].value = newRules.rules[0].value + 10;
$(this.refs.queryBuilder).queryBuilder('setRules', newRules);
this.setState({rules: newRules});
}
render() {
return (
<div>
<div id='query-builder' ref='queryBuilder'/>
<div className='row'>
<div className='col-md-4'>
<button className='btn btn-success' onClick={this.handleGetRulesClick.bind(this)}>GET RULES FROM QUERY BUILDER</button>
</div>
<div className='col-md-4'>
<button className='btn btn-success' onClick={this.handleSetRulesClick.bind(this)}>SET RULES FROM REACT</button>
</div>
</div>
<pre>
Component state:
{JSON.stringify(this.state.rules, undefined, 2)}
</pre>
</div>
);
}
};
ReactDOM.render(React.createElement(QueryBuilderWrapper), document.getElementById('react-app'));
</script>
</head>
<body>
<div id="react-app"></div>
</body>
</html>