how to restore back data in template using knockout.js - javascript

I want to build a template like gridview. My problem is when I edit the data '123qwe' to '123' but when i click 'cancel' button not to edit the data. the data not restore back to '123qwe'.
I have a workaround to solve this, however, it is not work in my computer but in jsfiddle, I want to know the reason and help to solve in a knockout.js format. Thank you~~~
code to attention:
// Reset button
self.reset = function (index) {
self.nameLists([]);
$.each(og_arr, function(i,item){
self.nameLists.push(new nameFilter(item.name));
});
this.editTemplate("readOnlyTemplate");
};
program in jsfiddle

There is no built-in notion of canceling an edit. You are editing an observable. Have a look at protected observables, a simple extension that allows you to commit or reset a change.

For general "undo" functionality, check out the Memento plugin for Knockout.
(I didn't write it, just a satisfied user)

Related

How to create handler on localestorage changed using native javascript

How to create handler on localestorage changed using native javascript?
My try is:
console.log("START");
window.addEventListener('storage',function(e){
if(e.storageArea===sessionStorage){
console.log("storage was changed");
} else {
/* else, event is caused by an update to localStorage, ignore it */
console.log("ELSE");
}
});
localStorage.setItem('qweyxcadsadsadsa', "DSADAS");
console.log("end")
But, it not working, where is a problem?
Thank you for any help!
There is a jsfiddle:
https://jsfiddle.net/1qft6gcb/
You can create your own trigger with setInterval(). On first run, store current values. After that, compare current values with previous, trigger if necessary and update values. I would suggest using an existing pub/sub library for that. Amplify.js encapsulates both local storage and pubsub, but is quite old -mainly because it works very well and doesn't need updates.

Dojo Gridx programattic refresh shows no data

I am trying to create a programmatic filter. I have a dijit.tree and a dojo gridx using the same source on a jsp. When user clicks the tree node, I want to use the node as a filter and show all rows matching it in the gridx
This is my code I have now for the onClick event of the dijit tree node.
var global=this;
treeWidget.onClick = function(item){
global.grid.filter.setFilter(global.grid.filter.grid.filter.moduleClass.or("test"));
Earlier I asked for a sample expression. I went and tried the code above and seems to
refresh the grid but comes back as No items to display. I do have data that match test and if I do a manual filter I see data returning. What am I missing here.
At https://github.com/oria/gridx/wiki/How-to-filter-Gridx-with-any-condition%3F ( see Filter Expressions)
I was able to accomplish the task using the following code in the diji.tree onClick event.
global.grid.filterBar.applyFilter({
conditions: [{
condition: 'contain',
value: 'test'
}]
});
This is a comment rather than an answer, but I can't post comments yet.
Can you post a working snippet of code? That's not complete, as I don't see your store that you're specifying, etc.
I usually do a myinstancename.grid.body.refresh(); to accomplish a proper refresh.

Chosen:Trigger function if no results

I'm using Chosen jquery plugin. It shows "no results" if none found.
Is thera a way to trigger a function if no results found using this plugin?
Now, There is event chosen:no_results that you bind function to like this:
$('select.chosen-select').bind('chosen:no_results', chosenNoResults);
function chosenNoResults(evt, params) {
//Add new Option Logic
}
Refer Github for Reference
As a developer of Chosen I can safely tell you that there is currently no callback or event triggered when there are no results.
Although you could submit a feature request as an issue at the GitHub repository
The last few weeks there has been active development on Chosen, so changes of quick action upon your request are good.

Adding Changes Saved Javascript Popup

This is a MVC3 project using razor. Instead of displaying another view to inform the user that the changes have been saved successfully I would like to simply fire a JavaScript popup informing them... Everything I have found on the web either opens a whole new browser window, or misses what I am trying to accomplish all together... I know there is a simpler way to go about doing this but this is where I am... At the end of the controller function that does the save on the return I simply use redirect and send it to another controller function that displays a screen saying "Changes Have Been Saved Successfully" then the user clicks a button there which will take them back to the index page... IMO this is a bit shotty and think it can be cleaned up through the use of Javascript...I have not found any luck on this yet.. Currently the below code is what I am using:
Function SomeFunctionName()
db.SaveChanges()
Return RedirectToAction(ChangesSaved)
End Function
Function ChangesSaved()
Return View()
End Function
And the javascript that I have implemented in the ChangesSaved view.
#Code
ViewData("Title") = "ChangesSaved"
End Code
<script type="text/javascript">
alert("Changes Have Been Saved Successfully");
</script>
There are a few problems with this though...
How do I tell the javascript When the user clicks OK it should take them to another page.
I did just try the below and since I am very new to java/javascript it failed:
var r=alert("Changes Have Been Saved Successfully");
if (r == true) {
#html.Action("***********","Admin")
}
If I were you I would post your form using Jquery. Then you can set a callback. In Mvc you can return JSON data, a simple value indicating that the save worked would be enough. Then you can call your alert although you might consider using a jQuery UI dialog as it's way more flexible. If you haven't ever used jQuery I wouldn't be afraid, it's easy and there is a lot of great examples out there.
Take a look at this http://api.jquery.com/jQuery.post/ and this, ASP.NET MVC controller actions that return JSON or partial html

asp.net disable/enable validator using Javascript

I have a complex form requiring me to switch specific validators on or off depending on selections made by the user.
ValidatorEnable seems to do the job, but it seems that when I call this method it actually fires the validation process as well, without the user actually hitting the submit button.
Is that how it works?
I eventually found the best way to do this was to use the following code:
var validatorObject = document.getElementById('<%=ValidHasShippingLocationZip.ClientID%>');
validatorObject.enabled = false;
validatorObject.isvalid = true;
ValidatorUpdateDisplay(validatorObject);
I wrote some code seems can meet your requests.
Iterate validators and enable these you needs.
ValidatorEnable(validatorObj, true);
then clear the screen,erase the error info.
The full code snippet can be found here http://codelife.cybtamin.com/enable-and-disable-asp-net-validator-by-javascript/

Categories