Dynamic Select with 'Only Choose Once' options
Dynamic Select with 'Only Choose Once' options
Alright, sorry about the cryptic title but it was the only thing I could think of to describe what I'm trying to accomplish here. Here's the setup:
Latest DataTables and Editor versions
I have a database column of all IP Addresses we own. Some IPs are tied to an item and some are not. I have the data properly showing inside DataTables and inside the Editor Select field for IP Address. This all works fine. Here's the kicker, If another item returned in the DataTables query is using an IP Address then it should NOT show in the Editor Select field as selectable. I've tried a few different ways to accomplish this but I can't seem to show the current item's IP (when set) and not any of the other IPs that have already been assigned.
Goals:
-Show each item's IP in DataTables table results view
-Show current selected item's current IP in Editor form's IP Address Select drop-down
-Show available IPs that are not linked to another item in Editor form's IP Address Select drop-down
I hope this makes sense. We're trying to basically keep track of which IPs have been used and which haven't inside our database. We want to be able to pick an available IP and assign it to an item but then not be able to pick that IP again in another item. Is doing a separate ajax call from the editor field for the IP Address to retrieve the data the only option here? Not quite sure how to go about doing this.
This question has an accepted answers - jump to answer
Answers
It sounds like you'll need to use either the
select
field type'supdate()
method or thedependent()
method (which in turn would use theupdate()
method).Specifically, use
update()
to update the list of options available, removing the ones which are no longer available.One option would be to return the remaining options available in the Ajax response to a create / edit / delete action, and then update the list of options based on that data.
Allan
Alright, so I've been playing around with the update method for quite some time. I tacked on all Available IPs and All IPs in my initial DataTable Ajax response. I've tried manually updating the field each time an Editor form is opened with the available IP list which works BUT it does not include the current Item's IP in the drop-down. SO, I went about manually adding it in and calling update() each time the Editor form is opened. Seemed to work but then since I needed to add the current IP to the drop-down utilizing the value found here editor.fields('ip').get(), it seems to get stuck on the previous item's field value. I'll open one item, close it, open the next and the previous value is used in the wrong item's form because editor.fields('ip').get() called in the editor.on('open' method is not updating. Any ideas what could be causing that?
You can kind of see what I'm playing around with in my editor.on('open here:
The
get
value is probably incorrect at that point since the list of options won't include the value that you need. Aselect
list cannot take a value that is not in the value list and your existing value is not in the list. So when Editor tries to set the value for edit, it can't!Possibly the best way to handle this is to read the IP value for the row being edited from the DataTable, rather than the form. You could use
modifier()
androw().data()
to do that - e.g.:var rowData = table.row( editor.modifier() ).data()
. And then get the IP value from the row's data.Allan
Allan! You're a life saver! That worked perfectly to get the value and perform the modifications to the drop down that I needed. To keep the field properly updated I just do a table.ajax.reload() on the editor close event now and everything seems to be working perfectly. Thanks so much for your help and because I haven't said it yet (we just bought Editor a week or so ago), I absolutely love this product! It's made this project I'm working on so much fun! Thanks again!
You are very welcome :-). Great to hear that helped!
Regards,
Allan