Object Custom Select Dropdown

Object Custom Select Dropdown

btreebtree Posts: 99Questions: 14Answers: 11

Hi,

I want to use a custom values for a dropdown but an object does not work. The problem are the outer [..] from the object.

Field Code:

{
    label: i18n.gettext("Apiary:"),
    name: 'movedate.apiary_id',
    type: 'select',
    options: apiary_dropdown('table_dropdown')
},

Object Function:

var obj = {};
$.each(e,function(key, value)  //GET EACH OBJECT
{
 obj[value.name]=value.id;
});
console.log(obj);
return obj;

Don't work

options: [{Home: "500", test2: "501"}]

Work:

options: {Home: "500", test2: "501"}

Cheers
Hannes

This question has an accepted answers - jump to answer

Answers

  • ThomDThomD Posts: 334Questions: 11Answers: 43
    edited September 2015

    You have not talked about what software and versions, so I'm guessing this is Editor. Check out the docs on the select field type.

    http://editor.datatables.net/reference/field/select

    From the docs, when using an array of objects, the field labels need to be in the objects. I can' tell from your code, but I'm going to guess that those are key/value pairs. So:

    options: [{ label: "home", value: 500},{label: "test2", value: 501}]
    
    
  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin

    The first will work if you have an object for each item you want to appear and use the optionsPair option for the field type to specify what parameters you should use. however, if your array only has a single item in it, I would suggest simply getting the object (i.e. myArray[0]) rather than passing in an array that isn't used.

    Allan

  • btreebtree Posts: 99Questions: 14Answers: 11
    edited September 2015

    Hi,

    first thanks for the quick answers but I cant follow you.

    In the documentation it says:

    object - Name / value pairs, where the property name is used as the label and the value as the field value. For example: { "Edinburgh": 51, "London": 76, ... }.

    With my understanding my above code should work, also when i copy and paste the console log into the option it works.

    obj[value.name]=value.id;   //name = label, id = value
    

    edit tried an array like ThomD said, also dont work for me?

    var arr = new Array();
    var i = 0;
    $.each(e,function(key, value)  //GET EACH OBJECT
    {   
        arr[i] = {"label":value.name, "value": value.id};
        i++
    });
    console.log(arr);
    return arr;
    

    Cheers
    Hannes

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    Answer ✓

    First we need to determine what your data structure looks like. Do you have an array of multiple objects, or an array with just a single object that has lots of key values in it?

    Can you link to the page so we can debug it directly please?

    Allan

  • btreebtree Posts: 99Questions: 14Answers: 11

    Hi,

    could fix it myself. Was good to take a day off programming. I use a ajax call for the return data and didnt though about async and that the return object is not defined.

    Now i wrap the code into a $.when....done function and populate the select with update.

    if (kind == 'apiaries') {  //Only get dropdown data for specific table
        $.when(apiary_dropdown('table_dropdown')).done(function(data){  //check when ajax call is done (deferred object) 
            e = $.parseJSON(data);  //make PHP JSON TO JQUERY OBJECT
            var obj = new Object();  //create empty object
            $.each(e,function(key, value)  //GET EACH OBJECT
            {   
               obj[value.name]=value.id; 
            });
            move_edit.field('movedate.apiary_id').update(obj);  //update select field with dynamic data
        });
    }
    

    Thanks again for all the help and advices!

    Great Support
    Cheers Hannes

This discussion has been closed.