2

I am mixing a rails form_for helper with datatables that uses it's own HTML coded button, outside of form_for, to select and deselect records. However, if I click 'Select All' button atop the datatable, the page is automatically refreshed and any selection in the table will be lost:

    <%= f.label :call_order %>
    <%= f.check_box :call_order %>

    **Datatables implementation**
    <button id="selectAllRestaurantsForCampaign">Select All</button>
    <button id="deSelectAllRestaurantsForCampaign">Deselect All</button>
    <table id="restaurantsForCampaign" class="display" cellspacing="0" width="100%">
         <... Table Code ...>
    </table> 

    <hr>
    <strong>Campaign Active</strong>

    **Final attribute in form_for**
    <%= f.check_box :is_active %>
    <br>

    <%= f.submit "Save changes", class: "btn btn-large btn-primary" %>

I want to prevent the page from refreshing until the person clicks the form_for submit button at the bottom of the page:

<%= f.submit "Save changes", class: "btn btn-large btn-primary" %>

The present CoffeeScript for the DeSelect and Select All buttons is:

 # Conditional 'Select All' (works)
  $("#selectAllRestaurantsForCampaign").click ->
    table.$('tr', {"filter":"applied"}).addClass "selected"

  # Deselecting all (works)
   $("#deSelectAllRestaurantsForCampaign").click ->
    table.$("tr").removeClass "selected"

How can I prevent the page from refreshing until submit is clicked?

3 Answers 3

4

If I just have the tag <button></button> then it actually acts as a submit thus refreshing the page.

What I should have done is set it to <button type="button"></button> so that the page won't refresh but still allow for functions and dynamic content to work with the button submission

Sign up to request clarification or add additional context in comments.

Comments

0

assign an onclick() action on the button and in the application.js create the function with preventdefault which will stop the submit,

When you want to submit just trigger the submit() method

2 Comments

Can you provide an example?
Can you please provide an example?
-1

This will prevent the 'SelectAll' from submitting the form:

$('#selectAllRestaurantsForCampaign').click(function(e){
     e.preventDefault(); 
    ... your code to select all
});

2 Comments

can you write this for coffeescript?
This did not work, all it did was prevent the buttons from performing any action at all, yet it still refreshed the page.

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.