scruffycoder86
scruffycoder86

Reputation: 549

Laravel dropdown without optgroup

I decided to try out Laravel and wanted to create dropdowns for my form. I created a convinience method inside my BaseController that I use to get the data from the database. Below is the function listing:

protected function getList( $model, array $fields, $empty = 'Select option  below' )
{
    $options = array();

    $owner = strtolower( $model ) . 's';

    $records = $model::lists($fields[1], $fields[0]);

    if( !empty( $records ) ){

        foreach( $records as $key => $value ){

            $options[$owner][$value] = $key;
        }
    }


    $options[$owner][''] = $empty;

    return $options;
}

And then in the controller you can just use it like:

//Get groups
$groups = $this->getList( 'Group', array(
    'name', 'id'
  ));

//Get Project Managers  
$project_managers = $this->getList( 'ProjectManager', array(
  'name', 'id'
 ));

The output of the select form control has an optgroup that references the owner of the list or the model rather. How can I remove the optgroup such that its not part of the contents of the dropdown?

Below is the output of the form:

<div class="input select">
  <label for="project_manager">Project Manager</label>
  <select name="project_manager_id">
    <optgroup label="projectmanagers">
      <option value="1">Riyaadh</option>
      <option value="2">Luyanda</option>
      <option selected="selected" value="">Select option below</option>
    </optgroup>
  </select>
</div>

Upvotes: 0

Views: 1952

Answers (4)

Matt Burrow
Matt Burrow

Reputation: 11067

You are adding another level within your getLists method with this line; $options[$owner][$value] = $key;, this is what is adding your optgroup, to get rid of this change the line to $options[$value] = $key;.

Optionally you can reduce the code of your getLists function to the following;

protected function getList( $model, array $fields, $empty = 'Select option  below' )
{
    $options = array();

    $owner = strtolower( $model ) . 's';

    $records = $model::lists($fields[1], $fields[0]);

    if( !empty( $records ) ){

        $options = array_flip($records);
    }


    $options[''] = $empty;

    return $options;
}

This uses the array_flip method, which exchanges the keys as values and values as keys.

Upvotes: 3

scruffycoder86
scruffycoder86

Reputation: 549

As Matt Burrow suggested it turned out that I was creating a multi-dimentional array which is translated to a list with optgroup.

I modified my function to rather exclude the owner which the records belong to. Below is my perfectly working function (excerpt):

 ...
 if( !empty( $records ) ){

    foreach( $records as $key => $value ){

       $options[$value] = $key;
    }
 }

 $options[''] = $empty;
 ...

Upvotes: -3

Deenadhayalan Manoharan
Deenadhayalan Manoharan

Reputation: 5444

Try this...

 $SchoolDetails = GeneralSettingModel::lists('SchoolName', 'id');

or

$SchoolDetails = DB::table('tablename')->lists('SchoolName', 'id');


    {{ Form::select('SchoolName',array(''=>'Select School')+$SchoolDetails,null, array('id'=> 'SchoolName'))}}

Output:

    <select id="SchoolName" name="SchoolName">
<option value="" selected="selected">Select School</option>
<option value="1">test</option>
<option value="2">test355wrew</option>
<option value="3">GOVT</option>
<option value="4">Kumaraguru</option>
<option value="5">valavan</option>
</select>

Upvotes: 1

kamlesh.bar
kamlesh.bar

Reputation: 1804

you can remove using JQuery by adding ID to select

 $(function () {
     $("#mySelect").children().remove("optgroup");
 });

Upvotes: -1

Related Questions