Angularjs Directive -Get Element By ID - angularjs

I have tried everything.
Instead of this:
var table = e.target.nextElementSibling
I want to use this:
var table = angular.element(document).find('#tabletarget');
or:
var table = angular.element(document).find('tabletarget');
But it's giving me an error as undefined. How can I select this specific table instead of blindly seeking for the next element?

angular.element(document.getElementByID('tabletarget'));
OR
angular.element(document.querySelector('#tabletarget'));

Related

possible to update a property after new model is created in sailsjs?

I am trying to create a new record for a model and there are fields I need to save later after the model is created.
I have the code like this
let new_model = await Model.create({ name, type}).fetch();
new_model.content = 'abc';
new_model.save();
TypeError: new_model.save is not a function
I googled, it doesn't seem like sailsjs can be done this way so I guess I have to use the update or updateOne by sails. But the thing is, name,type fields are not unique and will not be so using update will actually turn into update for ALL records. updateOne will give me errors saying there's more than one record though
Does anyone has any suggestions on what can be done here?
One other option I can think of is doing something like this with updateOne
let new_model = await Model.create({ name, type}).fetch();
// getting your content here or do something else
const content = 'abc';
new_model = await Model.updateOne({ id:new_model.id}).set({content});

Converting table from HTML to Javascript array in AngularJS

This plunk is my attempt to convert an HTML table to a Javascript array using only AngularJS/jqLite. I can get the head and body from the table, however when I try to get the rows from the body object I get the following error (see the console): tbody.children is not a function. How to fix this?
Javascript
var table = angular.element($scope.t);
var thead = table.children()[0];
var tbody = table.children()[1];
console.log(tbody.children());
I've not used this API before, but a quick check reveals that you lose the angular.element benefits when you pick the children(). You can wrap it again like this to access children method again:
var table = angular.element($scope.t)
var thead = angular.element(table.children()[0])
var tbody = angular.element(table.children()[1])
console.log(tbody.children());

How to Fetch a set of Specific Keys in Firebase?

Say I'd like to fetch only items that contains keys: "-Ju2-oZ8sJIES8_shkTv", "-Ju2-zGVMuX9tMGfySko", and "-Ju202XUwybotkDPloeo".
var items = new Firebase("https://hello-cambodia.firebaseio.com/items");
items.orderByKey().equalTo("-Ju2-gVQbXNgxMlojo-T").once('value', function(snap1){
items.orderByKey().equalTo("-Ju2-zGVMuX9tMGfySko").once('value', function(snap2){
items.orderByKey().equalTo("-Ju202XUwybotkDPloeo").once('value', function(snap3){
console.log(snap1.val());
console.log(snap2.val());
console.log(snap3.val());
})
})
});
I don't feel that this is the right way to fetch the items, especially, when I have 1000 keys over to fetch from.
If possible, I really hope for something where I can give a set of array
like
var itemKeys = ["-Ju2-gVQbXNgxMlojo-T","-Ju2-zGVMuX9tMGfySko", "-Ju202XUwybotkDPloeo"];
var items = new Firebase("https://hello-cambodia.firebaseio.com/items");
items.orderByKey().equalTo(itemKeys).once('value', function(snap){
console.log(snap.val());
});
Any suggestions would be appreciated.
Thanks
Doing this:
items.orderByKey().equalTo("-Ju2-gVQbXNgxMlojo-T")
Gives exactly the same result as:
items.child("-Ju2-gVQbXNgxMlojo-T")
But the latter is not only more readable, it will also prevent the need for scanning indexes.
But what you have to answer is why want to select these three items? Is it because they all have the same status? Because they fell into a specific date range? Because the user selected them in a list? As soon as you can identify the reason for selecting these three items, you can look to convert the selection into a query. E.g.
var recentItems = ref.orderByChild("createdTimestamp")
.startAt(Date.now() - 24*60*60*1000)
.endAt(Date.now());
recentItems.on('child_added'...
This query would give you the items of the past day, if you had a field with the timestamp.
You can use Firebase child. For example,
var currFirebaseRoom = new Firebase(yourFirebaseURL)
var userRef = currFirebaseRoom.child('users');
Now you can access this child with
userRef.on('value', function(userSnapshot) {
//your code
}
You generally should not be access things using the Firebase keys. Create a child called data and put all your values there and then you can access them through that child reference.

Angularjs: How to filter selected fields from an object array

Lets say I have an object array like this
peoples = [{name:"Joe",age:21,sex:'M'},
{name:"Smith",age:18,sex:'M'},{name:"Sally",age:25,sex:'F'}];
Some of these fields are irrelevant so I need to remove attribute age and sex from all of above objects.
This is the desired output.
[{name:"Joe"},{name:"Smith"},{name:"Sally"}];
Update:
It would be nice if I can use angularjs inbuilt service like $filter.
Well I don't see the need to remove the unnecessary objects since you only access the fields you want. Otherwise you can create a new array from the one you have created chucking out what you don't want.
var peoples = [{name:"Joe",age:21,sex:'M'},{name:"Smith",age:18,sex:'M'},{name:"Sally",age:25,sex:'F'}];
var names = [];
peoples.forEach(function(entry) {
names.push({name:entry.name});
});
console.log("New Names:",names); //[{name:"Joe"},{name:"Smith"},{name:"Sally"}];
Just do:
for( var i = 0; i < peoples.length; ++i ) {
delete peoples[i].age;
delete peoples[i].sex;
}
delete is the keyword created for this

How to loop through the extjs grid object to get its elements and values

I have created a grid and want to access it when I click on save button in a page.
How can I loop the grid object to get its elements and its values?
If you want to get a particular field from each record:
var data = [];
store.each(function(rec){
data.push(rec.get('field'));
});
Here is the answer to my question:
for (var i = 0; i < yourGrid.getStore().data.length; i++) {
var element = Ext.get(yourGrid.getView().getRow(i));
var record = yourGrid.getStore().getAt(i);
alert(record.data.ID);
}
How do you get the rows from the grid?
var rows = grid.getStore().getRange();
rows will be an Array of Record objects.
In order to get DOM of the row you can use following code :
yourGrid.getNode(yourGrid.getStore().getAt(rowIndex))
or you can use getNode directly but incase of any headerbar it may not work as supposed to.
yourGrid.getNode(rowIndex)
This will give you the table row.

Resources