angularjs - filter value between 2 number - angularjs

i want to ask. I'd already search about angularjs filter but havent found yet what is the solution for mine. I use angular for make android app with IONIC framework.
I have data with price. I want to filter it by price range.
ex:
name type price
item01 fruit 25000
item02 vegetable 50000
item03 vegetable 10000
I have 2 select box for filtering
<select ng-value="fprice">
<option value="">All</option>
<option value="0">0-24999</option>
<option value="25000">25000-49999</option>
</select>
<select ng-value="ftype">
<option value="">All</option>
<option value="fruit">Fruit</option>
<option value="vegetable">Vegetable</option>
</select>
I want if i select the second value (price) then item02 didnt show.
I have code like
<ion-list>
<ion-item ng-repeat="item in items | filter: { type: ftype, price: ??? }">
{{item.name}}<br>
{{item.type}}<br>
{{item.price}}
</ion-item>
</ion-list>
What is the code for my ??? in my code? I have try some but still dont find what I looking for.
Thank you. Sorry for bad english.

You can create custom filter. I post example on jsfiddle
In filter you can put what you want.
my custom filter
autoDrops.filter('filterCostum', function () {
return function (datas, fprice, ftype) {
console.log(ftype)
var options = [];
angular.forEach(datas, function (data) {
if(ftype != ""){
if (data.type == ftype) {
if(fprice == ""){
options.push(data);
}
else if(fprice == 0){
if(data.price >= 0 && data.price <=24999)
options.push(data);
}
else if(fprice == 25000){
if(data.price >= 25000 && data.price<=49999)
options.push(data);
}
}
}
else{
console.log(fprice)
if(fprice == ""){
options.push(data);
}
else if(fprice == 0){
if(data.price >= 0 && data.price <=24999)
options.push(data);
}
else if(fprice == 25000){
if(data.price >= 25000 && data.price<=49999)
options.push(data);
}
}
});
return options;
}
})
I call like this
<div ng-app="autoDrops" ng-controller="DropsController">
<select ng-model="fprice">
<option value="">All</option>
<option value="0">0-24999</option>
<option value="25000">25000-49999</option>
</select>
<select ng-model="ftype">
<option value="">All</option>
<option value="fruit">Fruit</option>
<option value="vegetable">Vegetable</option>
</select>
<div>
<div>
<div ng-repeat="elem in datas | filterCostum: fprice: ftype">
{{elem}}
</div>
</div>
<div>
link edited

Related

filter array doesn't work in React component

I wanna sort the sizes of my clothes but when I click to elect the size, no matter what I choose, it stays "All"...Is there anything wrong with my function filterProducts?
function Filter(props) {
return (
<div className="filter">
<div className="filter-result">{props.count} Products</div>
<div className="filter-sort">
Order
<select value={props.sort} onChange={props.sortProducts}>
<option value="latest">Latest</option>
<option value="lowest">Lowest</option>
<option value="highest">Highest</option>
</select>
</div>
<div className="filter-size">
Filter
<select value={props.size} onChange={props.filterProducts}>
<option value="">All</option>
<option value="">XS</option>
<option value="">S</option>
<option value="">M</option>
<option value="">L</option>
<option value="">XL</option>
<option value="">XXL</option>
</select>
</div>
</div>
);
}
My codesand link:https://codesandbox.io/s/redux-shop-cart-forked-tlpek?file=/src/App.js
All the size options were missing unique value props, they were all value="".
Update the value props to match your data:
Filter
<select value={props.size} onChange={props.filterProducts}>
<option value="">All</option>
<option value="XS">XS</option>
<option value="S">S</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
<option value="XXL">XXL</option>
</select>
You also had a typo in filterProducts in App where you reset the state to a product property instead of products. This wasn't allowing the "All" filter value to work.
filterProducts = (event) => {
if (event.target.value === "") { // all
this.setState({
size: event.target.value,
products: data.products // <-- reset products!!
});
} else {
this.setState({
size: event.target.value,
products: data.products.filter(
(product) => product.availableSizes.indexOf(event.target.value) >= 0
)
});
}
};

Select all option in materialize multi select

