How to add custom field type plugin with dynamic text fields
How to add custom field type plugin with dynamic text fields
Here is the relevant comment from 6 years ago:
https://datatables.net/forums/discussion/comment/105430/#Comment_105430
I'm wondering if anyone has done this yet? I have a use case where I need to show and edit 0-n pairs of text fields, and I want a '+' to add a new field, and a '-' to remove an existing one, and then save them all as an array. I'm able to show a text field with a '+' sign, but I'm struggling with the internals of the plugin to generate additional fields after the first one when the user clicks the '+' sign.
This question has an accepted answers - jump to answer
Answers
I'm not aware of anyone having implemented specifically this I'm afraid. How are you storing your data - is it an array of values, or a comma delimited string perhaps?
If you could post what you've got so far I can take a look.
Allan
I haven't committed to a data model yet, so I'm flexible on that.
It rather of depends on your database schema. If you are doing a one-to-many join to another table then you might be best taking an approach like this (and just have a text field for each entry in the inner table). That's good for reverential integrity as well, but again, it depends upon your data and the structure you want to store it in.
If however, you are storing it as an array or some separable string value, then the approach of using a list of fields is absolutely valid.
I'd have
conf._input
as an array ofdiv
elements which contain theinput
and anotherdiv
which could be used for the subtraction. That then makes it easier to write theget
andset
methods. Also introduce aconf._wrapper
which would be the containerdiv
that has all of the elements from theconf._input
array in it.Clicking the add button (which would also be in
conf._wrapper
) would add an item to the_input
array, and re-append all items to the_wrapper
(a function for that would be a good idea). Equally, a subtract button would remove the item from the array and call the re-append function.There are a few moving parts, but it is totally possible
Allan
Ok, I'm going to scrap all that and go with the parent/child approach, which I was unaware of. I'll have to dive into the docs and blog articles, but I think it will work, and most importantly, there's existing code to start with!
Let me know how you get on. I do want to write a plug-in like you originally described at some point. I've done it in the past (closed source unfortunately), so hopefully I will have learned a few things from that.
Allan
Ok, I'm back to the original approach, so I'll start with your suggestions, and keep you posted. It's the best fit for my data model, which is a simple jsonb structure stored with each row that needs +/- operations but no referential integrity needs.
Last question for now: what do you mean in your comment 'that then makes it easier to write the get and set methods'?
At the moment you have:
For the getter you could just use:
i.e. get the
input
from each entry in theconf._inputs
array, get its value and return as an array.The
set
function would probably be best erasing the current entries in the array, then adding one for each entry and setting its value (similar to what theadd
event handler would be doing).Allan
Ok, here's what I have so far, still not sure how to proceed on a couple of things:
A couple of weird things happening:
1. I see GET being called, and I don't understand why
2. The first field is invisible, until clicking the '+' then I see 2
3. Still not sure how to delete an element
4. Still not sure I'm doing wrapper and input correctly.