How to fill a select2 from a table

How to fill a select2 from a table

rrojas2018rrojas2018 Posts: 8Questions: 3Answers: 0

I need your help please I have the select2 working with static data, but I can not fill it with data from my table, in this tablei need concat the code+name, to show in select

Replies

  • rf1234rf1234 Posts: 2,806Questions: 85Answers: 406

    Your code would be helpful but I try without it:
    a) Javascript field instance (I use selectize but select2 isn't much of a difference):

    fields: [ {
        label: "For:",
        name:  "proposal.govdept_id", //render gov_name, govdept_name
         type: "selectize", 
          opts: {
             create: false,
             maxItems: 1,
             maxOptions: 15,
             openOnFocus: false,
             allowEmptyOption: false,
             placeholder: "Please select a Department"
         }
     }]
    

    b) PHP from the Editor instance:

    Field::inst( 'proposal.govdept_id' )->validator( 'Validate::notEmpty', array('message' => $msg[0]) )
        ->options( Options::inst()
             ->table('govdept, gov')
             ->value('govdept.id')
             ->label( array('gov.name', 'govdept.name', 'gov.type', 'gov.regional_12') )
             ->render( function ( $row ) {               
                    return $row['gov.name'].' / '.$row['govdept.name'].' ('.$row['gov.regional_12'].'); '
                         .renderGovType($row['gov.type']); 
               } )
              ->order( 'gov.name asc' )
                //where clause MUST be a closure function in Options!!!
                ->where( function($q) {
                 //only govs that are already clients can be selected
                  $q ->where( function($r) {
                        $r ->where('gov.is_client', 1 );
                        $r ->where('govdept.gov_id', 'gov.id', '=', false); //join
                  });
             } )
        )
    

    As you can see you can concatenate as many fields as you like on the server side. You can even join multiple tables with the ->options method. But LEFTJOIN does not work in -> options hence I needed to use an old fashioned 1990's style join through the WHERE clause mentioning both tables in the ->table instance. In addition the ->options method only accepts WHERE clauses as closure functions.

  • rrojas2018rrojas2018 Posts: 8Questions: 3Answers: 0

    Thanks rf1234, your code helps me understand many things but the reason I want to use select2, is because I need the search through autocomplete, I'm not sure if the selectize allows you to do that?

  • rf1234rf1234 Posts: 2,806Questions: 85Answers: 406
    edited March 2017

    Selectize also has autocomplete. But keep using Select2. It should work as well. The rendering is done on the server side anyway regardless of what you use on the front end.
    I don't use Select2 because you can't limit the number of options read. In my case it is over 10,000 read from a database which causes performance issues on the front end. Selectize allows me to limit the number of options presented.

    The link to the seletize plug-in for Editor: https://editor.datatables.net/plug-ins/field-type/editor.selectize
    More details regarding selectize usage: https://github.com/selectize/selectize.js/blob/master/docs/usage.md

  • rrojas2018rrojas2018 Posts: 8Questions: 3Answers: 0

    Great I must say that I missed adding the plugin files is exactly what I needed many thanks rf1234

This discussion has been closed.