0

I have a data model as follows in jaydata

Model.js

$data.Entity.extend('Expense', {
  Id: { type: 'int', key: true, computed: true },
  Name: { type: 'string' },
  Date: { type: Date },
  Price: { type: 'number' }  
});

$data.Entity.extend('Regular', {
  Id: { type: 'int', key: true, computed: true },
  Name: { type: 'string' },
  Price: { type: 'number' }  
});

$data.EntityContext.extend("ExpenseDatabase", {
    Expenses: { type: $data.EntitySet, elementType: Expense },
    Regulars: { type: $data.EntitySet, elementType: Regular }
});

I want to filer the results by Date, which i havent been able to. Can someone help me with this regard? My code for filtering is as follows. But its not working, throws up a type error.

var d = new Date();
exDB.Expenses
    .filter( function(exp) {            
        return exp.Date.day() == d.getDate();                    
    })
    .forEach( function(exp) {
        alert(exp.Name + exp.Date.getDate());
        toAdd = '<li><a>'+exp.Name+'<span class="ui-li-count">'+exp.Price+'</span></a></li>';
        $("#expenseList").append(toAdd);
        $("#expenseList").listview("refresh");
    });

1 Answers1

0

The generic solution would be to write a query with two conditions that checks for an interval:

//first second of the current day
var intervalStart = new Date(new Date(date - date % (24 * 60 * 60 * 1000)));
//last second of the current day
var intervalEnd = new Date(intevalStart .valueOf() + 24 * 60 * 60 * 1000);

exDB.Expenses
    .filter( function(exp) {            
        return exp.Date >= this.minDate && exp.Date <= this.maxDate
    }, {minDate: intervalStart , maxDate: intervalEnd })
    .foreach(function(){...});
Community
  • 1
  • 1
Robesz
  • 1,646
  • 11
  • 13
  • Thanks! worked like a charm. Any idea on how to do it for a month? because different months have different number of days. Does jaydata support any day/month functions for indexeddb? – Ninaada Bellippady Sep 05 '13 at 06:28
  • We would be happy to cover this feature for indexeddb, but there is no general solution for this with IndexedBB Cursor API. It could be done only in memory, which is slow...or with some indices, but that is app-specific. – Robesz Sep 05 '13 at 07:55