How to update a table row with save button using .ajax - asp.net

I have a table which has one row and only one cell will be editable. I have accomplished this with the following code.
$("td#effEndDate").click(function() {
if (!$(this).hasClass("edit")) {
var value = jQuery.trim($(this).html());
$(this).html("<input id=\"txtEdit\" type=\"text\" value=\"" + value + "\" />");
$(this).addClass("edit");
$("#txtEdit").focus();
}
});
Now this is the part where i'm stuck.
After the field is updated a save button must be clicked to call the proper .ajax method to update the database. But how can I compare the previous value to the current value on a button press? Since i'm not using the onblur property where I could have saved the old value and passed it to the update function.

There are two possibilities.
Pass the variable around in between functions
Make the variable global
if you want the variable global do not use the "var" keyword
Change:
var value = jQuery.trim($(this).html());
To this:
value = jQuery.trim($(this).html());
Edit
If the click function is getting hit more then once before a page refresh and assuming you want to keep a copy of the original table rows you can try this. Save a copy of the original table in a variable then you can query the original table for the html using the ID number. Here is a quick mock
first store the table in a variable upon the page loading. This will save an original copy of the table
//get a copy of the table
var GetCopyofOriginalTable = function() {
var TableToBeCopied = $('the appropriate selector');
CopyOfTable = JQuery.extend(true, {}, TableToBeCopied); //Notice no var, its global
}
//Now store the variale
GetCopyofOriginalTable();
var FindTableRowByID = function(trID) {
return $('table:has(tr#' + trID));
}
Now you can loop through the new table test its value agaisnt the old table. This methed make use alot of memory depending on how big the table is.

I would store the original value somewhere in a variable, and compare the two in the submission function before sending the .ajax call.

Related

nextPage() method - not functioning

I have a datasource that contains > 1000 records. The current Query page size is at 100.
I have a need to loop through each item, and try to find a record that matches input given by the user. Fairly simple use-case, however, I can't seem to get the script to loop through the pages so it just finishes its loop at the query page size of 100 and therefore only searching the first 100 records.
I've tried putting in
app.datasources.Vehicles.nextPage();
at the end of the for loop and then call regoExists again with the new page but it doesn't work. How is nextPage() meant to be used in client scripts?
function regoExists(rego){
var regoUp = rego.toUpperCase();
regoUp = regoUp.trim();
ds = app.datasources.Vehicles.items;
for (var i in ds){
if (ds[i].registration === regoUp){
console.log(ds[i].registration + " equals " + regoUp);
app.datasources.Vehicles.query.filters.registration._equals = regoUp;
return true;
} else {
console.log(ds[i].registration + " does not equals " + regoUp);
continue;
}
}
}
Rather than looping through each record and performing the query on each individual record I would suggest introducing a textbox widget in the same datasource and setting the binding to:
#datasource.query.filters.registration._equals
Then load the datasource via a button click or via the onValueEdit event of the textbox widget. If the registration value exists, it will be returned in a table presumably, and if it doesn't exist no records would be returned.

How Can I Duplicate A Item/Record?

(Deleted my old question to simplify it. )
I enter data in a table, I then want to make an exact duplicate of that data in a new item/record/row*.
*not sure the proper term.
Is there any way to accomplish this?
Sorry for the slow response. Here is what you should do:
Add a "copy" button in the row. In the onClick on that button, add this code:
var createDataSource = widget.datasource.modes.create;
var rowDataSource = widget.datasource;
createDataSource.item.foo = rowDataSource.item.foo;
createDataSource.item.bar = rowDataSource.item.bar;
// And so on for each field
createDataSource.createItem();
You could probably make sure of javascript for-in to loop through all the properties of the item in so you don't have to manually specify each record, but I didn't have time to experiment with this.
Edit:
The above code won't show the copied record in the list immediately, because I used row's create data source, instead of the lists create data source. Try this instead:
var rowDataSource = widget.datasource;
// Instead of using the row datasource for create, explicitly use the data source of your list.
var listDatasource = app.datasources.NameOfYourListsDataSource;
var createDataSource = listDatasource.modes.create;
createDataSource.item.foo = rowDataSource.item.foo;
createDataSource.item.bar = rowDataSource.item.bar;
// And so on for each field
createDataSource.createItem();

xpages session scope AND document data binding

I have a value that I get from a picklist. I set this value as a sessionScope variable.
I then want to use this value, do a lookup, and set the value of an input field - which is working.
However, I am doing the lookup code in the fields data binding section using SSJS, and as such am not too sure how to save this value (normally my data binding would just be document1.FIELDNAME)
I've tried setting the value as part of my code, but the change is not saved in the backend doc.
I've also tried doing the lookup code in the fields "Default value" property, but this always just returns nothing.
Does anyone know how I can display on the xpage the value from my lookup AND also save this value to the backend document?
I fear I am missing something simple and maybe getting tunnel vision!
The code I am using for my data binding value is below.
Thanks
try{
var key1 = sessionScope.PLProspectiveAssured;
var dbName:NotesDatabase = session.getDatabase(database.getServer(),"CIR2001.nsf");
if (key1==""){
returnVal = "Not found";
}else {
var vwOrgs:NotesView = dbName.getView("OrgDocID");
var doc:NotesDocument = vwOrgs.getDocumentByKey(key1);
returnVal = doc.getItemValueString("OrgCountry");
}
// set our field
var doc:NotesDocument = document1.getDocument();
doc.replaceItemValue("ProspectiveAssured", returnVal);
return returnVal;
}catch(e){
openLogBean.addError(e,this);
}
Use your datasource and set the value using .setValue(field, value). In your case:
// set our field
document1.setValue("ProspectiveAssured", returnVal);
Make sure that you save your datasource somewhere (else).

