document.querySelectorAll to find item based on multiiple condition? - domdocument

I am trying to find div based on combination of class name and data attribute. But it doesn't seems to returning any value.
var products = document.querySelector("div.productDisplay").querySelectorAll("[data-productId='" + id + "']");

The first .querySelector() call will only return a single element. If that does not match the selector in the querySelectorAll() call then no elements will be retured.
Try using a single selector, such as:
var products = document.querySelector("div.productDisplay[data-productId='" + id + "']");

Related

Property Metadata Binding Assertion Failed

I have a View in my SAPUI5 application where I am creating a OData Model.
In the next step I want to use his metadata to bind a Property.
My error message is
abap.js:64 Assertion failed: COLUMN/#sap:label is not a valid property path
I think here is another mistake because before i tried this, I had always a OData Model which i defined in the manifest file and it worked fine - but now when creating a Model in the same view it doesn't work.
I also thougt about to set the Model to the View, but I think its not neccessery because in the Path I am saying, please look at "oModel" OData Model for your data. {oModel</....
Did I forgot something?
// creating model
var oModel = new sap.ui.model.odata.v2.ODataModel("/sap/opu/odata/sap/xxxx/", "oModel");
var oLabel = new sap.ui.comp.smartfield.SmartLabel({
text: "{oModel>/#showcase/" + column + "/#sap:label}" // {ODataModel>/#showcase/" + column + "/#sap:label}" //
});
the "oModel" inside the line
text: "{oModel>/#showcase/" + column + "/#sap:label}"
refers to a name, under which you can assign the model to any Control that is inherited from sap.ui.base.ManagedObject. it has no relationship to the variable name "oModel" that you use to create the model in JS.
the following should work (note, I replaced one occurrence of oModel with bla, the other with blubber to point out the difference
var bla = new sap.ui.model.odata.v2.ODataModel("/sap/opu/odata/sap/xxxx/");
var oLabel = new sap.ui.comp.smartfield.SmartLabel({
text: "{blubber>/#showcase/" + column + "/#sap:label}"
});
oLabel.setModel(bla, "blubber")
or, alternatively:
var oLabel = new sap.ui.comp.smartfield.SmartLabel({
models: {
"blubber": new sap.ui.model.odata.v2.ODataModel("/sap/opu/odata/sap/xxxx/")
},
text: "{blubber>/#showcase/" + column + "/#sap:label}"
});

Setting Multiple Variables to Result of Query

I have the query below, that will return the city and state that is the shortest distance between an inputted set of points and all rows of the table Metros:
#lat = row[latitude]
#long = row[longitude]
Metros.select(
"major_city
, major_state
,(latitude - " + #lat + ")**2 + (longitude - " + #long + ")**2 as 'distance'")
.group("major_city,major_state").limit(1).order('distance ASC')
What I need to do now is set the result to two variables row[major_city] and row[major_state] with major_city set to row[major_city] and major_state to row[major_state].
How can I accomplish this task two allow for setting to the two variables independently?
It comes out with a ActiveRecord::Relation type even if it was just one piece of result, so use .first and then take it as a single record may work.

Get column index and sort order while performing sorting using tablesorter?

Am using tablesorter to sort my table html table. Is it possible to get column index and sort order using tablesorter?
Can you share some more details about when you need this information.
I wrote this answer assuming you want the sort information after a sort. In this example, only the first sorted column information is returned, but all sorted columns would be contained within the table.config.sortList variable (demo):
$(function () {
$('table')
.on('sortEnd', function(){
var currentSort = this.config.sortList,
firstColumn = currentSort[0][0],
firstColumnDirection = currentSort[0][1] === 0 ? 'Ascending' : 'Descending';
console.log('column = ' + firstColumn, ', direction = ' + firstColumnDirection);
})
.tablesorter({
theme: 'blue'
});
});

ASP.Net MVC Post: key-value pairs - can't retrieve value

I have a forms collection (fc) and I'm attempting to append to an email the values of the 'key' and the 'value'. I'm having no problem with the key (newKey), but I can't seem to code the 'value' properly. The 'for' loop checks to see if the key's first 3 characters of the key are 'ddl' indicating it came from a dropdownlist. if so, the loop should append the value from the dropdownlist control (the value of the key-value pair). (If not, the loop calls another method to append either a yes or no based upon the value of a checkbox control) Thanks in advance.
//Append new key-value pairs implemented since legacy keys
for (int i = 0; i < newKeys.Length; i++ )
{
//Checks for prefix of element to determine type of element
if(newKeys[i].Substring(0,3) == "ddl"){
sb.Append(newKeys[i] + ": " + fc.GetValue(newKeys[i]) + "\",<br />");
sb.Append(newKeys[i] + ": " + fc.GetValues(newKeys[i].ToString()) + "\",<br />");
}
else{
sb.Append(newKeys[i] + ",\"" + Boolean(fc[newKeys[i]]) + "\",<br />");
}
}
The 2 sb.append commands return the following:
ddlStratacacheConstellationManagerRole: System.Web.Mvc.ValueProviderResult"
ddlStratacacheConstellationManagerRole: System.String[]",
The values you are writing aren't of type String, nor they have the ToString() method overridden.
That's why the standard object.ToString() method is called and the object's type name is appended to the string.
To remedy this, you either need to override the ToString() method of all possible form collection's values, or think of some other algorithm, which would iterate over collections and output the corresponding values.

How to Detect Diferences between GET and POST ViewModel Values in a form?

I have a single view that handles a lot of Models of type VoyagesViewModel, in that view the user can create a new voyage or edit all the active voyages, so i have different instances of the same object type in that view.
each form contains something like:
<% = Html.Hidden("Voyages["+ i +"].VoyageDetails[" + i2 + "].Id", location.Id)%>
<% = Html.TextBox("Voyages[" + i + "].VoyageDetails[" + i2 + "].ArrivalDate", location.ArrivalDate, new { #class = "dates" })%><% = Html.ValidationMessage("Voyages[" + i + "].VoyageDetails[" + i2 + "].ArrivalDate", "*")%>
<% = Html.TextBox("Voyages[" + i + "].VoyageDetails[" + i2 + "].DepartureDate", location.DepartureDate, new { #class = "dates" })%><% = Html.ValidationMessage("Voyages[" + i + "].VoyageDetails[" + i2 + "].DepartureDate", "*")%>
<% = Html.Hidden("Voyages[" + i + "].VoyageDetails[" + i2 + "].LocationID", location.LocationID)%>
<% = Html.Hidden("Voyages[" + i + "].VoyageDetails[" + i2 + "].LocationName", location.LocationName)%>
<% = Html.Hidden("Voyages[" + i + "].VoyageDetails[" + i2 + "].VesselName", location.VesselName)%>
<% = Html.Hidden("Voyages[" + i + "].VoyageDetails[" + i2 + "].VesselID", location.VesselID)%>
<% = Html.CheckBox("Voyages[" + i + "].VoyageDetails[" + i2 + "].remove", location.remove)%> (remove)
I have 8 to 10 of this in that View, when the user POST the form(I have all this fields in a single form), i want to be able to detect if the user change something in a voyage to save it, so i don't have to save that specific voyage if no changes where made.
To check this i have a hidden field and i do this:
<% = Html.Hidden("Voyages[" + i + "].hash", TamperProofing.GetExpiringHMAC(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Model), DateTime.Now.AddMinutes(15)))%>
Im using HMAC to get a hashed version of the hole object, but first i serialize the object into a string format (JSON Format):
{"Id":22,"VesselName":"CAPTAIN P (CPP)","VesselID":8,"VoyageDetails":[{"Id":58,"ArrivalDate":"\/Date(1259298000000)\/","DepartureDate":"\/Date(1259384400000)\/","LocationID":404,"LocationHash":null,"LocationName":"Balboa, Panama (PABLB)","VesselName":"CAPTAIN P (CPP)","VesselID":8,"Order":0,"Comment":null,"remove":false},{"Id":60,"ArrivalDate":"\/Date(1260248400000)\/","DepartureDate":"\/Date(1260334800000)\/","LocationID":406,"LocationHash":null,"LocationName":"Colon Free Zone, Panama (PACFZ)","VesselName":"CAPTAIN P (CPP)","VesselID":8,"Order":0,"Comment":null,"remove":false},{"Id":61,"ArrivalDate":"\/Date(1260421200000)\/","DepartureDate":"\/Date(1260507600000)\/","LocationID":407,"LocationHash":null,"LocationName":"Cristobal, Panama (PACTB)","VesselName":"CAPTAIN P (CPP)","VesselID":8,"Order":0,"Comment":null,"remove":false},{"Id":62,"ArrivalDate":null,"DepartureDate":null,"LocationID":408,"LocationHash":null,"LocationName":"Manzanillo, Panama (PAMAN)","VesselName":"CAPTAIN P (CPP)","VesselID":8,"Order":0,"Comment":null,"remove":false},{"Id":59,"ArrivalDate":null,"DepartureDate":null,"LocationID":405,"LocationHash":null,"LocationName":"Coco Solo, Panama (PACSO)","VesselName":"CAPTAIN P (CPP)","VesselID":8,"Order":0,"Comment":null,"remove":true}],"newVoyageDetail":null,"isComplete":false,"Code":"A Code","position":0,"hash":null}
So when the user POST the form i check if changes where made to each voyage like this:
var obj = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(_voyage);
if (TamperProofing.VerifyChanges(obj, hash) == TamperProofing.HMACChanged.True)
{
//UPDATE
}else
{
//DO Nothing, is the exact same object
}
Serialize it again and compare both hashes, the one on the hidden against the recently calculated one, if it match it means nothing changed.
All works pretty well, i was just wondering, if there are any other options to do this?
And my other concern is the time that can take the serialization and all the HMAC thing with this large string generated by the Serialization against the time it could take just to Update unchanged object again to the DB.
EDIT: I don't need to know which field changed, just if something changed.
If server performance is a concern, you might consider creating a "changed" field for each record and using Javascript to set it when one of the user-editable field values changes.
To ensure correctness in downlevel browsers or NoScript fringe cases, a common pattern is to use a tri-state value, with one state meaning "unknown" or "JS disabled". For example, you initialize the fields on the server to 0 (or empty), have your JS code set them all to 1 on load, and then to 2 when the field is modified. If your server sees a 0/null/empty on POST, then it falls back on the server-side method, in this case your hash-compare. If it's set to 1, then it ignores that record, and if it's set to 2, then it automatically triggers an update.
This way, you could avoid computing hashes in 99% of cases, and still correctly handle the outliers.
This doesn't necessarily prevent tampering but you weren't too specific about what kind of tampering you're trying to prevent. The most effective means of tamper-proofing is really to keep the object or information out of reach in the first place, i.e. in the session state or an encrypted cookie.
Does that help at all?

Resources