How to render checkbox as checked from localstorage data - javascript

I am new to Sencha and was am looking for help in setting a checkbox to checked when retrieving data from local storage. I have a basic form that saves data with no problems, and can even load the retrieved data back into the form when i want to edit. However, the checkboxes do not appear as checked and I was hoping someone could help me out or explain how i would accomplish this.
The following code snippet is activated when the edit button is pressed, slides in the form page and populates the data from local storage into the form;
activateNewEvent: function(record) {
var newEvent = this.getNewEvent();
newEvent.setRecord(record);
// console.log(record);
Ext.Object.each(record.data, function(key,value) {
if(value != 'false') {
// record.set('checked', true);
// key.checked(true);
}
});
Ext.Viewport.animateActiveItem(newEvent, this.left);
},
My console shows all the form data and the checkbox data, so the info is being saved correctly, but I am struggling to figure this out.

It's been over a year since I worked with something like this but I think you want to use setValue(true) on the checkbox object. I don't know if changing the underlying record after rendering will check the box.

Related

Asp.Net MVC app - caching data between page loads

I have a simple Asp.Net MVC app. I have a form, which I'm trying to gather data for, and then submit. However, within that form is a list of selections - each time a selection is made, I go to the server to add that data:
function addSelection(item) {
fetch("/test/selection/" + item,
{
method: "POST"
})
.then(response => {
const name = document.getElementById('Name').value;
localStorage.setItem("name", name);
if (response.ok) {
location.reload();
} else {
console.error("Unable to add");
}
});
}
The server then stores the list in a HttpContext.Session variable.
The target is to keep the name property in place across calls. The code above works great - I set the control on load:
window.onload = function() {
var getName = localStorage.getItem('name');
document.getElementById('Name').setAttribute('value', getName);
}
However, the form itself is eventually submitted, and the entire form, with the selected items, is submitted to the server:
<div>
<input type="submit" value="Submit"
asp-controller="My" asp-action="Create"/>
</div>
The problem that I have is that the local storage still retains the value of name, and so the next time the form is loaded, it's still there. Does anyone have any techniques for achieving the same result without using local storage?
The only solution that I can think of so far is to replace the submit button with a manually coded JS script that resets the value, but that doesn't account for situations where the user just moves away from the form. I feel like I'm heading down a rabbit hole of my own making, and there must be a simpler way to do the same thing.
One solution is to use hidden form fields to persist the data between requests instead of local storage. On each item selection, add the data to a hidden form field on the client-side instead of saving it to local storage. Then, when the form is submitted, the hidden form field value will be included in the form data and can be retrieved on the server-side.
On the server-side, you can retrieve the NameValue from the form data in your action method.
[HttpPost]
public IActionResult Create(string NameValue, ...)
{
...
}

How to put JSON object restored from chrome.storage.local into a variable?

I'm trying to build a little chrome extension, and, shortly, I have a popup where the user is prompt to flag/unflag a set of checkboxes. A function than modify a JSON object according to checked/unchecked boxes and later saves it in chrome.storage.local.
So far so good; problems starts at a later stage, when I try to recover the stored data into the content_script.
I managed to pass the object into an alert, but, in order for my script to work, I need that JSON to be stored back into a var.
This is the code I inserted in the popup.js and that works fine
chrome.storage.local.set({
capacity: object
}, function() {
console.log('stored with chrome.storage.local');
});
here's the code I have in the actual script:
var capacity;
load();
function load() {
chrome.storage.local.get('capacity', function(result){
capacity = result.capacity;
alert(result.capacity);
}
I'm trying to get capacity to be the same var it was on popup.js when I saved it, but after several attempts I only managed to get the values printed in the alert.
So my question is: is it possible to do this? Where am I getting this wrong?

How can I save the output on refresh : google Appscript

I have a requirement that on refresh the data should stay the same which we are calling from spreadsheet (data in spreadsheet can change due to which User view of his data should not change)
User first inputs his value, the value submits to the spreadsheet where there are some formulas which computes the data and gives the output in webapp.
Hence, if any other user is submitting input at the same time(from webapp) then It may change the output of the first user so we have to save the ouutput of the first user in another sheet which may work as data base. However How can I call the values from the data base and URL should remain the same with uniquue value like timestamp?
In order to have your webapp updated, you should add an auto-refresh function in the JS file.
Example :
function auto_check() {
google.script.run.withSuccessHandler(putData).askData();
window.setTimeout(auto_check, 3000); // every 3sec
}
//function with returning values to update the webapp
function putData(e) {
$('#elem_to_update').html(e.new_value);
}
//in order to start the autocheck as soon as loaded
window.onload = function () {
auto_check();
}
If this is not the result you need, please provide more infos of your issue

Javascript store session value even after click back button

I need to know how I can show the input data of a form after a user presses the back button.
Here is the working jsfiddle
I created a simple asp page.
If I click next after entering some input value, it will go to next page, and again I click back, it doesn't show entered input values.
$("#form").submit(function() {
var inputs=$('.input');
for(i=0;i<inputs.length;i++)validateEmail(inputs[i]);
if($('.invalid').length!==0)return false;
});
I need to show those values. How can I achieve this? Thanks in advance!
Note: In my jsfiddle didn't include next page action.
You have several methods, althought I'm not a big fan of cookies because they are not so smart to manage.
You can either use:
The localStorage API, if you want your values being saved until the user clears the cache, like this:
var myValues = [];
for(i=0;i<inputs.length;i++) {
validateEmail(inputs[i]);
myValues.push(inputs[i].value);
}
// Save them fot later use
localStorage.myValues = JSON.stringify(myValues);
// After clicking the button you can retrieve them back
var oldValues = JSON.parse(localStorage.myValues);
The sessionStorage API, which lives for the current session of the browser (i.e. when the user closes the browser it gets deleted):
var myValues = [];
for(i=0;i<inputs.length;i++) {
validateEmail(inputs[i]);
myValues.push(inputs[i].value);
}
// Save them fot later use
sessionStorage.myValues = JSON.stringify(myValues);
// After clicking the button you can retrieve them back
var oldValues = JSON.parse(sessionStorage.myValues);
NOTE: I'm using JSON.parse and JSON.stringify because both storage objects (local and session) can only store data in string format.

jqGrid - Submit Grid ID In POST Data

I have multiple grids on a single page so my PHP needs to know which jqGrid POST data came from. I thought I could easily use something such as:
jQuery('#grid1').jqGrid({
...
postData:
{
grid: function() { return $(this).attr('id'); }
},
...
});
POST certainly contains grid but the value is populated. For testing I put...
alert($(this).attr('id'));
...elsewhere in my code and it displayed the grid name. I'm not sure how/where to get the value into POST though.
Your help is appreciated, thank you.
I simply added a new field to the POST which contains the "type" of data being submitted so my API knows what it is being handed.

Categories