Is it possible to pass custom select options to Editor for each row in DataTable?

Is it possible to pass custom select options to Editor for each row in DataTable?

hubindustrialhubindustrial Posts: 3Questions: 2Answers: 0
edited September 2014 in Free community support

I have a field that needs to have different select options in the Editor window, for each row in the DataTable.

Here is a sample of the data structure that I want to use to populate the select options.

"product_uom_collection": [{
    "unit_of_measure": "CS",
    "unit_size": "12.0000",
    "unit_description": "Case"
},
{
    "unit_of_measure": "EA",
    "unit_size": "1.0000",
    "unit_description": "Each"
},
{
    "unit_of_measure": "PC",
    "unit_size": "1.0000",
    "unit_description": "Piece"
}]

Option label should use "unit_description" field, and value should use "unit_of_measure" field.

The "product_uom_collection" array varies per row, so I need to be able to have the options dynamically change to reflect the valid values for each DataTable row.

Please advise if this is possible, and how to achieve.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Answer ✓

    Hi,

    Thanks for the question! Is the product_uom_collection property part of the row data? If so, what you could do is this:

    editor.on( 'initEdit', function ( e, node, data ) {
      var selectOpts = $.map( data.product_uom_collection, function ( o ) {
        return {
          label: o.unit_description,
          value: o.unit_of_measure
        };
      } );
    
      editor.field( 'mySelectFieldName' ).update( selectOpts );
    } );
    

    That uses:

    • The event: initEdit to know when a row is being edited
    • The field method update() - select - to update the options
    • And a little bit of jQuery to get the options into a form that Editor understands

    You might need to do something similar on create, depending on how you want to handle that.

    Regards,
    Allan

  • hubindustrialhubindustrial Posts: 3Questions: 2Answers: 0

    That worked perfectly.
    Thank you. :-)

This discussion has been closed.