1

In rails5, I have the following in the index page:

<tbody>
  <% @properties_payments.each do |properties_payment| %>
    <tr>
      <td><%= properties_payment.property_id %></td>
      :
      :
      <td>
        <a class="property-update"  data-toggle="modal" data-
     target="#property-modal" data-attr='<%= properties_payment.id %>'>Update 
     Payment Transcation</a>
      </td>
    </tr>
  <% end %>
</tbody>

I have a modal header:

<div class="modal fade" id="property-modal" role="dialog" tabindex="-1">
  <div class="modal-dialog">
    <div class="modal-content"> 
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Property Payment Update</h4>
      </div>
      <div class="modal-body" id="test"></div>
      <div class="modal-footer">
        <button type="submit" class="btn btn-default property-submit-status">Update</button>
       </div>
     </div>
   </div>
 </div>

Now using a script tag:

<script>
  $(document).ready(function (e) {
    $('.property-submit-status').on('click', function(e) {
      var payment_id =  $('#property-update').data('attr');
      e.preventDefault();
      e.stopPropagation();
      var postdata = { payment_id:payment_id };

      $.ajax({
        type: 'post',
        url: '/admin/properties/payments/update_transaction',
        data: postdata,
      }).done(function (data, textStatus, jqXHR) {
        $('.result').html(data);
      }).fail(function (jqXHR, textStatus, errorThrown) {
        setNotification('error', 'Unknown Error. Try Again');
      });
    })
  });
</script>

I am not able to fetch the value of data-attr i.e properties_payment.id in var payment_id. How do I achieve this? Newbie to JavaScript.

0

3 Answers 3

1

The issue is that your php loop creates multiple

a.property-update

so you can't simply use $('a.property-update').data('attr'); as you'll always get the first link, not the one clicked on.

You can 'remember' which link was clicked with:

$('#property-modal').on('show.bs.modal', function (e) {
    $('#property-modal').data("link", $(e.relatedTarget));
});

then in your submit handler:

$(document).on("click", ".property-submit-status", function(e) {
    var payment_id = $($('#property-modal').data("link")).data('attr');

Edit: additional info:

<a class="property-update" data-toggle="modal" data-target="#property-modal"
   data-payment-id='<%= properties_payment.id %>'
   data-bill-id='<%= properties_payment.bill_id %>'
   data-customer-inarreas='<%= properties_payment.customer_overdue %>'>
    Update Payment Transcation
</a>

read the same way, using a variable to improve efficiency:

$(document).on("click", ".property-submit-status", function(e) {
    var button = $($('#property-modal').data("link"));
    var payment_id = button.data('payment-id');
    var showOverdueWarning = button.data('customer-inarreas') == 'true';

    load_bill(button.data("bill-id"));
});
Sign up to request clarification or add additional context in comments.

1 Comment

Freedomn-m Ok...Thanks for the update. I am using Ruby loop here. what if I need to use multiple data attributes. Like for example along with data-attr, I want to use data-bill-id. Could you show me an example please with the same solution which you gave
0

The issue is due to wrong selector for data.

You may pls try

$('a.property-update').data('attr');

Comments

0

Please replace your code as this,

<td>
 <a class="property-update"  data-toggle="modal" data-target="#property-modal" data-property='<%= properties_payment.id%>'>Update 
     Paym

And in the js method,

$('.property-update').attr("data-property")

property-update is a class and not an ID in the html view so use '.' instead of '#'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.