• Resolved gatestar

    (@gatestar)


    Hi I have a timesheet form that has fields with dropdowns for times so like 08:00am 80:30am and so on. This is saved in a custom table, can then get the plugin to read this, but it pulls only the data into the field and overwrites the list, can I get it to select the appropriate value that has been saved? So if the person selected 08:00am that would be selected but the user can change this and select from the other options.

    Thanks

Viewing 1 replies (of 1 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @gatestar

    Thank you very much for using our plugin. In this case, you don’t want to fill the field with the values stored in the database table. You want to select the value from the existing dropdown field. A possible alternative is the use of callbacks.

    I’ll try to describe the process with an example.

    Assuming I have the dropdown field posts with some posts ids as their choices:

    [select posts "--SELECT--" "22" "23"]

    Now, I want to read the list of published posts from the database and select the first choice that matches any of the posts read from the database. I insert the record-set tag to read the posts ids from the database and include the callback argument:

    [cf7-recordset id="posts-data" type="post" attributes="ID" condition="post_type='post' AND post_status='publish'" callback="select_option"]

    Finally, I implement the callback function in the form content:

    <script>
    function select_option(records) {
      var e;
      for(var i in records){
        e = jQuery('[name="posts"] option[value="'+records[i]['ID']+'"]');
        if( e.length ){ e.prop('selected', true); return;}
      }
    }
    </script>

    The complete form structure would be:

    <label> Posts Ids
        [select posts "--SELECT--" "22" "23"] </label>
    
    [cf7-recordset id="posts-data" type="post" attributes="ID" condition="post_type='post' AND post_status='publish'" callback="select_option"]
    
    <script>
    function select_option(records) {
      var e;
      for(var i in records){
        e = jQuery('[name="posts"] option[value="'+records[i]['ID']+'"]');
        if( e.length ){ e.prop('selected', true); return;}
      }
    }
    </script>
    

    Now, you only should modify your record-set tag to include the callback argument and implement the callback function adjusted to your project needs.

    Best regards.

Viewing 1 replies (of 1 total)

The topic ‘Drop Down Select’ is closed to new replies.