Getting a particular row's attribute value? - javascript

I have this function which I believe is following this process:
function verify(){
$.get("map_process.php", function (data) {
verified = $(data).find("marker").eq(-1).attr('verification');
});
}
Get data from php file/db
In the db, find the table "marker"
Find the last record in the table marker
Assign the value of the 'verification' column to the variable verified
This is doing what I want (kind of) but I need to be able to specify what record to get the 'verification' value from, but not by it's position in the table (as more records will be added and the above will just get the last record regardless). Is there another method that is kind of like .eq(x) but will allow me to specifically select a record based on another attr in that record.
eg. Say I want to find the verification value for record 1 through an event listener, and then find the verification value for record 6 through a different event listener.
I have a variable which can distinguish what row I want to get, but how can I incorporate this into the statement above. (i'm thinking instead of .eq(-1)

You can use filter() which can contain as many conditions as you need.
$.get("map_process.php", function (data) {
var myVariable = $(data).find("marker").filter(function(elementIndex, element){
return $(this).attr('someOtherAttribute') === 'valueWanted';
}).attr('verification');
});
Since I'm really not sure what the data looks like or what attribute you need the above is only a guess at how you would need to implement
See filter() API docs

Related

How to increment a map value in a Firestore array

I have a firestore firebase database , in which I have a collection users
there is an array in the collection and in the array there is a map
in map there is a field qty.. I want to increment that qty value..
using increment doesnt help as the qty is inside a array index
db.collection("users").doc(checkId).update({
myCart: firebase.firestore.FieldValue.arrayUnion({
qty: firebase.firestore.FieldValue.increment(1),
}),
this is the error Output =>
Uncaught (in promise) FirebaseError: Function FieldValue.arrayUnion() called with invalid data. FieldValue.increment() can only be used with update() and set()
My answer below won't work, given that the qty is in an array. The only way to update an item in an array is to read the entire document, update the item in the array, and then write the entire array with the updated item back to the document.
An alternative would be to use a map instead of an array, and then update the qty using the approach outlined in my (old, and non-working) answer below 👇
You need to specify the full path to the field you're trying to update. So I think in your case, that'll be:
db.collection("users").doc(checkId).update({
"myCart.0.qty": firebase.firestore.FieldValue.increment(1)
}),
The field you want to update is embedded in an array. In this case, you can't use FieldValue.increment(), since it's not possible to call out an array element as a named field value.
What you'll have to do instead is read the entire document, modify the field in memory to contain what you want, and update the field back into the document. Also consider using a transaction for this if you need to update to be atomic.
(If the field wasn't part of an array, you could use FieldValue.increment().)
As of today (29-04-2020)... this is tested by me.
Suppose my data structure is like this:
collection: Users
Any document: say jdfhjksdhfw
It has a map like below
map name: UserPageVisits
map fields: field1,field2,field3 etc
Now we can increment the number field in the map like below:
mapname.field1 etc...
That is use the dot operator to access the fields inside the map just like you would do to an object of javascript.
JAVA Code (Android), update the field using transactions so they can complete atomically.
transaction.update(<documentreference object>,"UserPageVisits.field1",FieldValue.increment(1));
I have just pushed a version of my app which uses this concept and it's working.
Kudos !!
My Best Regards
Previous answers helped me as well, but dont forget about the "merge" property!!! Otherwise it will overwrite your entire array, losing other fields.
var myIndex = 0;
const userRef = db.collection('users').doc(checkId);
return userRef.update({
'myCart.${myIndex}.qty': admin.firestore.FieldValue.increment(1)
}, {
merge: true
});

Setting ngModel using ng-clicked

Is there some reason when I save the input from an Angular form of checkboxes that I cannot get ng-model on the controller to become defined when I set the values on the checkboxes with ng-checked? Basically, I need to save the values in an array and if a user returns to the page it should set the form to the values stored in the array. Here is a plnkr of my code:
http://plnkr.co/edit/l1tIWwaYWsyKou64VjrO
Here is the function that ng-checked calls when the user returns to the page:
function getChecked(id) {
if (businessApp.serviceTypes.indexOf(id) > -1)
{
return true;
}
}
The list of services is stored in the array businessApp.serviceTypes.
You can do something like this. Basically it stores the checks (in the order they are displayed on the page). I added a toggle to ng-click to change the value stored in the array. I would recommend you modify your model to accommodate a more convenient way of storing the checks though, because right now your labels are sorted, so they come out a different order in the view than they are stored in the model.
Just added this to the controller:
self.checks = {
vals: [false,false,false,false,false,false,false,false,false]
}
HTML I changed/added this:
ng-checked="vm.checks.vals[$index]"
ng-model="vm.serviceType[$index]"
Demo

SuiteScript: How does the dynamic mode of nlobjColumn.setURL work?

In NetSuite, I have a scripted search of transactions that is expected to return results of several different transaction types. The results are then rendered in an nlobjList. I would like one of the columns of said list to be a link to the transaction that the list row represents.
In all NetSuite examples, this is accomplished something like:
var column = list.addColumn('number', 'text', 'Number', 'left');
column.setURL(nlapiResolveURL('RECORD','salesorder'));
column.addParamToURL('id','id', true);
Unfortunately, transaction is not an acceptable record type to pass to nlapiResolveURL, so I would need to dynamically detect the record type for each row. The setURL function does accept a second Boolean parameter that makes it dynamic per row, but I am not sure how this actually works. There are no examples, and the JSDocs do not explain its usage.
Does anyone have any guidance on generating a list with dynamic URLs in NetSuite?
If you set the dynamic argument to true, then the first argument should be a column listed in the data source that will contain the base URL.
column.setURL('base_url', true);
column.addParamToURL('id','id', true);
Then, on each record of your results, make sure you have a base_url that is set to the url you are looking for.
Note, the following example assumes a regular javascript object instead of the search result object.
rec.base_url = nlapiResolveURL('RECORD', rec.type)
Transaction field is just an abstraction for all transaction types. You can search them but can't load them.
The field you need to retrieve is recordtype. Sample code is below.
var recs = nlapiSearchRecord('transaction',null,null,new nlobjSearchColumn('recordtype'));
for(var i in recs)
url = nlapiResolveURL('RECORD',recs[i].getValue('recordtype'));

Javascript array in SQL prepared statement

Here is what I am trying to do. I have a simple piece of java script code which takes the value of a checkbox and deletes the corresponding from the database.. Say that I have three options red, blue, yellow displayed and red is selected, the record for red will be deleted.
This code does it perfectly.
var n = $('input[type=checkbox]:checked').val();
db.transaction(function (tx) {
tx.executeSql('delete from details where name = ?',[n],function(){
alert('The event is successfully deleted');
});
});
But the requirement is to capture multiple checkbox selections as well (i.e) if both red and blue are selected or all the three are selected, the code must deleted the records of the selected values. I am able to capture the values selected in an array with the following piece of code.
var checkboxValues = $('.myCheckbox:checked').map(function() {
return $(this).val();
}).get();
Now i need to use this array of strings in a SQL statement. I tried the following piece of code but of no use.
db.transaction(function (tx) {
tx.executeSql('delete from details where name in (?)',[checkboxValues],function(){
alert('The event is successfully deleted');
});
A few other answers suggested me to convert the array object into a comma-seperated string using toString() and join() function but those did not quite help me. I doubt whether my SQL statement is correct. Pls someone help me. Thanks in advance !!
Maybe you could turn the function around so that it iterates over the returned values and preforms the db function for each in turn. Something along the lines of:
$(checkboxValues).each(function(tx){...
Or you could consider preforming the SQL update as each value is found within the first function, as long as you are throwing security out the window. Then you would only need to include the value as a this and you could lose the variable.

How can I bind the selected text of a Select box to an object's attribute with Knockout JS, or anything else?

I have a select box pull down that I'm populating with a JSON list returned from a stored procedure, but unfortunately when I update the linked object I need to return the selected text of the pulldown, not the selected index like one would think (poor database design, but I'm stuck with it for now and cannot change it).
Does anyone have any ideas what I can do to keep the selected text synced with the appropriate javascript object's attribute?
You could keep both, the value and the text, if you use subscribers.
For instance, if each of your javascript objects look like this:
var optionObject = {
text:"text1"
value: 1
}
Then your binding would look like:
Where 'OptionsObjects' is a collection of optionObject and selectedOption
has two observable properties: text and value.
Finally you subscribe to the value property of the selectedOption:
viewModel.selectedOption.value.subscribe(function(newValue){
var optionText = viewModel.OptionsObjects[newValue].text;
viewModel.selectedOption.text(optionText);
});
Then if you want to see the new selected option text when the value is changed,
you could have a binding as follows:
<span data-bind:"text:selectedOption.text"></span>
In your particular case you would return selectedOption.text().
So yes, you got what I was getting at. Use the text as the value for the select options rather than using an index. The value really should be something useful, I can't think of any case where I've ever used an index. A number sure, but a number that relates to the application's models in some way (like an id from a database), not to the number of items in the select box.
Well done.

Categories

Resources