Comments (7)
Completely understand, thanks for your time and quick responses
I've come up with a hacky workaround for now https://jsfiddle.net/yusafme/vuj3kth5/18/ !!!
let form = document.querySelector('form');
function fromJson(data) {
FormDataJson.fromJson(form, data);
checkBoxes(data, '');
}
function checkBoxes(data, nameAttr = '') {
const isStringArray = Array.isArray(data) && data.every((str) => typeof str === 'string');
if (isStringArray) {
if (!form.querySelectorAll(`[name="${nameAttr}[]"]`).length) {
return;
}
for (let i = 0, iLen = data.length; i < iLen; i++) {
const checkbox = form.querySelector(`[name="${nameAttr}[]"][value="${data[i]}"]`);
if (checkbox) {
checkbox.checked = true;
}
}
return;
}
for (let key in data) {
const newNameAttr = nameAttr === '' ? key : nameAttr + `[${key}]`;
if (data[key] && typeof data[key] === 'object') {
checkBoxes(data[key], newNameAttr);
}
}
}
fromJson({
communication: {
preference: ['email', 'letter']
}
});
from form-data-json.
Thanks for your report. I think this is almost impossible to fix for how the internals currently work, without breaking any other working edge cases.
Undefined input names (with an implicit key from []
) are tricky. To make this working, the internal recursion must be totally reworked, as it's currently unclear what name matches the exact values. It would require a separate register with all the checkbox values in each name level, then checking that. It gets even more complicated when somebody mixes named and implicit keys.
I don't tend to touch this right now.
If you think the other way, uniquely named input names with a proper object instead of array values, example communication[preference][sms]
and {communication:{ preference:[{"sms": "sms"}}
it will work without problems.
from form-data-json.
BTW thank you soo much for this project, really very grateful, I'm using it in my own project which hopefully I'll share with you when complete.
I tried going through the internals, but honestly I got lost very quickly !!!
I have done a few workarounds within my project already, but was really struggling with this issue
Although I may have thought of another workaround just now!!!, something along the lines
form.querySelectorAll('[name="communication[preference][]"][value="letter"]').checked = true
Would it be possible within the internals if the key is []
to iterate over the array of values and check the checkboxes like above?
from form-data-json.
Unfortunately no. The inputs are traversed in a tree manner, depending on the options given. There is no deeper selector use at all. Basically its just traversing the name-tree and traversing the given object-tree and comparing the same levels and values.
Keeping track of which tree level you are is the hard part and not easy to change in this project without breaking anything.
And as you see in your example, the trees are not equal, so its not finding it.
It's sadly just a case that don't work right now. I understand your thing, but don't have time and muse to touch it now just because i know how hard it will be to fix and not breaking anything else :)
from form-data-json.
I was curious about a fix... And tried something. Maybe give it a shot from source of current main?
from form-data-json.
Fantastic, working like a charm!
Thanks soo much you've been a great help :)
from form-data-json.
Alright. Released with https://github.com/brainfoolong/form-data-json/releases/tag/2.2.1
from form-data-json.
Related Issues (20)
- v2-dev: implement `resetOthers` similar to .reset/.clear methods HOT 3
- v2-dev: make `uncheckedValue` false by default HOT 10
- v2-dev: `skipEmpty` still includes empty arrays/objects HOT 12
- Document how to import this module HOT 18
- IncludeButtonValues does not include button values HOT 4
- Trigger for 'onchange' HOT 2
- Doesn't work when inputs are nested more than one child element deep HOT 1
- Focus Element metod HOT 1
- Implement FormDataGraphQL HOT 3
- Automatic array processing while fromJson HOT 2
- Get empty value in the data if use field name with nesting (array) HOT 4
- Name syntax with dot HOT 2
- Elements out of order are processed incorrectly HOT 4
- Conversion of a list of element[number] not done when a number is missing HOT 3
- radio element value 0 HOT 1
- how to skip non selected select option HOT 2
- Module parse failed: Unexpected token (15:30) HOT 4
- Use `import` instead of `require` HOT 3
- Wouldn't it be nicer if toJSON returns promise HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from form-data-json.