Skip to content

Commit 7e0a5a3

Browse files
committed
Added ability to determine which columns can be utilized in filtering.
1 parent 0a69dc3 commit 7e0a5a3

File tree

7 files changed

+50
-16
lines changed

7 files changed

+50
-16
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,20 @@ This is a boolean value that determines whether or not that column can be sorted
164164
}
165165
```
166166

167+
#### filterable
168+
169+
This is a boolean value that determines whether or not that column should be utilized in filtering. Filtering is enabled by default on all columns except for component columns. Component columns do not currently support filtering. To disable filtering on any particular column, define `filterable` as `false`:
170+
171+
```
172+
{
173+
label: 'Email',
174+
field: 'email',
175+
filterable: false
176+
}
177+
```
178+
179+
Filtering works such that the text in the filter input is split by white space, and each word is then checked against every filterable column. If each word appears at least once in any column, then that row passes and is displayed in the table.
180+
167181
### rows
168182

169183
This is the data that is represented by each row in the table. It must be an array of objects where the key of each object is the value for your column.field entries. For example, if you have a column entry like this:

dist/vuejs-datatable.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/vue-1/app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ vm = new Vue({
66
el: 'body',
77
data: {
88
columns: [
9-
{label: 'ID', field: 'id', align: 'center'},
9+
{label: 'ID', field: 'id', align: 'center', filterable: false},
1010
{label: 'Username', field: 'username'},
1111
{label: 'First Name', field: 'first_name'},
1212
{label: 'Last Name', field: 'last_name'},

examples/vue-2/app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Vue.component('custom-filter-bar', {
100100

101101
window.getColumns = function(){
102102
return [
103-
{label: 'ID', field: 'id', align: 'center'},
103+
{label: 'ID', field: 'id', align: 'center', filterable: false},
104104
{label: 'Username', field: 'username'},
105105
{label: 'First Name', field: 'first_name'},
106106
{label: 'Last Name', field: 'last_name'},

examples/vue-2/build/app.js

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/filter-bar.vue

+23-8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<script>
4242
module.exports = {
4343
props: {
44+
columns: [Object, Array],
4445
rows: [Object, Array],
4546
filterable: {
4647
type: Boolean,
@@ -74,21 +75,35 @@ module.exports = {
7475
}
7576
7677
var pass = false;
78+
// var i = 0;
7779
78-
for(var c in row){
79-
var column = row[c];
80+
for(var i in this.columns){
81+
var column_definition = this.columns[i];
82+
var column_text = '';
8083
81-
if(column === null){
84+
if(!column_definition.filterable){
8285
continue;
8386
}
8487
85-
column = '' + column + '';
88+
if(column_definition.field){
89+
column_text = row[column_definition.field];
90+
}else if(typeof column_definition.callback === 'function'){
91+
column_text = (column_definition.callback)(row);
92+
}else{
93+
continue;
94+
}
95+
96+
if(!column_text){
97+
continue;
98+
}
99+
100+
column_text = ('' + column_text + '').trim();
86101
87-
if(typeof column.toLowerCase === 'function'){
88-
column = column.toLowerCase();
102+
if(typeof column_text.toLowerCase === 'function'){
103+
column_text = column_text.toLowerCase();
89104
}
90105
91-
if(column.indexOf(filter_word) !== -1){
106+
if(column_text.indexOf(filter_word) !== -1){
92107
var pass = true;
93108
}
94109
}
@@ -99,7 +114,7 @@ module.exports = {
99114
}
100115
101116
return true;
102-
}.bind(this));
117+
}, this);
103118
}
104119
105120
return rows;

src/vue-datatable.vue

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<component :is="filterBar"
1212
v-if="filterable || paginate"
13+
:columns="column_props"
1314
:rows="sorted_rows"
1415
:filterable="filterable"
1516
:paginate="paginate"
@@ -80,12 +81,16 @@ module.exports = {
8081
return this.columns.map(function(column){
8182
var sortable = typeof column.sortable === 'undefined' ? true : column.sortable;
8283
sortable = column.component ? false : sortable;
84+
85+
var filterable = typeof column.filterable === 'undefined' ? true : column.filterable;
86+
filterable = column.component ? false : filterable;
8387
8488
return {
8589
id: i++,
8690
label: column.label || '',
8791
align: column.align || 'left',
8892
sortable: sortable,
93+
filterable: filterable,
8994
field: column.field || null,
9095
callback: column.callback || null,
9196
component: column.component || null

0 commit comments

Comments
 (0)