Abdullah Faraz
Abdullah Faraz

Reputation: 35

Dropzone isn't working on Modal Showing through ajax

Dropzone and Javascript isn't working on bootstrap Modal i'm calling Modal through ajax.Because i have hundreds of records i don't want to place Modal in foreach so i used ajax on button click controller method hits and return a new view and that view i'm calling and main view where modal will be displayed. Modal is working fine but i have used dropzone plugin which isn't working and even no other js code is working neither css.

MainView.blade.php On button Click the following script hits this the my main view where i am calling Bootstrap Modal

<div class="modal" id="Edit-SpecsModal-products">
      <div class="modal-dialog modal-max ">
         <div class="modal-content">
            <!-- Modal Header -->
            <!-- Modal body -->
            <div class="modal-body text-center">
               <span class="f-24 black bold">Edit Product</span>
               <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <div class="modal-body p0">
               <div class="product_details" id="product_details"></div>

            </div>
            <!-- Modal footer -->
            <div class="modal-footer">
               <button type="button" class="btn-bg2" data-dismiss="modal">Cancel</button>
               <input type="button" class="btn-bg1" data-dismiss="modal" onclick="submitForms()">Add to Project</button>
            </div>
         </div>
      </div>
   </div>

<script>

     $(document).ready(function(){  
      $('.edit_product_model').click(function(){  
           var product_id = $(this).attr("id");  
           $.ajax({  
                url:"{{url('architecture-edit-product')}}/" + product_id ,  
                method:"post",  
                headers: {
                    'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
                },
                data:{product_id:product_id},  
                success:function(data){  
                     $('#product_details').html(data.html);  
                     $('#Edit-SpecsModal-products').modal("show");  
                }  
           });  
      });  
 });  
    </script>

Controller

