Multi Column Sorting Using Ember JS - javascript

My requirement is to sort array using Dynamic property names using EmberJS.
What i have done previously for single column sorting is
ents = #get('acceptedEntities') //get the array
#set('sortAscending', !#get('sortAscending'))
sort_data = ents.sortBy(property_name) //property name is sort order
And what i am looking id
ents = #get('acceptedEntities')
#set('sortAscending', !#get('sortAscending'))
sort_data = ents.sortBy([property_name1, property_name2])
I tried with the above solution but no luck and i here about computed sort
and implemented like this
model = #get('acceptedEntities')
sortProperties = [property_name, 'entity_sf_account_name']
sort_data = Ember.computed.sort(model, sortProperties)
But Sorting is not going properly, Please give me suggestions to do this.
i tried this too
sortProperties = ['one:asc', 'two:desc', 'three:asc']
sort_data = Ember.ArrayProxy.createWithMixins(Ember.SortableMixin, {
content: model,
sortProperties: sortProperties
})
the above code works fine for sorting with multiple parameter but when i want sort order its not working properly
Thanks

There are many ways to do this, but I would do it like this:
sortProperties = ['one:asc', 'two:desc', 'three:asc']
sort_data = Ember.ArrayProxy.createWithMixins(Ember.SortableMixin, {
content: model,
sortProperties: sortProperties
})

Related

Can I get the list of Caman.js filters?

Is there any way to return the list of all the built-in filters within the library.
For example:
var caman_Default_List = [];
Caman('myCanvas', function(){
caman_Default_List= this.getAllFilters();
});
For now I'm using this and it works okay:
var filters =
[
"vintage", "lomo", "clarity", "sinCity", "sunrise",
"crossProcess", "orangePeel", "love", "grungy", "jarques", "pinhole",
"oldBoot", "glowingSun", "hazyDays", "herMajesty", "nostalgia",
"hemingway", "concentrate"
];
myList.push(filters[ some filters ]);
Caman("#myCanvas", function(){
this[myList[index]]().render();
});
But I was wondering if there is a way to get the values of the filters without delcaring them customly. (eg. list = [ "vintage", "lomo", ......... ])
I was looking through their docs, but could not find anything helpful for the data you are trying to get. I took a look at their code and came up with the below for you.
I am not sure I would 100% trust the code, because the order of properties might change, but at least it gives you what you wanted.
console.log(Object.keys(Caman.prototype).slice(75, 93))
<script src="https://cdnjs.cloudflare.com/ajax/libs/camanjs/4.1.2/caman.full.min.js"></script>
I used the code below to achieve what I wanted as #AndrewLohr stated:
//Declare lists to store filter names
var list4,list5,list6,list7 = [];
//Get Caman Filters (Samples : "vintage", "clarity", ... )
list4 = (Object.keys(Caman.prototype).slice(75, 93));
list5 = list4.toString().toUpperCase(); //To upper Case as string value (use as Label Button Name)
//Get Caman Filters (Customs : "brightness", "saturation")
list6 = Object.keys(Caman.prototype).slice(45, 55);
//Add some more elements in the list
list6.push("clip", "stuckBlur", "exposure", "noise", "sharpen");
list7 = list6.toString().toUpperCase(); //To upper Case as string value (use as Slider Name)
//Print lists
console.log(list4);console.log(list5);console.log(list6);console.log(list7);
<script src="https://cdnjs.cloudflare.com/ajax/libs/camanjs/4.1.2/caman.full.min.js"></script>

Add element to multi row array with angular / javascript

I have the following array with data:
[
{"count":1,"title":"Ark: Survival","platform":"Playstation PS4"},
{"count":2,"title":"Lara Croft:", "platform":"Playstation PS4"},
{"count":1,"title":"Madden NFL", "platform":"Playstation PS4"}
]
All in my angular Cart scope:
var data = $scope.cart;
now i would like to add to each row the order number, so i use a push:
$scope.cart.push({"orderNumber": $scope.OrderNumber});
the problem is that is adds the order number but as a separate row and not inside each of the rows. so my desired output should look like this:
[
{"count":1,"title":"Ark: Survival","platform":"Playstation PS4","orderNumber":1 },
{"count":2,"title":"Lara Croft:", "platform":"Playstation PS4","orderNumber":1},
{"count":1,"title":"Madden NFL", "platform":"Playstation PS4","orderNumber":1}
]
how would i push to each row of the array? i tried several versions but can't seem to get it to work.
in order to achieve that you should iterate through each object and set order number separately
angular.forEach($scope.cart, function (value, key) {
value.orderNumber = $scope.OrderNumber;
});
You need to iterate over each item and add the orderNumber as a property for each item. So you can use forEach function of the angular for that.
angular.forEach($scope.cart, (item) => item.orderNumber = $scope.OrderNumber)
just got an answer that i implemented but the user has deleted the response, however it works perfect, this was the solution that i used:
$scope.cart.forEach(item => item.orderNumber = $scope.OrderNumber)

ng-repeats, showing record while it's value is a part of field of another object

