Laravel动态数据填充在bootstrap模式中

时间:2017-12-31 07:10:11

标签: php jquery twitter-bootstrap laravel laravel-5

我的引导模式我放在仪表板中。按钮将填充我放置在另一页面中的模态,并在那里我扩展仪表板。 当我点击不同的产品ID按钮时,产品相关信息应该以模态显示。我的问题是我如何让它变得动态。表示将根据我单击以快速查看的产品填充引导模式。

**button for quick view in index.blade.php :**

<div class="btn-quickview"> 
    <a href="#" data-toggle="modal" data-target="#modal-quickview">
        <i class="fa fa-search-plus" aria-hidden="true">
        </i> Quick View
    </a> 
</div>

  **Modal placed in dashboard.blade.php**

<div id="modal-quickview" class="modal fade" role="dialog">
    <div class="modal-dialog">
      <div class="modal-body">
        <button type="button" class="close myclose" data-dismiss="modal">×</button>
        <div class="product-view-area">         
          <div class="col-xs-12 col-sm-7 col-lg-7 col-md-7 product-details-area">
            <div class="product-name">
              <h2>Lorem Ipsum is simply</h2>
            </div>
            <div class="price-box">
              <p class="special-price"> <span class="price-label">Special Price</span> <span class="price"> $329.99 </span> </p>
              <p class="old-price"> <span class="price-label">Regular Price:</span> <span class="price"> $359.99 </span> </p>
            </div>

          </div>
        </div>
      </div>
      <div class="modal-footer"> <a href="#" class="btn-services-shop-now" data-dismiss="modal">Close</a> </div>
    </div>
  </div>
</div>

3 个答案:

答案 0 :(得分:2)

您可以通过jQuery ajax

加载bootstrap模式的内容
<div id="view-modal" class="modal fade"  
    tabindex="-1" role="dialog" 
    aria-labelledby="myModalLabel" 
    aria-hidden="true" style="display: none;">
     <div class="modal-dialog"> 
          <div class="modal-content"> 

               <div class="modal-header"> 
                    <button type="button" class="close" 
                        data-dismiss="modal" 
                        aria-hidden="true">
                        ×
                     </button> 
                    <h4 class="modal-title">
                        <i class="glyphicon glyphicon-user"></i> User Profile
                    </h4> 
               </div> 
               <div class="modal-body"> 

                   <div id="modal-loader" 
                        style="display: none; text-align: center;">
                    <img src="ajax-loader.gif">
                   </div>

                   <!-- content will be load here -->                          
                   <div id="dynamic-content"></div>

                </div> 
                <div class="modal-footer"> 
                      <button type="button" 
                          class="btn btn-default" 
                          data-dismiss="modal">
                          Close
                      </button>  
                </div> 

         </div> 
      </div>
</div><!-- /.modal -->   

<button data-toggle="modal" data-target="#view-modal" 
    id="getUser" class="btn btn-sm btn-info"
    data-url="{{ route('dynamicModal',['id'=>$id])}}"
    >
        Submit
</button>

<script>
$(document).ready(function(){

    $(document).on('click', '#getUser', function(e){

        e.preventDefault();

        var url = $(this).data('url');

        $('#dynamic-content').html(''); // leave it blank before ajax call
        $('#modal-loader').show();      // load ajax loader

        $.ajax({
            url: url,
            type: 'GET',
            dataType: 'html'
        })
        .done(function(data){
            console.log(data);  
            $('#dynamic-content').html('');    
            $('#dynamic-content').html(data); // load response 
            $('#modal-loader').hide();        // hide ajax loader   
        })
        .fail(function(){
            $('#dynamic-content').html('<i class="glyphicon glyphicon-info-sign"></i> Something went wrong, Please try again...');
            $('#modal-loader').hide();
        });

    });

});

</script>

在路线档案中

Route::get('dynamicModal/{id}',[
    'as'=>'dynamicModal',
    'uses'=> 'ControllerName@loadModal'
]);

在控制器中

public function loadModal($id)
{
    // write your process if any
    return view('view_having_model_body',['data'=>$ifAnyData]);
}

答案 1 :(得分:1)

动态执行此操作的另一种方法是,将您的产品ID添加到数据目标,例如

<a href="#"
  data-title="Lorem Ipsum is simply"
  data-old-price="$359.99"
  data-special-price="$329.99"
  data-toggle="modal"
  data-target="#modal-quickview{{$product->id}}"
>

对模态主体ID做同样的事情

<div id="modal-quickview{{$product->id}}"
  class="modal fade"  
  tabindex="-1"
  role="dialog" 
  aria-labelledby="myModalLabel" 
  aria-hidden="true"
  style="display: none;"
>
  <div class="modal-dialog"> 
    <div class="modal-content"> 
      <div class="modal-header">

在这种情况下,laravel为每个模态生成唯一的ID。在模态主体中,您只需要根据其ID显示产品

答案 2 :(得分:0)

您可以在锚点中添加所需的数据值,并将值附加到模态弹出窗口。你可以这样做:

<a href="#" data-title="Lorem Ipsum is simply" data-old-price="$359.99" data-special-price="$329.99" data-toggle="modal" data-target="#modal-quickview">
<i class="fa fa-search-plus" aria-hidden="true">
    </i> Quick View
</a>

<script>
    $(function(){
        $('.btn-quickview').on('click','a',function(ev){
            $('.product-name').find('h2').html($(this).attr('data-title'))
            $('.special-price').find('.price').html($(this).attr('data-special-price'))
            $('.old-price').find('.price').html($(this).attr('data-old-price'))
        })
    })

</script>

如果您不想将数据属性附加到锚点,则可以通过Ajax获取数据并适当设置值;)