public function editArchitectureProductSave(Request $request, $id){

    $data['products'] = Product::with('productImages')->where('id',$id)->get();
    $returnHTML = view('manufacturer::projects.includes.ajaxhtmlviews.edit-product-Bootstrap-modal', $data)->render();

    return response()->json( ['html'=> $returnHTML], 200);

edit-product-Bootstrap-modal.blade.php

    <div class="max-wid">
       <div class="pl-3 pr-3 mb-5 p0">
          <div class="">
             <div class=" f-column in">
                <div class="">
                   <ul class="nav nav-tabs nav-border-producttab">
                      <li class="nav-item ">
                         <a class="nav-link active" data-toggle="tab" href="#add_new_products2">Add New Product</a>
                      </li>
                   </ul>
                </div>
                <div class="">
                   <div >
                      <div class=" mt-3">
                         <!-- Nav tabs -->
                         <!-- Tab panes -->
                         <div class="tab-content model-hit">
                            <div id="add_new_products2" class="container tab-pane active">
                               <br>
                               <div class=" row">
                                  <div class="col-sm-6">

                                     <div class="col-sm-12 p-0 mrg_modal_produt">
                                        <div class="col-sm-12 p-0 label_modal_product">
                                           Product Documents
                                           <div class="clearfix"></div>
                                           <div class="gry f-13">(Specs, CHPS Certificate, Product Data Sheet)</div>
                                        </div>
                                        <div class="my-form1 text-center" id="my-for2">
                                           <?php echo Form::open(array('route' => 'architecture-product-file-save', 'files' => true, 'method' => 'PUT', 'id' => 'product-file-form', 'class' => 'dropzone', 'name' => 'formName')); ?>
                                           <input type="hidden" name="last_product_inserted_id" id="last_product_inserted_id" value="">
                                           <div class="col-sm-12 p-0 mrg_modal_produt">
                                              <div id="preview-template" style="display: none;">
                                                 <div class="dz-preview dz-file-preview" style="width: 20% ;">
                                                    <div class="dz-image"><img data-dz-thumbnail /></div>
                                                    <div class="dz-details">
                                                       <div class="dz-size"><span data-dz-size></span></div>
                                                       <div class="dz-filename"><span data-dz-name></span></div>
                                                    </div>
                                                    <div class="dz-progress"><span class="dz-upload" data-dz-uploadprogress></span></div>
                                                    <div class="dz-error-message"><span data-dz-errormessage></span></div>
                                                    <div class="dz-file-type">
                                                       <br />
                                                       <select name="file_type" class="dz-file-type-select" id="file_type">
                                                          <option value="">Select File Type</option>
                                                          <option value="datasheet_file">Datasheet</option>
                                                          <option value="cad_file">CAD file</option>
                                                          <option value="bim_file">BIM file</option>
                                                          <option value="leed_file">LEED Statement</option>
                                                       </select>
                                                    </div>
                                                 </div>
                                              </div>
                                           </div>

                                           </form>
                                        </div>
                                     </div>
                                     <div class="col-sm-12 p-0 mrg_modal_produt">
                                        <div class="col-sm-12 p-0 label_modal_product">Product Images</div>
                                        <div class="my-form1 text-center" id="my-for3">


                                           <input form="product-form" id="file-input" name="product_images[]" type="file" multiple>
                                           <div id="preview"></div>

                                        </div>
                                     </div>
                                  </div>
                                  <div class="col-sm-6">
                                     <?php echo Form::open(array('route' => 'architecture-product-save', 'files' => true, 'id' => 'product-form')) ?>
                                     <input type="hidden" name="method_type" value="PUT">
                                     <input type="hidden" name="id" value="{{$products['0']->id}}">
                                     <div class="col-sm-12 p-0 mrg_modal_produt">

                                     <input name="name" type="text" class="form-control input_product_modal" value="{{$products['0']->name}}">
                                        <input name="" type="text" class="form-control input_product_modal" value="Manufacturer Name">
                                     <input type="hidden" name="project_slug" value="{{Request::segment(2)}}">
                                    <textarea name="description" class="form-control input_product_modal" cols="" rows="">{{$products['0']->description}}</textarea>
                                        <input name="leed_file_url" type="text" class="form-control input_product_modal" value="{{$products['0']->leed_file_url}}">
                                     </div>
                                     <div id="product_images_div"></div>
                                     <input type="submit" class="btn btn-info" value="Save" id="save-product-form-submit" style="display:hidden;">
                                     </form>
                                  </div>
                               </div>
                            </div>
                         </div>
                      </div>
                   </div>
                </div>
             </div>
          </div>
       </div>
    </div>  

Code is working fine modal is calling but problem is only when modal from edit-product-Bootstrap-modal.blade.php is called through the script used in mainView.blade.php for Bootstrap Modal is not working

Upvotes: 0

Views: 1283

Answers (2)

Nijeesh Joshy
Nijeesh Joshy

Reputation: 1481

Check if you have disabled autoDiscvoer for dropzone.js on Dropzone.autoDiscover if its false set its value as true (Dropzone.autoDiscover = true;).

If thats also not working try manually initializing the dropzone by attaching it to the model.

   myDropDown = new Dropzone('#product_details', {<options>})

Upvotes: 1

zlatan
zlatan

Reputation: 3951

I had a similar problem with modals, where my datepicker.js input field wouldn't work when I click it, but id didn't show any errors. I did some digging, and apparently, modals don't properly load scripts defined in your view (or wherever). I managed to do this, by initializing the same script again, but with another name, in my app.js, something like this:

$(document).ready(function () {
    $('.datepickerCustom').datepicker({
        //datepicker logic
    }).on('changeDate', function (e) {
        $(this).parent().find('.datePickerInput').val(e);
    });
});

After compiling my views again (webpack), date picker was working fine. There's also another solution. Seems like sometimes js scripts "hide" behind the modal, so you need to set their z-index the modal's z-index which is 1050. This could be done inside your modal view:

<style>
    .datepicker {
      z-index: 1600 !important; /* has to be larger than 1050 */
    }
</style> 

If this still doesn't work, there's actually one more solution, where you need to initiate the date picker on the Modal shown event, something like this:

$('#myModal').on('show.bs.modal', function (e) {
  if (!data) return e.preventDefault() // stops modal from being shown
})

Hope that any of those solutions can solve your problem. Good luck.

Upvotes: 0

Related Questions