2016-08-02 99 views
0

有我這個選擇權在這裏:從選擇中選擇一個項目後打開模型? Laravel

{!! Form::open(['action' => '[email protected]', 'method' => 'post']) !!} 
{!! Form::select('product_id', $products) !!} 
{!! Form::close() !!} 

$產品變量:

$products = Products::lists('name', 'id')->toArray(); 

現在我有一個工作模型。我已經在我的另一個項目中使用了圖像模型。

其它項目的模式,我想用:


打開模型:

@foreach($themes as $thema) 
     <a style="cursor: pointer" data-toggle="modal" data-target="#{{ $thema->id }}"> 
      <span class="text">{{ $thema->thema }}<br></span> 
     </a> 
@endforeach 

的型號:

@foreach($themes as $thema) 
    <div class="modal fade" id="{{ $thema->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
     <div class="modal-dialog" role="document"> 
      <div class="modal-content"> 
       <div class="modal-header"> 

        // not important 

       </div> 
       <div class="modal-body"> 

       // not important 

       </div> 
      </div> 
     </div> 
    </div> 
@endforeach 

我的問題是,我不能」弄清楚如何通過點擊打開模型我已選擇的項目。當我點擊選擇框中的某個項目時,就會出現類似的情況,模型會彈出。

或創建一個獲取product_id的按鈕,並在模型被打開時打開該模型。

我在Javascript中的知識是爲了創建這個,也許有另一種方式,但我還找不到。

感謝您的幫助!

+0

使用這種方法,如果數據在數據庫中增長,最終會在頁面中出現大量模態。爲什麼不用一個模式並在單擊後用數據填充它的內容,可能還需要AJAX調用。 – Michel

+0

我很樂意這樣做,但是我在JS/AJAX方面的技能還不足以實現這一點。 – WellNo

+0

在我看來,AJAX將是最好的解決方案。一個快速而骯髒的方法是在加載頁面時將所有主題傳遞給JavaScript數組,並渲染一個模式。當單擊數據切換鏈接打開模式時,可以使用JavaScript/jQuery更新模式的內容,並根據鏈接中主題的ID將其設置爲正確的主題。 – piscator

回答

0

在像Laravel這樣的MVC framework中,您不應該使用您的模型來設置輸出的格式。這是觀點的工作。

該代碼應該在視圖中,並使用Javascript調用控制器中的方法。你不能用Javascript調用模型。

+0

我認爲他是指bootstrap莫代爾不是雄辯模型 – Michel

+0

是的,米歇爾是對的。我正在談論bootstrap模型:) – WellNo

1
//HTML 

@foreach($variable as $fromController) 
    <tr> 
     <td> <a data-id="{{ $fromController->id }}"></a>{{ $fromController->name }} </td> 
    </tr> 
    @endforeach 

    <div class="modal"> 

    </div> 



    //JAVASCRIPT (AJAX) 
//Jquery required 

<script> 
$('a[data-id]').click(function(event) { 
    var val = $(this).attr('data-id'); 

    $.ajax({ 
     url: '/getdata', 
     type: 'POST', 
     dataType: 'json', 
     data: {id: val}, 
     success: function(data){ 
      $('.modal').html(data['value']).modal('show'); 
     } 
    }); 


    return false; 
}); 
</script> 


//Route 

Route::post('/getdata', '[email protected]'); 

//Controller 

public function getData(Request $req){ 
    $get = Model::where('id', $req->input('id'))->first(); 
    //Assuming you have a column named "description" 

    return response()->json(['value' => $get->description]); 
} 

希望你能從中得到啓發。這樣你只有一個模式。

+0

我會試試看,謝謝:) – WellNo

+0

描述會是什麼?我沒有那樣的專欄。所以我可以這樣寫:return response() - > json(['value'=> $ get]); – WellNo

+0

我說的假設。因此,描述將是您希望在模式中顯示其值的列的名稱 – Michel

相關問題