Passing object into select field

Passing object into select field

test42test42 Posts: 21Questions: 8Answers: 0

Hi,

I'm not sure if I missed this but on the select field (https://editor.datatables.net/reference/field/select) I see there's a few options for hardcoding data into the options. Is it possible to pass in an object or array instead?

I'd like to dynamically determine what set of data I should show (marketing vs accounting dropdown options) based on the page I'm loading. Then, pass in that department's options object to send into the dropdown.

Is this possible?

Thanks!

Answers

  • colincolin Posts: 15,146Questions: 1Answers: 2,586

    Hi @test42 ,

    The last two examples on that page, select, show how you can update the select list, and also add a new one to the form through the API. You could do this as part of the table initialising once you know the set of data you have.

    Hope that helps,

    Cheers,

    Colin

  • test42test42 Posts: 21Questions: 8Answers: 0

    I see, so even though we pass data in it initially using "options", you immediately overwrite that field using update? and using update you can pass in a json object?

  • test42test42 Posts: 21Questions: 8Answers: 0

    Also, when trying to use the update function, even using the example, I'm getting this error: Uncaught TypeError: editor.field(...).update is not a function

     $(document).ready(function() {
            var editor = new $.fn.dataTable.Editor({
    
    ----
    
    $('.test-button').on('click', function(e) {
                console.log("test");
                editor.field('url').update([
                    'Mr', 'Ms', 'Mrs', 'Miss', 'Dr', 'Captain'
                ]);
            });
    
  • allanallan Posts: 61,734Questions: 1Answers: 10,111 Site admin

    I see there's a few options for hardcoding data into the options. Is it possible to pass in an object or array instead?

    Do you mean so that if you update the object / array externally then Editor would see that and automatically update the list of options? Currently no - that is not something that Editor can do. It evaluates the object or array that was passed in immediately and adds the options from it to the HTML. It can't then be re-evaluated later.

    You could use an observable yourself and then call the field().update() method yourself.

    TypeError: editor.field(...).update is not a function

    That might happen if it is not a select field.

    Allan

  • test42test42 Posts: 21Questions: 8Answers: 0

    Gotcha, what I was trying to do was if the dropdown was initialized with {option A, option B} I wanted a way to switch it to {option C, option D} based on an event.

    I actually got it to work now but is it possible to do it only per row?

    As in if you're on Row 1 and column 1 has a dropdown that says "marketing or accounting", if you select marketing then column 2 updates it's dropdown with only marketing options? I can figure out how to do that with this code now but I'm wondering if it's possible to just apply that update to a singular row vs the entire table.

This discussion has been closed.