Bootstrap multisleect has option for select all (for example here). Is this possible in materialize multi select?
Here is a smaller version: https://codepen.io/souvik1809/pen/rvNMyO
HTML
<div class = "row">
<label>Materialize Multi Select</label>
<select multiple class="select_all">
<option value="" disabled selected>Choose your option</option>
<option value = "1">Mango</option>
<option value = "24">Orange</option>
<option value = "3">Apple</option>
<option value = "4">Cucumber</option>
<option value = "5">Litchi</option>
</select>
</div>
JavaScript
$(document).ready(function() {
// $('select').val([1]);
$('select').formSelect();
$('select.select_all').siblings('ul').prepend('<li id=sm_select_all><span>Select All</span></li>');
$('li#sm_select_all').on('click', function () {
var jq_elem = $(this),
jq_elem_span = jq_elem.find('span'),
select_all = jq_elem_span.text() == 'Select All',
set_text = select_all ? 'Select None' : 'Select All';
jq_elem_span.text(set_text);
jq_elem.siblings('li').filter(function() {
return $(this).find('input').prop('checked') != select_all;
}).click();
});
});
You can add them like this:
https://codepen.io/anon/pen/XgEbRL?editors=1010
HTML:
<div class="input-field col s12">
<select multiple>
<option value="" disabled selected>Choose your option</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<label>Materialize Multiple Select</label>
</div>
<a class="btn" onClick="javascript:selectAll()">Select all</a>
<a class="btn" onClick="javascript:selectNone()">Select none</a>
JavaScript:
var activateOption = function(collection, newOption) {
if (newOption) {
collection.find('li.selected').removeClass('selected');
var option = $(newOption);
option.addClass('selected');
}
};
$(document).ready(function() {
$('select').material_select();
$('.test-input > .select-wrapper > .select-dropdown').prepend('<li class="toggle selectall"><span><label></label>Select all</span></li>');
$('.test-input > .select-wrapper > .select-dropdown').prepend('<li style="display:none" class="toggle selectnone"><span><label></label>Select none</span></li>');
$('.test-input > .select-wrapper > .select-dropdown .selectall').on('click', function() {
selectAll();
$('.test-input > .select-wrapper > .select-dropdown .toggle').toggle();
});
$('.test-input > .select-wrapper > .select-dropdown .selectnone').on('click', function() {
selectNone();
$('.test-input > .select-wrapper > .select-dropdown .toggle').toggle();
});
});
function selectNone() {
$('select option:selected').not(':disabled').prop('selected', false);
$('.dropdown-content.multiple-select-dropdown input[type="checkbox"]:checked').not(':disabled').prop('checked', '');
//$('.dropdown-content.multiple-select-dropdown input[type="checkbox"]:checked').not(':disabled').trigger('click');
var values = $('.dropdown-content.multiple-select-dropdown input[type="checkbox"]:disabled').parent().text();
$('input.select-dropdown').val(values);
console.log($('select').val());
};
function selectAll() {
$('select option:not(:disabled)').not(':selected').prop('selected', true);
$('.dropdown-content.multiple-select-dropdown input[type="checkbox"]:not(:checked)').not(':disabled').prop('checked', 'checked');
//$('.dropdown-content.multiple-select-dropdown input[type="checkbox"]:not(:checked)').not(':disabled').trigger('click');
var values = $('.dropdown-content.multiple-select-dropdown input[type="checkbox"]:checked').not(':disabled').parent().map(function() {
return $(this).text();
}).get();
$('input.select-dropdown').val(values.join(', '));
console.log($('select').val());
};

Why does this Angular filter get applied to LIKE ids?

I have a series of 3 selects that remove options that are selected in the other fields.
Plunker
<label>Choice 1</label>
<select ng-model="food.fruitOne"
ng-options="fruit.id as fruit.name for fruit in fruits | filter:food.fruitTwo!=''?{id: '!' + food.fruitTwo}:{} | filter:food.fruitThree!=''? {id: '!' + food.fruitThree}:{}">
<option value=""></option>
</select>
<label>Choice 2</label>
<select ng-model="food.fruitTwo"
ng-options="fruit.id as fruit.name for fruit in fruits | filter: food.fruitOne!=''?{id: '!' + food.fruitOne}:{} | filter: food.fruitThree!=''?{id: '!' + food.fruitThree}:{}">
<option value=""></option>
</select>
<label>Choice 3</label>
<select ng-model="food.fruitThree"
ng-options="fruit.id as fruit.name for fruit in fruits | filter: food.fruitOne!=''?{id: '!' + food.fruitOne}:{} | filter: food.fruitTwo!=''?{id: '!' + food.fruitTwo}:{}">
<option value=""></option>
</select>
Works great except this issue;
It seems to filter like ids. So if 1 is selected, items with 1x id also get filtered from subsequent selects. Same with other similar ids - 2 & 2x.
Clearly I am not being explicit enough. What's missing?
Thanks!
The filter can accept function that could be useful in your case since you have to handle 3 different cases
$scope.filterFruitsA = function(val) {
return val.id !== val.id !== $scope.food.fruitTwo && val.id !== $scope.food.fruitThree
}
$scope.filterFruitsB = function(val) {
return val.id !== $scope.food.fruitOne && val.id !== $scope.food.fruitThree
}
$scope.filterFruitsC = function(val) {
return val.id !== $scope.food.fruitOne && val.id !== $scope.food.fruitTwo
}
Link to working plunker https://plnkr.co/edit/gzuhYYX2P1Wd0OtfLgOU?p=preview
I'm sure it could be done in a clever way but at that time of the day I can't think of anything, I'll try and revisit this question tomorrow

AngularJS filter by price

Im trying to filter by price, i got a code that its not working properly and i cant seen to know why.
app.js
app.filter('price', function () {
return function ( product , value ) {
// console.log(product, value);
var filteredItems = [];
angular.forEach(product , function ( product ) {
if ( product.price >= value ) {
filteredItems.push(product);
console.log(product);
}
});
return filteredItems;
}
})
the input
<select ng-model="greaterThan" name="price-min">
<option value="">Desde</option>
<option value="500">500</option>
<option value="2000">2000</option>
<option value="5000">5000</option>
<option value="20000">20000</option>
</select>
ng-repeat
<div ng-repeat="product in cars.records | price:greaterThan | filter:query | limitTo:6 " class="mdl-cell mdl-cell--4-col mdl-cell--middle mdl-cell--12-col-phone">
link to the website:
(first input)
http://apcarautomoveis.pt/feed.php

Getting selected item of ng repeat on a datalist

I'm using a array iteration to know which is the selected option. I'm insterested on id atribute of result. is there other way?
<input type="text" list="products" ng-model="query" />
<datalist id="products">
<option value="{{result.name}}" ng-repeat="result in results" >
</datalist>
...
$scope.search = function (query) {
$scope.results.forEach(function (result) {
if (result.name === query) {
// code here
}
}
}
...
Try this:
<datalist id="products">
<option value="{{result.name}}" ng-selected="query.id == result.id" ng-repeat="result in results" >
</datalist>

Resources