Hi I have trying to implement a default placeholder in dynamic select which is chained to another select. I am using a repository to query the db to retrieve the users
clients
which is loaded into the first select. The second select is a chained select which has a placeholder, which was easy to implement. Anyway I have tried the following but it loads the incorrect ID's in the value attribute of the select option.
I am fetching the auth users clients and then then the clients projects as so in my repository:
public function getClients()
{
return \Auth::user()->clients()->orderBy('client_name', 'asc')->lists('client_name','id');;
}
public function getClientsProjects($clientId)
{
$client = Client::find($clientId);
if ( ! $client = Client::find($clientId)) { echo 'client not found'; }
//$projects = $projects = \Auth::user()->projects()->where('client_id', $client->id)->orderBy('project_name')->get(array('id','project_name'));
$projects = $projects = Project::where('client_id', $client->id)
->orderBy('project_name')
->get(array('id','project_name'));
$response = array();
foreach($projects as $project){
$response[$project->id] = $project->project_name;
}
return $response;
}
In my task controller I feed this as so:
public function clientsProjects()
{
$clientId = Input::get('option');
return Response::json($this->project->getClientsProjects($clientId));
}
public function create()
{
$tasks = Auth::user()->tasks;
$client_options = $this->project->getClients();
$client_options = array_merge([0 => 'Select a client from the list'], $client_options);
$status = $this->project->getStatus();
$priority = DB::table('priorities')->orderBy('name', 'asc')->lists('name', 'id');
return View::make('tasks.create', array( 'client_options' => $client_options, 'status' => $status, 'priority' => $priority));
}
and in my view i have stripped it back to the basics no styling at all and just jquery and the call to route to my controller function and update the chained select:
@if(count($client_options)>0)
{{ Form::select('client', $client_options, Input::old('client'), array('id' => 'select_client', 'class' => ' tempus_select')) }}
@endif
{{ Form::select('project', array_merge(array('default' => 'Select client first')), 'default', array('class' => 'tempus_select', 'id' => 'project_select')) }}
{{ HTML::script('js/jquery-2.0.3.js'); }}
<script>
$(document).ready(function($){
$('#select_client').change(function(){
$.get("/task/clientsprojects",{
option: $(this).val()
}, function(data) {
console.log(data);
var model = $('#project_select');
model.empty();
$.each(data, function(key, value) {
$('#project_select').append("<option value='"+key+"'>"+value+"</option>'");
});
$("#project_select").trigger("change");
});
});
});
</script>
However the HTML it outputs is the following:
<select id="select_client" class=" tempus_select" name="client">
<option value="0">Select a client from the list</option>
<option value="1">Client 2</option>
<option value="2">Client 1</option>
</select>
However in the db they have the following values
id| client_ name |
1 | Client 1 |
2 | Client 2 |
I've done the following in my getClientsProjects()
function for debugging:
\Log::info('client id = ' . $clientId);
and the following in my create function in the task controller:
\Log::info($client_options);
and they have returned the correct values but, I know if I take this array_merge
out from the code:
$client_options = array_merge([0 => 'Select a client from the list'], $client_options);
Then the values are loaded correctly, so i'm not sure what is going on, why would this cause the value attribute to be incorrectly loaded? Can somebody help me to figure this out, or perhaps specify another way to insert a default value?