Put a range as a parameter for quick filter - angularjs

I am working with Angular and I need some help with quick filter in typescript. So, to perform a set quick filter on a grid and I am using value as parameter.
for example:
types : [
{id: 1, value: '3', name: Jake},
{id: 2, value: '6', name: Brandon}]
filters (event){
this. grid API . set Quick Filter(event);
}
so I am using the value from Jake to perform quick filter. However, I want to put a range of number (like 2-9) inside the value instead of only typing 3 or 2; so that, the set quick filter take that range from the value as apply and perform a quick filter
I tried doing like:
{id: 1, value: '2' && '3' && '4' && '5' && '6', name: Jake}
I was expecting set quick filter to take all the values for Jake and filter the grid

Related

In the vis.js library, how do I get the max value in the "id" field?

I'm discovering vis.js, especially the network module.
I need to get the max value of the "id" field of my nodes dataset:
var nodes = new vis.DataSet([ {id: 1, label: "Node 1"}, {id: 2, label: "Node 2"}, {id: 3, label: "Node 3"}]);
The best I've been able to do so far is using a forEach loop:
var max=0;
nodes.forEach(function(el){j=parseInt(el.id);if(j!=NaN && j>max){max=j;}});
console.log("max: ", max);
It seems to me it can't be THE way to do this.
I saw a max(field) method documented in the doc for vis' DataSet (https://visjs.github.io/vis-data/data/dataset.html):
max(field) [Object|null] Find the item with maximum value of specified field. Returns null if no item is found.
But as stupid as i may sound, I just can't get it to work.
I tried :
console.log("max: ", nodes.max('id'));
console.log("max: ", nodes.max(node => node.id));
console.log("max: ", nodes.max(node => node['id']));
How can I simply get the max value of the field 'id' of all entries of a DataSet?
[Edit] The ID's in the example here above are numeric ({id: 1, ...}).
In my case, they were strings ({id: '1', ...}), and exactly that seemed to be the problem.
try this line:
nodes.max('id').id
nodes.max('id') will return the node with the max id value.
After loads of lost time, I finally figured out that max() works perfectly with numeric IDs.
My first thought was then: Reading the doc could have saved me hours...
...But checking https://visjs.github.io/vis-network/docs/network/nodes.html , it explicitely defines ID as a string:
[name:] id [type:] String [default:] undefined [description:] The id of the node. The id is mandatory for nodes and they have to be unique. This should obviously be set per node, not globally.
So beware: it's supposed to be a string, but if it's a string, some features don't work.
If I'm still missing something, I'd be happy to read your comments.

Ruby way of summing an array of objects by field

I have an array of objects that I'd like to group by field1 and sum by field2. An example would be a class product that has a title field and a price field.
In an array of products, I have multiple gloves with different prices, and multiple hats with different prices. I'd like to have an array with distinct titles, that aggregate all the prices under the same title.
There's an obvious solution with iterating over the array and using a hash, but I was wondering if there was a "ruby way" of doing something like this? I've seen a lot of examples where Ruby has some unique functionality that applies well to certain scenarios and being a Ruby newbie I'm curious about this.
Thanks
There's a method transform_values added in ruby 2.4 or if you require 'active_support/all', with this you can do something like so:
products = [
{type: "hat", price: 1, name: "fedora"},
{type: "hat", price: 2, name: "sombrero"},
{type: "glove", price: 3, name: "mitten"},
{type: "glove", price: 4, name: "wool"}
]
result = products
.group_by { |product| product[:type] }
.transform_values { |vals| vals.sum { |val| val[:price] } }
# => {"hat"=>3, "glove"=>7}
It's a little unclear to me from the question as asked what your data looks like, so I ended up with this:
Product = Struct.new(:title, :price)
products = [
Product.new("big hat", 1),
Product.new("big hat", 2),
Product.new("small hat", 3),
Product.new("small hat", 4),
Product.new("mens glove", 8),
Product.new("mens glove", 9),
Product.new("kids glove", 1),
Product.new("kids glove", 2)
]
Given that data, this is how I'd go about building a data structure which contains the sum of all the prices for a given title:
sum_by_title = products.inject({}) do |sums, product|
if sums[product.title]
sums[product.title] += product.price
else
sums[product.title] = product.price
end
sums
end
This produces:
{"big hat"=>3, "small hat"=>7, "mens glove"=>17, "kids glove"=>3}
To explain:
Ruby inject takes an initial value and passes that to the iteration block as a "memo". Here, {} is the initial value. The return value from the block is passed into the next iteration as the memo.
The product.title is used as a hash key and the running sum is stored in the hash value. An if statement is necessary because the first time a product title is encountered, the stored value for that title is nil and cannot be incremented.
I probably wouldn't ship this code due to the hidden magic of the hash default value constructor but it's possible to write the same code without the if statement:
sum_by_title = products.inject(Hash.new { 0 }) do |sums, product|
sums[product.title] += product.price
sums
end
Hope you enjoy Ruby as much as I do!

Angular - convert expression value based of set values

I am trying to change the value of an expression based off an object of options
Currently I have an expression {{ resultItem.OdometerUnit }} which returns a total of 3 variations depending on result type
hours
kilometers
miles
I want to first check whats returned to expression, and then convert the value to a shorthand version based off the below values
hrs
kms
ms
I am thinking to do this using an object as follows:
$scope.odometerUnitsTranslate = [
{ name: 'Kilometers', trans:'Kms' },
{ name: 'Hours', trans:'Hrs' },
{ name: 'Miles', trans:'Ms' }
];
Beginner with angular so not sure best approach. Any help appreciated
Thanks
JS
$scope.odometerUnitsTranslate =
{'Kilometers':'Kms',
'Hours': 'Hrs' ,
'Miles': 'Ms'
};
HTML
{{odometerUnitsTranslate[unit]}}

How to return current value of ng-model for requirement conditional

I have a select box that is populated using ng-options, and I need to conditionally change the requirement of certain fields based on what is selected in the select box. My function to check if the value is in an array:
$scope.getBTypeSelected = function () {
return $scope.formData.businessType;
};
// Business Type conditionals
$scope.isRequired = function() {
var notRequired = ['2', '5', '6', '7', '12', '80'];
if ($.inArray($scope.getBTypeSelected(), notRequired) == -1) {
return false;
}else {
return true;
}
};
I've tried $watch-ing the scope variable for changes and updating it, and I've tried retrieving the current value via a function to no avail so far.
I understand that the conditionals are getting the initial value of that variable, but I don't understand why I am unable to update it.
Here is a plunker demonstrating my issue: http://plnkr.co/edit/FKFvDu2RMjDKRva6JYwi?p=preview
Any help is appreciated!
You are comparing strings to integers. Your array contains strings, but your selected item is an integer, so inArray is always returning false. I changed the array to integers and its working as intended.
var notRequired = [2, 5, 6, 7, 12, 80];

Display Description from Array using id

I have the following Struct
manufacturers = [
{id: 1, name: 'Apple'},
{id: 2, name: 'HP'},
{id: 3, name: 'Microsoft'},
]
and
product.manufacturerId = 2;
Now i use the following to display the Manufacturer
<p ng-repeat="m in manufacturers | filter: {id: product.manufacturerId}">{{m.name}}</p>
I find this way a littlebit complicated. Isn't there an easier way?
I'm using Couchbase, so I cannot make a SQL JOIN. It needs to be joined with Javascript
You can use indexOf() to get the corresponding item:
$scope.itemToShow = manufacturers.indexOf(product.manufacturerId);
then:
<p>{{itemToShow.name}}</p>
You just need to start your indexes at 0 instead of 1.

Resources