SELECT populate from Mysql

SELECT populate from Mysql

hdsahdsa Posts: 7Questions: 0Answers: 0
edited October 2013 in Editor
How can I populate a Select Box from a MySQL Source instex of static information.
Ex:
When I create a new record, the editor shows the varous fields including select elements. What I need is to fill the select options with database information rather than static one.

Best regards.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    You can use the field's `update()` method for this:

    [code]
    editor.field('dept.id').update( json.dept );
    [/code]

    There is an example here:
    http://editor.datatables.net/release/DataTables/extras/Editor/examples/join.html

    Allan
  • astreiffastreiff Posts: 2Questions: 0Answers: 0
    Allan-

    Is there a way to extend this if you want to query from the dept table? As an example, you have a field called "status_id" in the table and you only want to include options where status_id=1. If I can't use this method for loading the select options and need to load via html, how do you set the "selected" option for an update.
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Sorry, I'm a little confused. The values for the select list aren't available in your Ajax data source - is that correct? So you are wanting to update the select list with options from the DOM? Where are those options in the DOM?

    Allan
  • astreiffastreiff Posts: 2Questions: 0Answers: 0
    Sorry for the confusion. I can go either way. So far, I've populated using DOM. But, I'm wondering if there is a way to use the field's update() method you referenced above or some other method with an extra wrinkle of adding filter on another value in the table. So if the dept table above also had a column called "status_id", is there a way to populate the select options for dept with only records where the status_id=1?
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Sure - you could use the `change` event emitted by he other field:

    [code]
    $( editor.node( 'status_id' ) ).on( 'change', function () {
    // Get and modify list of options
    // ...

    editor.field( 'dept.id').update( data );
    } );
    [/code]

    Is that the kind of thing you are after?

    Allan
This discussion has been closed.