Search code examples
dojostoredgriddstore

Dojo dgrid: Filter data from store with diffrent fields when I click on filter button


I am using 'dgrid/Grid' and dstore/RequestMemory for creating grid and storing data. Now I want to filter data according to values in the fields(see img). I am not sure how to filter data when using simple Dgrid and dstore.

 var structure = [{
            label : "Value Date",
            field : "valueDate"
        }, {
            id: "currencyCol",
            label : "Currency",
            field : "currency"
        }, {
            label : "Nostro",
            field : "nostroAgent"
        }];

        var store= new RequestMemory({
            target: 'getReportData',
            idProperty: "cashflowId",
            headers: structure
        });
        // Create an instance of OnDemandGrid referencing the store
        var grid = new(declare([Grid, Pagination, Selection]))({
            collection: store,
            columns: structure,
            loadingMessage: 'Loading data...',
            noDataMessage: 'No results found.',
            minRowsPerPage: 50,
        }, 'grid');
        grid.startup(); 

      on(document.getElementById("filter"), "click", function(event) {
            event.preventDefault();
            grid.set('collection', store.filter({                   
                **currencyCol: "AED"**
                      .
                      .
                      .
            }));

Dgrid Page Any help would be appreciated or suggest if I use some diffrent store or grid.


Solution

  • I got the solution for my question. On filter button click I have written all my filtering logic and the final store will set to dgrid:

            on(document.getElementById("filter"), "click", function(event) {
    
                var store= new RequestMemory({
                    target: 'getReportData',
                    idProperty: "cashflowId",
                    headers: structure
                });
    
                var from=dijit.byId('from').value;
                var to=dijit.byId('to').value;
                var curr=dijit.byId('currency').value;
                var nos=dijit.byId('nostro').value;
                var authStatus=dijit.byId('authStatus').value;
                var filterStore;
                var finalStore=store;
                var filter= new store.Filter();
                var dateToFindFrom;
                var dateToFindTo;
    
                if (from != "" && from !== null) {
    
                    var yyyy = from.getFullYear().toString();
                    var mm = ((from.getMonth()) + 1).toString(); // getMonth() is zero-based
                    var dd  = from.getDate().toString();
    
                    if(mm <= 9){
                        mm= "0" + mm;
                    }
                    if(dd <= 9){
                        dd= "0" + dd;
                    }
    
                    dateToFindFrom =yyyy + mm + dd;
    
                    filterStore= filter.gte('valueDate', dateToFindFrom);
                    finalStore=finalStore.filter(filterStore);
    
                }
                if (to != "" && to !== null) {
                    var yyyy = to.getFullYear().toString();
                    var mm = ((to.getMonth()) + 1).toString(); // getMonth() is zero-based
                    var dd  = to.getDate().toString();
    
                    if(mm <= 9){
                        mm= "0" + mm;
                    }
                    if(dd <= 9){
                        dd= "0" + dd;
                    }
    
    
                    dateToFindTo =yyyy + mm + dd;
    
                    filterStore= filter.lte('valueDate', dateToFindTo); //.lte('valueDate', dateToFindTo);
                    finalStore=finalStore.filter(filterStore);
                }
    
                if(curr != "" && curr !== null) {
                    filterStore= filter.eq('currency', curr);
                    finalStore=finalStore.filter(filterStore);
                }
                if(nos != "" && nos !== null) {
                    filterStore= filter.eq('nostroAgent',nos);
                    finalStore=finalStore.filter(filterStore);
                }
    
                if(authStatus != "" && authStatus !== null) {
                        if (authStatus=='ALL') {
                            var both= [true, false];
                            filterStore= filter.in('approved', both);
                            finalStore=finalStore.filter(filterStore);
                        } else if (authStatus=='Authorised Only') {
                            filterStore= filter.eq('approved', true);
                            finalStore=finalStore.filter(filterStore);
                        } else if (authStatus=='Unauthorised Only') {
                            filterStore= filter.eq('approved', false);
                            finalStore=finalStore.filter(filterStore);
                        };
                };
                grid.set('collection', finalStore);
    
            });