douguai4653 2014-09-04 20:22
浏览 55
已采纳

laravel在select选项中错误地将id加载到value属性中

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?

  • 写回答

1条回答 默认 最新

  • dtv7174 2014-09-04 20:34
    关注

    You have to do:

    $client_options = [0=>'Select a client from the list'] + $client_options;
    

    Instead of the merge.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题