preselect an item in a drop down list of Editor popup

preselect an item in a drop down list of Editor popup

bayenibayeni Posts: 57Questions: 8Answers: 0

I read many threads about select in editor, so I found the update function for reading the options from database and update the select in the editor. I use editor version 1.5.2.

my editor fields:

// is called with loading the page
function getCompanynames () {
    $.ajax({
        url: 'contact/companies/dt_names',
        success: function (response,statusText) {
           var aDropdownList = new Array ();
           var l = response.length;
           for (var i=0;i < l;i++) {
               aDropdownList[i] = { 
                       "label": response[i][0], 
                       "value": response[i][1]
               };
           }
           editor.field('company').update(aDropdownList);                
        }
           error: ...
    });
} 

editor = new $.fn.dataTable.Editor( {
...
      fields: [
        { "label": "Id",  "name": "id", "type": "readonly" },
        { "label": "Number",  "name": "number"},
        { "label": "Company", "name": "company", "type": "select",
                // options set with update
        },
        { "label": "Companyname", 
          "name": "companyname" , 
          "type": "hidden"
        }
      ],
...
})

But now I need to know how can I preselect a label or value from the select list, to show the company name from the selected table row in the select list. The selected item has to be set dynamically for each row which should be edited. I want to try it when the edit popup opens (editor.on('open', function ...)).
I can't find that information in the duscussions.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Sounds like you want to set a default value - fields.def and field().def() can be used to set the default.

    Allan

  • bayenibayeni Posts: 57Questions: 8Answers: 0

    maybe, but I don't get it working, with

           editor.on('preOpen', function( e, mode, action ) {
                  var company = editor.field( 'company' );
                  $('#info').append(table.cell('.selected', 4).data()+"< /br>");
                  company.def(table.cell('.selected', 4).data());
           })
    

    I tried preOpen and open, and the label and the value from the select (4 is the label, 5 would be the value), in my test field #info, it shows the correct value. But in the edit popup in the select I only see the empty option and if I open the select I can see the whole list of options. So the list os there, but nothing is selected.
    Mißunderstood I how to use it?

  • bayenibayeni Posts: 57Questions: 8Answers: 0

    Can anybody tell me, why that isn't working, if def is the function to select an option in a dropdown?

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    The field().def() function will set the default value. So when the Editor form is shown, if there is no value (i.e. in the case of a "Create" action) then the default value will be selected.

    If you want to set a value, not a default, then use field().val().

    If you do actually want a default, the default would have to be set before preOpen. It should be set as soon as you know what the default will be (i.e. before the user interacts with the form).

    Allan

  • bayenibayeni Posts: 57Questions: 8Answers: 0

    Thanks Allan, val() does what I need.

This discussion has been closed.