Kurounin / Pagination

This package allows you to paginate the subscriptions over meteor's collections. It can be used in a Blaze template or in ReactJS.
MIT License
56 stars 22 forks source link

Search in all Pages from Pagination #91

Closed Dev24Patrick closed 5 years ago

Dev24Patrick commented 5 years ago

Hi, i'm using this Pagination and it works very good.

I have a question for filtering over all on client side. i'm using this code:

client side (test.js):

if (isAdminForFilter === true){

     filter = {`
          barcode: {
            $regex: Template.instance().search.get()
          },
          typeOfEntry:{
            $regex: typeOfEntry.get()
          },
          completed: true,    
      }
}
else{ 
      filter = {
          barcode: {
            $regex: Template.instance().search.get()
          },
          companyId: {
            $regex: cmpyIdForFilter
          },
          typeOfEntry:{
            $regex: typeOfEntry.get()
          },
          completed: true,
      }
}

this.pagination = new Meteor.Pagination(damage, {
    filters:{filter
            },
    sort: {
        createdAt: 1,
    },
});

var dataForCheck = Template.instance().pagination.getPage();

Is it possible to search and filter directly and automatic refresh from the pagination list on client side?

Thanks fpr your help

Kurounin commented 5 years ago

Hi,

To update the filters automatically you should call filters() method inside a Tracker autorun. For example:

Template.myList.onCreated(function () {
    this.search = new ReactiveVar("");
    this.typeOfEntry= new ReactiveVar("");

    this.pagination = new Meteor.Pagination(damage, {
        filters: {}, // you can also set an initial filter here
        sort: {
            createdAt: 1,
        }
    });

    this.autorun(() => {
        // change filter every time one of the reactive vars change
        const filter = {
            barcode: {
                $regex: this.search.get()
            },
            typeOfEntry:{
                $regex: this.typeOfEntry.get()
            },
            completed: true,    
        };

        if (!isAdminForFilter === true){
            filter.companyId = {
                $regex: cmpyIdForFilter
            };
        }

        this.pagination.filters(filter);
    });
});

Although if you want to add an additional company filter if user is not an admin you should use the server side dynamic_filters or transform_filters options when initializing the pagination.

Dev24Patrick commented 5 years ago

Hi,

 

thanks for this help. It works fine :)

 

At leat one question. Is there something different by the filter to search between a date?

 

Example:

 

  let timestampFrom = (new Date(this.filterFrom.get())).getTime();   let timestampTo = new Date(this.filterTo.get());   timestampTo.setHours(23);   timestampTo.setMinutes(59);   timestampTo.setSeconds(59);   timestampTo = timestampTo.getTime();

 

  filter = {                   barcode: {                     $regex: this.search.get()                   },                   typeOfEntry:{                     $regex: typeOfEntry.get()                   },                   createdAt:{                     "$gte": timestampFrom,                     "$lt": timestampTo                   },                   deletedAt: false,                        completed: true,                    }

 

 Thanks for your help

 

 

Best regards Patrick

 

 

Gesendet: Freitag, 16. November 2018 um 16:23 Uhr Von: Kurounin notifications@github.com An: Kurounin/Pagination Pagination@noreply.github.com Cc: Dev24Patrick PWamser@gmx.de, Author author@noreply.github.com Betreff: Re: [Kurounin/Pagination] Search in all Pages from Pagination (#91)

Hi,

To update the filters automatically you should call filters() method inside a Tracker autorun. For example:

Template.myList.onCreated(function () { this.search = new ReactiveVar(""); this.typeOfEntry= new ReactiveVar("");

this.pagination = new Meteor.Pagination(damage, {
    filters: {}, // you can also set an initial filter here
    sort: {
        createdAt: 1,
    }
});

this.autorun(() => {
    // change filter every time one of the reactive vars change
    const filter = {
        barcode: {
            $regex: this.search.get()
        },
        typeOfEntry:{
            $regex: this.typeOfEntry.get()
        },
        completed: true,    
    };

    if (!isAdminForFilter === true){
        filter.companyId = {
            $regex: cmpyIdForFilter
        };
    }

    this.pagination.filters(filter);
});

});

Although if you want to add an additional company filter if user is not an admin you should use the server side dynamic_filters or transform_filters options when initializing the pagination.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

Kurounin commented 5 years ago

If you're storing createdAt as a number, then your query is correct. If you're storing it as a date you should use dates to filter:

                  createdAt:{
                    "$gte": newDate(timestampFrom),
                    "$lt": new Date(timestampTo)
                  },
Dev24Patrick commented 5 years ago

Hi,

 

i fixed this problem and your package works perfect, thanks for this great job!

 

i only have one question, maybe you have an idea how to realize this with your package:

 

current publish:

 

Meteor.publish('users', function() {         if ( !this.userId ) return this.ready();         let user = Meteor.users.findOne(this.userId);

        if (  user.profile.admin === true) {             return Meteor.users.find();         }         else {             if (user.emails[0].address === null || user.emails[0].address === undefined){                 return this.ready();             }             else{                 return Meteor.users.find();             }             }     });

 

 

publish with your package:

?

 

 

thanks for your help

 

regards Patrick

 

Gesendet: Montag, 19. November 2018 um 19:58 Uhr Von: Kurounin notifications@github.com An: Kurounin/Pagination Pagination@noreply.github.com Cc: Dev24Patrick PWamser@gmx.de, Author author@noreply.github.com Betreff: Re: [Kurounin/Pagination] Search in all Pages from Pagination (#91)

If you're storing createdAt as a number, then your query is correct. If you're storing it as a date you should use dates to filter:

              createdAt:{
                "$gte": newDate(timestampFrom),
                "$lt": new Date(timestampTo)
              },

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

Kurounin commented 5 years ago

To accomplish that you can use the dynamic_filters option on server side:

publishPagination(MyCollection, {
    dynamic_filters: function () {
        if ( !this.userId ) {
            return { _id: 0 }; // apply an impossible filter to not return anything
        }

        const user =  Meteor.user();

        if (  user.profile.admin === true) {
            return {}; // do not apply any additional filters to execute find with existing filters
        } else {
            if (user.emails[0].address === null || user.emails[0].address === undefined){
                return  { _id: 0 }; // apply an impossible filter to not return anything
            }
            else{
                return {}; // do not apply any additional filters to execute find with existing filters
            }    
        }
    },
});