I've got objects 'ing' with a field named 'id' and another one called 'fObj' with a field named 'contain'.
By using ng-repeat i'd like to show only these 'ing' objects where ing.id is a part of fObj.contain
e.g.
ing=[{id: 1,field: value},{id:2, field: othervalue},{id:3, field: cat}];
fObj={field1: value1, field: value2, contain: ':1:3:'};
By having this contain value I'd like to show only ing's with id=1 and id=3
Yeah, I know there are two types of data (number and string) but even if i changed numbers to strings it still didn't work
I just dont't know how to make it works. It's probably some kind of custom filter, but I've tried couples and nothing happend.
I would be glad if you suggest me a solution.
Thanks
In your controller,
var ids = fObj.contain.split(':');
// the array for your ng-repeat
var displayIng = [];
// loop the objects, see if the id exists in the list of id's
// retrieved from the split
for(i = 0; i < ing.length; i++) {
if(ids.indexOf(ing.id.toString()) displayIng.push(ing[i]);
}
I would split the numbers out of fObj.contain; and use them as hashmap object keys for simple filtering of the array
var ing=[{id: 1},{id:2},{id:3}];
var fObj={contain: ':1:3:'};
var IDs = fObj.contain.split(':').reduce(function(a,c){
a[c]=true;
return a;
},{});
// produces {1:true,3:true}
var filtered = ing.filter(function(item){
return IDs[item.id];
});
console.log(filtered)

How to replace ' / ' with '-' inside ObservableArray ? Knockout js

Well i am trying to pass an observable array via ajax call to my controller but i get every value there except date . i get something like '01-01-01' etc .
I found the issue but unable to fix that as i dont know how to replace / with - .
My ObservableArray have around 10 list items each list item holds a many properties out of those startDate holds the date like ("23/10/2014") . i just need something like ("23-10-2014") .
Tought of posting my function's and more i hope thats not required in this case i believe .
Let me explain with bit of code and sample data :
function myarray()
{
var self=this;
self.startDate=ko.observable("");
self.name=ko.observable("");
self.place=ko.observable("");
}
MyObservableArray :
self.Main= ko.observableArray();
In between i do some stuff to load Data into self.Main and i am sending self.Main to controller having data like below :
self.Main[0] holds :
startDate() -->gives you "23/10/2014" //individual observables inside onservable array
name() --> "jhon"
place()--> "croatia"
Likely
self.Main[9] holds :
startDate() --> "29/05/2012"
name() --> "pop"
place()--> "usa"
I am trying like i want to alter the self.Main() and replace the startDate and use the same self.Main to send to my controller . Once after replacing in self.Main when i check date the / should be replaced with - .
Possible solution : i can use a different observable array and push all the VM's of Main into it but i am trying to do on self.Main without using other .
If someone can show some light it is much appreciated .
What I got that you are facing problem in escaping / in replace.
Try this
"(23/10/2014)".replace(/\//g,"-") //returns "(23-10-2014)"
I tried something for you using simple JS
var arr = [{date:"(23/10/2014)"},{date:"(23/10/2014)"},{date:"(23/10/2014)"},{date:"(23/10/2014)"}];
arr.forEach(function(obj){obj.date = obj.date.replace(/\//g,"-")});
console.log(arr) //will print date field as "(23-10-2014)" for all objects.
One solution would be to add a computed value that returns the array with the right values.
self.Main = ko.observableArray([...values here...]);
self.MainComputed = ko.computed(function() {
var computedArray = [];
self.Main().forEach(function(item) {
var newItem = myarray(); //Create a new item.
newItem.name(item.name());
newItem.place(item.place());
newItem.startDate(item.startDate().replace(/\//g,"-"));
computedArray.push(newItem);
});
return computedArray;
});
Then use the computed value in the places where you need the values with -.
I can think of two other ways to solve your issue, when taken into account that you want to use self.Main:
Replace the / with - before setting startDate on your item.
Change startDate to a computed value while storing the original value in another variable.
The first solution should be pretty straight forward (provided that it is a valid solution).
The second solution would look something like this:
function myarray()
{
var self=this;
self.originalStartDate = ko.observable("");
self.name = ko.observable("");
self.place = ko.observable("");
self.startDate = ko.computed(function() {
if(self.originalStartDate()) {
//We can only replace if the value is set.
return self.originalStartDate().replace(/\//g,"-");
}
else {
//If replace was not possible, we return the value as is.
return self.originalStartDate();
}
});
}
Now when you set the values you do something like:
var item = myarray();
item.originalStartDate = "01/01/2014";
Then when you get the value of startDate you would get "01-01-2014".
I haven't used Knockout.js but you can do this with a Javascript replace:
var str = [your array value] ;
var res = str.replace("/", "-");
For more information:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

How to build an associative array with jQuery/javascript?

I have a large array in this format:
var cars = [{"brand":"Honda","year":"2002"},{"brand":"Toyota","year":"2000"},{"brand":"Subaru","year":"2009"}];
The array is very large (I made it small for demo purpose) and I want to organize cars of the same brand into an array, so the brand name will be the key for each sub-array, like this:
carsByBrand[ honda[], toyota[], subaru[] ]
I expected it would be straight forward but it's not so. I've tried several variations of the following code, but everytime it returns:
carsByBrand[item.brand] is undefined
var carsByBrand = [];
$.each(cars, function(i,item){
carsByBrand[item.brand].push(item);
});
//console.debug(carsByBrand); //uncomment and look at this if you have Firebug
alert(carsByBrand.length);
I've also tried carsByBrand[item['brand']], why doesn't this work, and how to do it?
jsBin: http://jsbin.com/orafos
carsByBrand[item.brand].push(item);
That will only work if the array for that brand already exists, which it does not unless you initialize it first with an empty array.
Try
brand = carsByBrand[item.brand];
if (brand) {
brand.push(item);
else {
carsByBrand[item.brand] = [item];
}

Categories

Resources