Querying with sorting Indexed db using library DB.js

Today we will try to sort in the library db.js. in Magento 2

First, we will build the database Webnexs_db with student table

  1. We include some students into data base
  2. we can sort with property and value
  3. We also can sort with function
  4. With sorting with function, you can custom to sort and,
  5. You can find example code
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 into data base:

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 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

Last Update: July 17, 2018  

February 16, 2018   299   Nandini R    Java Script    
Total 1 Votes:
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
INSTAGRAM
LinkedIn