user added select values

user added select values

loukinglouking Posts: 283Questions: 55Answers: 0

Description of problem:

I intend to have an editor field where the user can add email addresses. These will not be stored in a separate table from which the options can be retrieved. These will be stored the table I'm editing which will store these as a@example.com;b@example.com. I initially tried doing this with select2 using its tags feature, but the data which is already in the table isn't shown when I bring up the editor modal. (The table already has single email addresses as text fields, but I want to add the feature allowing the user to add or create multiple addresses)

I want to create a link to a test case, but first I want to verify that select2 is an ok way to do this. Most of the selects in my application are select2.

I looked briefly at the tags data type, but I don't think this works for my use case.

Assuming select2 is fine, where is the best place to find a live version I can clone to show my problem?

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 65,720Questions: 1Answers: 10,930 Site admin

    The Select2 plugin for Editor has a separator option that might be useful here. If you have a string which has multiple values delimited by a specific character, use that option to have it split the string up into the individual parts.

    Here is a Select2 example in Editor that you could use as your base.

    Do you need the Select2 lookup feature here (since you aren't storing it, there is nothing to lookup)? Or is it just a way to combine multiple values into a single string?

    Allan

  • loukinglouking Posts: 283Questions: 55Answers: 0
    edited April 14

    Just looking for a way to combine multiple values into a single string, and edit them if they change. I am using the separator option. The issue I'm seeing is the value already in the database isn't displayed. I'll work up a test case hopefully some time today.

  • loukinglouking Posts: 283Questions: 55Answers: 0
    edited April 14

    sorry, before I made any changes with the example, I wasn't seeing the edit buttons, and when I Run with JS I got Script error (line 0). Adding select2 to the html didn't help. https://live.datatables.net/devokawi/4/edit

  • loukinglouking Posts: 283Questions: 55Answers: 0

    I tried putting in specific versions. The root cause of the problem may be

    VM944 dataTables.select.js:1908 Uncaught TypeError: DataTable.type is not a function
    (anonymous) @ VM944 dataTables.select.js:1908
    (anonymous) @ VM944 dataTables.select.js:44
    (anonymous) @ VM944 dataTables.select.js:46

  • kthorngrenkthorngren Posts: 22,459Questions: 26Answers: 5,166

    For some reason Allan's example was loading Datatables 1.13.11 which s causing the errors you are seeing. You didn't remove tho old version of Datatables when updating to the new version so you were still getting the errors. I used the Downlaod Builder to replace the script tags in the test case. The updated test case works:
    https://live.datatables.net/devokawi/6/edit

    Kevin

  • loukinglouking Posts: 283Questions: 55Answers: 0
    edited April 14

    Thanks for getting this to work.

    I've reproduced my problem. See https://live.datatables.net/devokawi/7/edit

    I made the Position field a select2, with no select options but using the tags feature of select2.

    I want to allow values of the position field to a) be added by the user, and, b) show what's already in the table cell.

  • allanallan Posts: 65,720Questions: 1Answers: 10,930 Site admin
    Answer ✓

    Ah sorry, I need to update that base line - it has been quite a while since I've used that specific one. Many thanks for spotting that and correcting it Kevin!

    It appears that in Select2 for tags, it doesn't automatically create a tag when you set a value that it doesn't know about. If there is an option for that, I couldn't find it. So I tweaked the plugin in that example to be this. There is a // Moved comment and an // Added comment for the two changes to the plugin.

    Allan

  • loukinglouking Posts: 283Questions: 55Answers: 0
    edited April 14

    hmm, I think you started with an older version of the select2 plugin. I retrieved a version yesterday from https://editor.datatables.net/plug-ins/field-type/editor.select2

    It's a shame these aren't versioned. ;)

    Will you be updating that page with the latest (corrected) version?

  • loukinglouking Posts: 283Questions: 55Answers: 0
    edited April 17

    I just noticed with this version if the field is empty on Edit, or for New there's something shown in the select field, an X like there's an empty entry.

    See https://live.datatables.net/devokawi/9/edit for Ashton Cox

  • loukinglouking Posts: 283Questions: 55Answers: 0

    I fixed it under // Added for tags

    hopefully this code will work in all select2 situations

    Is this something you'd put on the plugin page?

  • allanallan Posts: 65,720Questions: 1Answers: 10,930 Site admin
    Answer ✓

    https://live.datatables.net/devokawi/10/edit

    A small tweak to the .split() operation to check for a value fixes it.

    Yes, I really need to get these versioned and also available on npm. That is on my list :).

    Allan

Sign In or Register to comment.