2013-05-17 3 views
1

Я пытаюсь заполнить поле выбора, основываясь на предыдущем выберите значение в окне Laravel 4. Вот что я до сих пор:Выберите Cascade с помощью JQuery и PHP в Laravel 4

Моих JS:

var url = document.location.hostname + '/cream/public/list-contacts'; 

var contacts; 

$.ajax({ 
    async: false, 
    type: 'GET', 
    url: url, 
    dataType: 'json', 
    success : function(data) { contacts = data; } 
}); 

$('#account_id').change(function() { 
    alert(url); 
    label = "<label class='control-label'>Contacts</label>"; 
    select = $("<select name='contact_id[]' id='contact_id'>"); 
    console.log(contacts); 

    for(var i in contacts) { 
     alert(contacts[i]['account_id']); 
     if(contacts[i]['account_id'] == $(this).val()) { 
      select.append('<option value="' + contacts[i]['id'] + '">' + contacts[i]['name'] + '</option>'); 
     } 
    } 

    $('.contacts').html(select).prepend(label); 
}); 

мой список-контакты маршрут декларация:

Route::get('list-contacts', '[email protected]'); 

Мои контакты() в моем ContactListController:

public function contacts() 
{ 
    return Contact::select('contacts.id', 'contacts.account_id', DB::raw('concat(contacts.first_name," ",contacts.last_name) AS name'))->get()->toArray(); 
} 

Форма на мой взгляд:

{{ Form::open(array('action' => '[email protected]', 'class' => 'view-only pull-left form-inline')) }} 
    {{ Form::label('account_id', 'Account', array('class' => 'control-label')) }} 
    {{ Form::select('account_id', $accounts) }} 
    <div class="contacts"></div> 
    {{ Form::label('delegate_status_id', 'Status', array('class' => 'control-label')) }} 
    {{ Form::select('delegate_status_id', $delegate_statuses) }} 
    {{ Form::label('price', 'Price', array('class' => 'control-label')) }} 
    {{ Form::text('price', '', array('class' => 'input-small')) }} 
    {{ Form::hidden('event_id', $event->id) }} 
    {{ Form::submit('Add Delegate', array('class' => 'btn btn-success')) }} 
{{ Form::close() }} 

EDIT: Я изменил мой код, указанный выше. Когда я посещаю/list-contacts, он получает нужные мне данные, это просто не присваивает эти данные переменной-контакту в моем запросе AJAX в моем JS? Любая помощь будет оценена по достоинству.

Ошибка: Это ошибка, которая отображается в моем журнале консоли для контактов переменных:

файла: «/ Applications/MAMP/HTDOCS/крем/поставщик/Laravel/рамки/SRC/Осветите/Маршрутизация/Контроллеры/controller.php» линии: 290 сообщения: "" типа: "Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException"

ответ

1

теперь у меня эта работа. Это связано с созданным URL-адресом в запросе AJAX. Я удалил document.location.hostname и жестко закодировал URL-адрес без localhost.

Вот рабочий код для интересующихся:

My JS:

var url = '/cream/public/list-contacts'; 

var contacts; 

$.ajax({ 
    async: false, 
    type: 'GET', 
    url: url, 
    dataType: 'json', 
    success : function(data) { contacts = data; } 
}); 

$('#account_id').change(function() { 
    select = $("<select name='contact_id' id='contact_id'>"); 

    for(var i in contacts) { 
     if(contacts[i]['account_id'] == $(this).val()) { 
      select.append('<option value="' + contacts[i]['id'] + '">' + contacts[i]['name'] + '</option>'); 
     } 
    } 

    $('.delegates .contacts').show(); 

    $('.delegates .contacts .controls').html(select); 
}); 

Мой список-контакты маршрут декларация:

Route::get('list-contacts', '[email protected]'); 

Мои контакты() метод в моем ContactListController:

public function contacts() 
{ 
    return Contact::select('contacts.id', 'contacts.account_id', DB::raw('concat(contacts.first_name," ",contacts.last_name) AS name'))->get(); 
} 

Форма на мой взгляд:

{{ Form::open(array('action' => '[email protected]', 'class' => 'delegates pull-left form-horizontal add-delegate')) }} 
    <div class="control-group"> 
     {{ Form::label('account_id', 'Account', array('class' => 'control-label')) }} 
     <div class="controls"> 
      {{ Form::select('account_id', $accounts) }} 
     </div> 
    </div> 
    <div class="control-group contacts"> 
     {{ Form::label('contact_id', 'Contacts', array('class' => 'control-label')) }} 
     <div class="controls"> 

     </div> 
    </div> 
    <div class="control-group"> 
    {{ Form::label('delegate_status_id', 'Status', array('class' => 'control-label')) }} 
     <div class="controls"> 
      {{ Form::select('delegate_status_id', $delegate_statuses) }} 
     </div> 
    </div> 
    <div class="control-group"> 
     {{ Form::label('price', 'Price', array('class' => 'control-label')) }} 
     <div class="controls"> 
      {{ Form::text('price', '', array('class' => 'input-small')) }} 
     </div> 
    </div> 
    {{ Form::hidden('event_id', $event->id) }} 
{{ Form::close() }} 
+0

Я пытаюсь реализовать решение, как у вас, но я изо всех сил, на мой вопрос размещен здесь: http://stackoverflow.com/questions/22273453/how-to -create-a-chain-select-box-in-laravel-4, я видел это после того, как я разместил свой вопрос, но я не уверен, как реализовать свое решение. вы можете помочь? – 001221