PDA

View Full Version : FilterBy function won't apply. it works great, but store doesnt't change after.



kevinv1990
2 Dec 2010, 1:34 AM
So i have a datastore, which is filled by json file. Now i want to use the filterBy method to only show location within the radius defined by the user. So i have datastore1:




// Datastore1
var datastore1 = new Ext.data.Store({
allowFunctions: true,
model: 'Vacatures',
sorters: {
direction: "DESC", // zorgen dat nieuwste eerst komen
property: "datum"
},
getGroupString: function(record) {
var ophalenDatum = record.get('datum');
var jobDate = new Date(ophalenDatum);
var difDate = new Date(currentDate - jobDate);
var difDay = difDate.getDate()-1;

var dayNumber = jobDate.format('d');
var yearNumber = jobDate.format('y');
var monthName = getMonthName(jobDate);

if(difDay == 0) {
return "Vandaag" ;
} else if(difDay == 1) {
return "Gisteren ";
} else if(difDay == 2) {
return dayNumber + " " + monthName + " 20" + yearNumber;
} else if(difDay == 3) {
return dayNumber + " " + monthName + " 20" + yearNumber;
} else if(difDay == 4){
return dayNumber + " " + monthName + " 20" + yearNumber;
} else if(difDay == 5){
return dayNumber + " " + monthName + " 20" + yearNumber;
} else if(difDay == 6){
return dayNumber + " " + monthName + " 20" + yearNumber;
} else if(difDay <= 14){
return "Een week of ouder";
} else if(difDay <= 21){
return "Twee weken of ouder";
} else if(difDay <= 31){
return "Drie weken of ouder";
} else {
return "Ouder dan een maand";
}

},
proxy: {
type: 'ajax',
url: 'js/test.json',
reader: {
type: 'json',
idProperty: 'id',
root: 'rows'
}
},
storeID: 'datastore1',
autoLoad: true
});


Filter by function :



datastore1.filterBy(function(record, id){
// Get Lat and Lng from the Job
var latitude = record.get('lat');
var longitude = record.get('lng');

var loc1 = new google.maps.LatLng(latitude, longitude)
var loc2 = new google.maps.LatLng(compareLat, compareLng)

var distance = distHaversine(loc1, loc2);
console.log("Distance: " + distance);
console.log("Formdistance: " + formdistance);
if(distance <= formdistance) {
console.log('true');
return true;
} else {
console.log('false');
return false;
}
});

datastore1.sort('datum', 'DESC');

console.log(datastore1);


So the filter return true or false, that works great. but in the console log, it just show me all data and not only the returned true one's.

Kevin

kevinv1990
2 Dec 2010, 2:34 AM
datastore1.each(function(record){
console.dir(record);
});
console.log('clear filter');
datastore1.clearFilter();

datastore1.each(function(record){
console.dir(record);
});

first one Log 5 objects and second 20 (all) objects..

So where it goes wrong with the list ? it should show only 5 objects but doesn't.

kevinv1990
2 Dec 2010, 4:28 AM
A workaround, not perfect but I use it till someone has the answer..


var arrayData = new Array();
datastore1.each(function(record, id) {
arrayData.push(record.data);
});
console.log(arrayData);

Than create new Store. put : data: arrayData,


Kevin