meteor 0.5.7: how to handle/use Meteor.Collection.ObjectID?

I have updated my meteor yesterday and tried using the new Meteor.Collection.ObjectID.
But since with no success. First i updated my collections in this way:
myCollection = new Meteor.Collection('mycollection', {idGeneration: 'MONGO'}
Now, normal new inserts have an _id like Wi2RmR6CSapkmmdfn... (?)
Then i have a collection with an array included. I like to have an unique id for every object in this array. So i $push an object with a field like id: new Meteor.Collection.ObjectID() into my array. The result in the database is like this: ObjectId("5b5fc278305d406cc6c33756"). (This seems to be normal.)
But later i want to update my pushed object, if the id equals an id, which i stored as data attribute in an html tag before.
var equals = EJSON.equals(dbId, htmlId); (This results every time in false. So i logged the values dbId and htmlId into the console with console.log(typeof dbId, dbId);)
The values of this two variables is as follows:
object { _str: 'a86ce44f9a46b99bca1be7a9' } (dbId)
string ObjectID("a86ce44f9a46b99bca1be7a9") (htmlId; this seems to be correct, but why is a custom type a string?)
How to use the Meteor.Collection.ObjectID correct?
When placing your htmlId in your html you need to put it in as a string and not as an object, remember _id is an object now, handlebars is guessing and using toString() & thats why it shows up as ObjectID("...").
So if you're using {{_id}} in your html you now need to use {{_id.toHexString}} to properly extract the string part of it out
When you extract this html value with your javascript you need to make it back into an objectid:
js:
var valuefromhtml = "a86ce44f9a46b99bca1be7a9"; //Get with Jquery,DOM,etc
htmlId = new Meteor.Collection.ObjectID(valuefromhtml); //see: http://docs.meteor.com/#collection_object_id
EJSON.equals(htmlId, dbId); //Should be true this time

ASP.Net Auto-populate field based on other fields

I've just moved to web development and need to know how i can implement below requirement using asp.net and vb.net.
I have three fields in a form which are filled by users. Based on these three values, i need to auto-populate the 4th field. I have planned to implement this in the following way
Write a separate class file with a function to calculate the possible values for the 4th fields based on 1st 3 inputs. This function can return some where between 1-10 values. So I've decided to use drop-down for 4th field, and allow users to select the appropriate value.
Call the above function in onchange function of 3rd field and take and use the return values to populate the 4th field. I'm planning to get the return values in array field.(Does this need a post back?)
Please let me know how if there is better way to implement this.
Thanks.
You may want to consider doing this with Javascript. You could read and control the fields pretty easily with pure Javascript, or using a nice library like jQuery (my favorite). If you did it this way, no post-back would be required and the 4th field would update immediately. (Nice for your users)
You can also do it with ASP.NET for the most part. "onchange" in ASP.NET still requires Javascript as far as I know, it just does some of it for you. A post-back will definitely happen when you change something.
You need javascript or to set autopostback=true on your form elements.
From a user perspective the best thing is to use javascript to populate the field for display, BUT when the form is submitted use your backend function to validate it. This will make sure the user didn't change the value.
An easy way is to use jQuery for the UI (that way you don't have to worry about long winded javascript and deal with browser compatibility as it's already taken care of for you) and have it call to the server for the data. For the server, your easiest route is to return JSON for looping values.
Include your jQuery:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
Then add in a handle for the JavaScript:
<script type="text/javascript">
function autoPopulate() {
var value1 = $('#ddl1').val();
var value2 = $('#ddl2').val();
var value3 = $('#ddl3').val();
var url = 'path/to/your/file.aspx?value1=' + value1 + '&value2=' + value2 + '&value3=' + value3;
$.getJSON(url, function(data) {
data == null ? return false : data = eval(data);
var ddl = $('#ddl4')[0];
for (i = 0; i < data.length; i++) {
var option = new Option(data[i][0], data[i][1]);
if ($.browser.msie) {
ddl.add(option);
} else {
ddl.add(option, null);
}
}
}
}
</script>
(Yes, I know I used a native loop but I'm little lazy here today :) )
Now, for your server side code you'll want your code your page to return data in the format of:
[['value1','text1'],['value2','text2'],['value3','value3']]
so something like:
<script type="vb" runat="server">
Private Sub Page_Init()
// get your data
// loop through it and add in values
// ex.
Dim result As String = "[" //start multi-dimensional array
For Each Item As String In data
result += String.Format("['{0}','{1}'],", _value, _text)
Next
result = result.SubString(0, result.Length - 1) // removes trailing comma
result += "]" // closes off m-array
Response.Write(result)
Response.Flush()
End Sub
</script>

Resources