Let’s learn how to query with sort index db in Magento 2 here and we will try to sort in the library db.js. in Magento 2,
Develop an eCommerce marketplace like eBay and Amazon platform with tailor-made solution!!!
First, we will build the database Webnexs_db with student table:
- We include some students in the database
- we can sort with property and value
- We also can sort with function
- With sorting with function, you can custom to sort and,
- You can find example code
Querying with sorting Indexed DB.js in Magento 2
var server; db.open({ server: 'Webnexs_db', version: 1, schema: { student: { key: {keyPath: 'id'}, indexes: { id: {}, name: {}, age: {} } } } }).then(function (s) { server = s; }
We add some students to the database:
server.student.add({ id:1, name: 'Louis', Age: 30, Address: 'England' }).then(function (item) { // item stored });
Then, we can sort with property and value like:
server.student.query() .filter('name', 'Linda') .execute() .then(function (results) { // do something with the results });
We also can sort with function like this:
server.student.query() .filter(function(stu) {return stu.name == 'Linda';}) .execute() .then(function (results) { // do something with the results });
With sorting with function, you can custom to sort or you can follow the below example:
<script> var server; db.open({ server: 'Webnexs_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>
I hope the following steps mentioned above will help you to querying with sorting Indexed db using library DB.js in Magento 2.You can try our Magento 2 Demo to whole experience its new awesome features. For more queries feel free to find more Magento 2 Tutorials by our Experts.
Leave a Reply