Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic content for model popup based on link selected

I have an array of Accept Reject button. if a user clicks on these buttons separate model popup will show. Accept and reject button link has separate data-id and data-action.

My aim to write a single javascript function to load the content of the model popup instead of repeating the code of modal.

ERB code

<% @non_claim_items.each do |damage_item| %>
  <tr>
    <td>
      <div class="input-prepend">
        <span class="add-on"><%= damage_item.estimated_total_repair_cost.currency %></span>
        <span class="uneditable-input input-small currency-format"><%= damage_item.estimated_total_repair_cost %></span>
      </div>
    </td>
    <td>
      <a data-toggle="modal" data-target="#acceptModel" data-id="<%= damage_item.id %>" data-action = 'accept' class="btn btn-small btn-primary">Accept</a>
      <a data-toggle="modal" data-target="#rejectModel" data-id="<%= damage_item.id %>" data-action = 'discuss' class="btn btn-small btn-default">Discuss</a>
    </td>

  </tr>
<% end %>

<div id="acceptModel" class="modal fade hide" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;</button>
    <h4><%= t('headings.damage_item.accept_damage') %></h4>
  </div>
  <div class="modal-body" style="max-height: 500px;">

  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-primary" ><%= t('navigation.buttons.confirm') %></button>
    <button type="button" class="btn btn-default" data-dismiss="modal"><%= t('navigation.buttons.cancel') %></button>
  </div>
</div>
</div>

Against each item have one accept/discuss button, data_id and data action are data parameter to model pop.

Script

<script type="text/javascript">
      var damage_items = '<%= @non_claim_items.only(:id, :estimated_total_repair_cost, :damage_location_name ).to_json.html_safe %>';
      $('a[data-toggle=modal]').click(function () {
          if (typeof $(this).data('id') !== 'undefined') {

              data_id = $(this).data('id');
              action = $(this).data('action');
              setModelContent($(this), action, data_id)
          }
      });

      function setModelContent(modal, action, data_id) {
          if( action == 'accept')
          {
            // based on action need to set  the body of model pop up
          }
      }

</script>
  1. I need help to write a javascript function that can set the body of model popup as per action.

  2. Based on data_id, need to pick the corresponding data from the damage_items javascript variable, then data stored in the jquery hash need to show in the model popup body.

like image 209
Anand Jose Avatar asked Aug 17 '18 13:08

Anand Jose


People also ask

How do I pass data to modal popup?

Data can be passed to the modal body from the HTML document which gets displayed when the modal pops up. To pass data into the modal body jquery methods are used. jQuery is similar to JavaScript, however jQuery methods are simple and easier to implement. jQuery reduces the lines of code.

How do you trigger the modal by data attributes?

To trigger the modal window, you need to use a button or a link. Then include the two data-* attributes: data-toggle="modal" opens the modal window. data-target="#myModal" points to the id of the modal.

How do you create a dynamic modal?

const modal = document. createElement('div'); modal. classList. add('modal'); modal.id = 'modal'; modal.

How do I open modal popup automatically?

You can use the Bootstrap . modal('show') method for launching the modal window automatically when page load without clicking anything. A common example of this technique is loading the modal when user landed on the home page and requesting them to subscribe the website newsletter.


1 Answers

You can do it like this:

function setModelContent(modal, action, data_id) {

  if (action == 'accept') {
    // based on action need to set  the body of model pop up
  }

  // Get damage item by data_id
  let damage_item = Object.entries(damage_items).filter(item => item[1].id == data_id);

  // Creating dynamically bootstrap elements and setting value of inputs by damage_item
  let fisrtLabel = $(document.createElement('label')).text('Cost:');
  let fistInput = $(document.createElement('input')).addClass('form-control').val(damage_item[0][1].estimated_total_repair_cost);

  let firstCol6 = $(document.createElement('div')).addClass('col-sm-6')
    .append(fisrtLabel)
    .append(fistInput);

  let secondLabel = $(document.createElement('label')).text('location name:');
  let secondInput = $(document.createElement('input')).addClass('form-control').val(damage_item[0][1].damage_location_name);

  let secondCol6 = $(document.createElement('div')).addClass('col-sm-6')
    .append(secondLabel)
    .append(secondInput);


  let formGroup = $(document.createElement('div'))
    .addClass('form-group')
    .append(firstCol6)
    .append(secondCol6);

  // Clearing modal-body and filling it by new elements
  $('#acceptModel').find('.modal-body').html("").append(formGroup);

}

Online demo (jsFiddle)

like image 185
Ali Soltani Avatar answered Nov 09 '22 15:11

Ali Soltani