Search inside the List<Map> in flutter dart? - list

I want to filter the List by searching in the search bar but it does not work.
for (var map in offersList) {
if (map.containsKey("foodname")) {
if (map["foodname"] == query) {
setState(() {
offersList.clear();
offersList.add(map);
});
}
}
}

Use a forEach
offersList.forEach((map){
if (map.containsKey("foodname")) {
if (map["foodname"] == query) {
setState(() {
offersList.clear();
offersList.add(map);
});
}
}
});

Related

How to extract certain value from a collection of ember data?

Code below is my current solution,
tagsValue: Ember.computed('tags.#each', {
get() {
const out = [];
this.get('tags').forEach((tag) => {
out.push(tag.get('value'));
});
return out;
}
}),
Is there a better way to do so?
Try code below,
tagsValue: Ember.computed('tags.#each', {
get() {
return this.get('tags').mapBy('value');
}
}),

Is there a shorter way to mock an object in jasmine?

I have an angular filter function that relies on a $window function that has a very big namespace:
.filter('dbNonUtcToDate', function ($window) { //for fields like birthdate in EmployeeData which are not UTC
return function (val, defaultVal) {
if (val !== null) {
var mdate = $window.moment($.jsonDateToDate(val)).format($window.Fr.Alpha.Rep.Web.context.dateFormat().toUpperCase());
var nullMDate = $window.moment([1, 0, 1]).format($window.Fr.Alpha.Rep.Web.context.dateFormat().toUpperCase());
if (mdate !== nullMDate) {
return mdate;
}
}
return defaultVal !== undefined ? defaultVal : '';
};
})
Is there a way to mock the $window.Fr.Alpha.Rep.Web.context.dateFormat() without doing something like:
beforeEach(module(function ($provide) {
var fakeWindow = {
Fr:
{
Alpha:
{
Rep:
{
Web:
{
context:
{
dateFormat: function () {
return ...;
}
}
}
}
}
}
}
$provide.value('$window', fakeWindow);
}));

ember removeObserver and addObserver

