I using react for my project. I rendering the page based on JSON.
The page has various component types from image to text Content.
Each component has delete option, image can be changed from desktop and text content can be changed by contenteditable.
On performing these actions, the JSON should be updated immediately, later based on an action I would save the modified JSON.
I don't want to write onchange handler for each component, instead as data loads from JSON, it's key map should return or update the respected JSON value.
I am storing the JSON in variable. If required I'm okay to stringify.
Store the JSON in a state property. The key of each component will be its index in the array that you will extract from the JSON. Every time you click the "delete" button, pass the key to the onChange function.
In the onChange function, destructure the JSON and save it into a temp variable. Delete the JSON object at the specified index from the JSON array. Set that temp variable back to state.
Related
What I should do to change the status value in the document specified in the desired collection groups.
You can't update a single property on a specific item in the array. You will have to:
Read the entire document into you application code.
Get the current value of the data array from the document.
Update item 0 in the data array in the application code.
Write the entire data array back to the database.
My requirement is,
By default submit button is in disable mode, If user changes existing
data in form then submit button should change to enable mode after
that he changes his mind and reverted back to old data then submit
button should change to disable mode.
For this requirement I have done code changes those are
While loading form data I am taking a temporary variable placing
form data in that temporary variable.
User changes any field in the form I am comparing form data with that temporary form data by using angular.equals(obj, tempObj)
method.
These code changes are working for one way i.e, User changes form data, I am identifying that event and comparing form data with temporary data and changing the button into enable mode --> working
The problem is my form contains non mandatory fields. By default form contains empty data in those non mandatory fields.
First user enters data in that non mandatory field then one json key, value pair is creating in my JsonObject. When i compare two objects by using equals method it is returning false after that i revert back that non mandatory field but the created json object property is not removing from JsonObject with empty value. This is angular inbuilt behavior.
Can anyone suggest me how to remove that empty value json key in my JsonObject.
Note : I will not use for each to iterate and find the empty json value and remove it from the JsonObject because my form contains 40 elements. It will leads the performance of my application. I have to apply same behavior in different places.
You can _.Omit to remove json object https://lodash.com/docs/4.17.10#omit
https://ufile.io/cc219
sorry about the dl timer - any better upload sites?
I am trying to copy an array located in a service to a component to populate data on my view, however it's behaving inconsistently.
The values of the copied array are changing the items on the view automatically, while other items set individually are not affected.
All of my variables are private and I have no idea what is causing this
Arrays are reference types. If you pass an array around, the receiver gets a reference to the same array. It's the same fore objects. Primitive values like number, string, or boolean are copied by value, which means the receiver gets a copy of the value instead of a reference.
I have a JSON object which contains models array. Each model contain params array and each param contain values. values should displayed on page as radiobuttons or selects and user can choose it. I need to build HTML so that if user chooses the value then variable selectedValues keeps object like this {param_id: value_id, another_param_id: another_value_id} e.t.c. This variable should show what values for which parameters selected by the user. The problem is i don't know how many params and how many value will come - it's fully dynamic JSON, generated by server.
Demo fiddle
http://jsfiddle.net/z68tx53c/1
If you are using selectedValues()[1] when selectedValues() is null (which you are, before it's populated), you will get this error: Cannot read property '1' of undefined . Wrap the html which uses selectedValues in a with or if binding, so that the containing nodes are only there when selectedValues is non null.
If one has the output of a JSON.parse readily at hand, is there some legitimate way of storing that object in the DOM as a single entity?
i.e.
jsonObject = JSON.parse(something);
I'd like to consider storing the jsonObject in the DOM as a (child || textNode || ???) of some element and later retrieving it so that immediately after retrieval I could access its contents via the usual:
jsonObject.keyName
I want to avoid adding dozens of dataset attributes and later being forced to extract them one at a time. That's too much work and seems too inefficient if you have dozens of key:value pairs.
Data attributes have to be strings, you can't store objects in them directly. So don't parse the JSON string, just store the JSON string directly in the dataset attribute.
If you use jQuery, its .data() method will take an object, and it will automatically stringify it as needed.
If the elements you want to associate the object with all have IDs, another option is to use a global object as a table, keyed off the element's ID.
jsonTable[id] = jsonObject;
It depends on the life cycle of your page. If you don't intend to reload the page it's better to just leave it as a JavaScript variable on the page.
You can also consider storing the raw JSON in a hidden filed or some other hidden DOM element. Keep in mind though that hidden fields will be posted to the server if you do a post of the page
TGH has the right answer. Leave it as a variable.
An alternative is to use history.pushState() to store it along with the URL to your page. This is helpful if you ever want the user to be able to click the back button and have the json restored to the value it had for that page.
If you want to store a data (JSON) associated with DOM element.
You could use jQuery data function.
e.g., store a JSON to a restaurant row (div)
$("div.restaurant-row").data("info",{purchases: "blablabla", mealFormulas: "xxxxx"});
e.g., fetch DOM associatd data
$("div.restaurant-row").data("info").purchases; //blablabla
I'm not sure if this is what you want.
Hope this is helpful for you.