Today, we will try to filter in the library db.js
First, we will create the data base magestore_db with table student:
1 | We insert some students into data base |
2 | we can filter with property and value |
3 | We also can filter with function |
4 | With filtering with function, you can custom to filter and, |
5 | You can get example code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
var server; db.open({ server: 'magestore_db', version: 1, schema: { student: { key: {keyPath: 'id'}, indexes: { id: {}, name: {}, age: {} } } } }).then(function (s) { server = s; } |
We insert some students into data base:
1 2 3 4 5 6 7 8 |
server.student.add({ id:1, name: 'Louis', Age: 30, Address: 'England' }).then(function (item) { // item stored }); |
Now, we can filter with property and value like:
1 2 3 4 5 6 7 8 9 10 11 |
server.student.query() .filter('name', 'Linda') .execute() .then(function (results) { // do something with the results }); |
We also can filter with function like this:
1 2 3 4 5 6 7 8 9 10 11 |
server.student.query() .filter(function(stu) {return stu.name == 'Linda';}) .execute() .then(function (results) { // do something with the results }); |
With filtering with function, you can custom to filter and, or … You can take a look at following example:
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 |
<script> var server; db.open({ server: 'magestore_db', version: 1, schema: { student: { key: {keyPath: 'id'}, indexes: { id: {}, name: {}, age: {} } } } }).then(function (s) { server = s; students = [ { id:1, name: 'Louis', age: 30, address: 'Canada', }, { id:2, name: 'Peter', age: 20, address: 'america', }, { id:3, name: 'Linda', age: 23, address: 'England', }, { id:4, name: 'Athen', age: 24, address: 'England', }, { id:5, name: 'Alice', age: 25, address: 'Vietname', }, ]; server.student.count() .then(function (results) { if(!results){ jQuery.each(students,function(index,value){ server.student.add(value).then(function (item) { }); }); } }); }); class Query { constructor(server) { this.server = server; console.log(this.server); this.filters = []; } addFieldToFilter(param){ this.filters.push(param); } load(){ var self = this; this.server.student.query() .filter(function(student) { var check = true; jQuery.each(self.filters,function(index,filter){ if(typeof filter.field == 'string'){ if(filter.condition == 'like'){ var value = String(filter.value).toLowerCase().replace('%', '').replace('%', ''); if((String(student[filter.field]).toLowerCase().indexOf(value)) < 0){ check = false; } } if(filter.condition == 'eq'){ if(student[filter.field] != filter.value){ check = false; } } if(filter.condition == 'neq'){ if(student[filter.field] == filter.value){ check = false; } } if(filter.condition == 'gt'){ if(student[filter.field] <= filter.value){ check = false; } } if(filter.condition.toLowerCase() == 'lt'){ if(student[filter.field] >= filter.value){ check = false; } } if(filter.condition.toLowerCase() == 'gtq'){ if(student[filter.field] < filter.value){ check = false; } } if(filter.condition.toLowerCase() == 'ltq'){ if(student[filter.field] > filter.value){ check = false; } } if(filter.condition == 'in'){ if($.isArray(filter.value) && filter.value.indexOf(student[filter.field]) < 0 ){ check = false; } } } }); return check; }) .execute() .then(function (results) { jQuery('#list-student').html(''); jQuery.each(results,function(index,stu){ var stuhtml = '<tr><td>'+stu.name + '</td><td>'+stu.age+'</td><td>'+stu.address+'</td></tr>'; jQuery('#list-student').html(jQuery('#list-student').html()+stuhtml); }); }); } } </script> |
You can get example code here.
You may want to try the Magento 2 Demo to fully experience its new awesome features. And of course, please feel free to find more Magento 2 Tutorials by our Experts.