I have an array property named 'checked' that I'm observing and I want to temporarily disable observation while I update the property. What is the correct syntax for removeObserver and addObserver?
applyPreReqs: function() {
var currentContent = this.get('content');
currentContent.entitlements.forEach(function(entitlement) {
if (entitlement.checked && entitlement.prerequisiteEntitlementIds !== undefined) {
entitlement.prerequisiteEntitlementIds.forEach(function(prereq) {
currentContent.entitlements.forEach(function(item) {
if (item.id === prereq && !item.checked) {
Ember.set(item, 'checked', true);
}
});
});
} else {
currentContent.entitlements.forEach(function(item) {
if (item.id !== entitlement.id && item.checked) {
Ember.removeObserver('content.entitlements.#each.checked', this, this.applyPreReqs);
Ember.set(item, 'checked', false);
Ember.addObserver('content.entitlements.#each.checked', this, this.applyPreReqs);
}
});
}
}.observes('content.entitlements.#each.checked')
Thank you!
I don't all that you are doing here but this is how you remove the array observer and then make your updates then add the observe back as the observe is fired.
applyPreReqs: function() {
var currentContent = this.get('content');
currentContent.entitlements.forEach(function(entitlement) {
....
}
currentContent.entitlements.removeArrayObserver(this);
}.observesBefore('content.entitlements.#each.checked')
applyPreReqs: function() {
var currentContent = this.get('content');
currentContent.entitlements.forEach(function(entitlement) {
....
}
currentContent.entitlements.addArrayObserver(this);
}.observes('content.entitlements.#each.checked')
I hope this helps
Cheers

Is there a more efficient way to filter through a collection with computed properties?

In short, I am creating a table that is sortable and is filterable by whether the item is new, updated or needs to be deleted. The FilteredParts property needs to observe both the query, sortBy and model.#each.isDirty in order to ensure the table is up to date. Changing filters and sorting are quick however if I want to update any model in the collection it very slow. Is there a way to use model.#each.isDirty or something similar so that it is not so taxing?
Here is my controller:
App.VendorPartsController = Ember.ArrayController.extend(App.PartTableMixin,{
queryParams: ['query','sortBy'],
query: "all",
hasChanges: function(){
if(this.get('filteredParts').isAny('isDirty',true))
{
return true;
}
return false;
}.property('filteredParts.#each.isDirty'),
hasNew: function() {
if(this.get('filteredParts').isAny('isNew',true))
{
return true;
}else{
return false;
}
}.property('filteredParts.#each.isNew'),
hasUpdated: function() {
var parts = this.get('filteredParts');
return parts.any(function(part){
return part.get('isDirty') && !(part.get('isNew')) && !(part.get('deletable'));
});
}.property('filteredParts.#each.isDirty'),
hasDeleted: function() {
if(this.get('filteredParts').isAny('deletable',true))
{
return true;
}
else
{
return false;
}
}.property('filteredParts.#each.deletable'),
filteredParts: function(){
var parts = this.get('model');
var query = this.get('query');
var sortBy = this.get('sortBy');
switch(query){
case "new":
return parts.filterProperty('isNew',true).sortBy(sortBy);
break;
case "updated":
return parts.filter(function(part){
return part.get('isDirty') && !(part.get('isNew')) && !(part.get('deletable'));
}).sortBy(sortBy);
break;
case "deleted":
return parts.filterProperty('deletable',true).sortBy(sortBy);
break;
case "all":
return parts.sortBy(sortBy);
break;
}
}.property('query','sortBy','model.#each.isDirty'),
actions: {
undoAll: function() {
this.get('model').forEach(function(part){
part.rollback();
})
this.transitionToRoute('vendor.parts');
}
}
})
Here is a mixin that I will probably add back into the controller:
App.PartTableMixin = Ember.Mixin.create({
sortAscending: false,
sortBy: 'vendor_part_number',
outOfParts: function()
{
if(this.get('filteredParts').length < 1)
{
return true;
}else
{
return false;
}
}.property('filteredParts.length'),
actions: {
trash: function(model){
if(model.get('isNew'))
{
model.rollback();
}
else
{
model.set('deletable',true);
}
},
undo: function(model) {
model.rollback();
}
}
});

How to filter a list in Sencha Touch 2?

I'm filtering a list of stocks by symbol, but it doesn't work. Maybe there's something wrong with the code? Something I have missed? Here are some of things I've tried:
function filterBySymbol: function(select, value) {
var ordersStore = this.getBrokerageOrderHistoryList().getStore();
ordersStore.clearFilter();
if (value !== '') {
ordersStore.data.filterBy(function (record, id) {
// log to make certain this gets called (and it is)
console.log(id, record.get('symbol') === value);
return record.get('symbol') === value;
});
// Other things I've tried (nothing worked):
// 1)
// var f = new Ext.util.Filter({
// filterFn: function(record) {
// return record.get('symbol') === value;
// }
// });
// ordersStore.filterBy(f);
// 2)
// ordersStore.filter(function (record) {
// return record.get('symbol') === value;
// });
// 3)
// this.getBrokerageOrderHistoryList().setStore(ordersStore);
// this.getBrokerageOrderHistoryList().refresh();
}
}
It turns out we had to disable remote filtering on the store, which is supposed to default to false, but it wasn't:
this.getOrderList().getStore().setRemoteFilter(false);
one of these should work
// 1
ordersStore.filter("symbol", value);
// 2
ordersStore.filter([
{ filterFn: function(item) { return item.get("symbol") === value; }}
]);
// 3
ordersStore.filterBy(function(item) {
return item.get("symbol") === value;
}
UPDATE: sample that works:
Ext.define('ST.store.Products', {
extend: 'Ext.data.Store',
config: {
fields: ["title", "category" ],
storeId: "Products",
data: [
{ title: 'Text1', category: 'cat1'},
{ title: 'Text2', category: 'cat2'},
{ title: 'Text3', category: 'cat3'},
]
}
});
console.log("before");
Ext.getStore("Products").each(function(item){
console.log(item.data.title);
});
Ext.getStore("Products").filterBy(function(item){
return item.get('title') == 'Text1';
});
console.log("after");
var store = Ext.getStore("Products").each(function(item){
console.log(item.data.title);
});
in my case I see the following in the developer console
before
Text1
Text2
Text3
after
Text1