[Rails 6] cocoon_ Add id and data attributes to the form to be added

Development environment

table of contents __1) How to install cocoon 2) Dynamically add forms * Chikajika will be UP 3) Add id and data attributes to the form to be added __

3) Add id and data attributes to the form to be added

__ I think it's difficult to read my class and id name as they are, so I use A, B, and C for easy understanding. __ I'm thinking of posting a GIF image if I feel like it soon.

$(function() {
  //Don't forget the definition first.
  let index = 1;
  //I didn't know which class to take, so I tried it many times.(.onegai)Is an expression of that heart.
  //As a result, in my case"Button to add"When"The child you want to add"I created a div that encloses all of them and gave them a class.
  $('.onegai')
     // 'cocoon:after-insert'This event is the key. There are other events that are executed before insertion and before / after deletion, so if you are interested, please see the official website.
    .on('cocoon:after-insert', function(e, insertedItem) {
      //The data attribute is added to the inserted item. When you want to pass the id.Please change after attr.
      //Example).attr('id','index') **By the way, index is a variable, so anything is OK.
      $(insertedItem).find('.class-a').attr('data-a', index);
      $(insertedItem).find('.class-b').attr('data-b', index);
      $(insertedItem).find('.class-c').attr('data-c', index);
      //I'm adding one by one here.
      index = index+1

      //If you want to raise an event using the given data attribute, you can write it here.
      //This time, when a change occurs in A, it is an event to put a value based on A in the text of B and C.
      $('.class-a').on('change',function(e){
      //dataA index(number)Is getting.
        const dataA = $(this).attr('data-a')
        
        const dataB = $("option:selected", this).data("b");
        const dataC = $("option:selected", this).data("c");
        
        $(`[data-b="${dataA}"]`).text(dataB)
        $(`[data-c="${dataA}"]`).text(dataC)
      })
    })
})

__ ↓ ↓ ↓ It was a version with the explanation deleted because it was difficult to see because the explanation was described too much. If you can understand by just looking at the code, click here __

$(function() {
  //Don't forget the definition first.
  let index = 1;
  $('.onegai')
    .on('cocoon:after-insert', function(e, insertedItem) {
      $(insertedItem).find('.class-a').attr('data-a', index);
      $(insertedItem).find('.class-b').attr('data-b', index);
      $(insertedItem).find('.class-c').attr('data-c', index);
      index = index+1

      $('.class-a').on('change',function(e){
        const dataA = $(this).attr('data-a')
        
        const dataB = $("option:selected", this).data("b");
        const dataC = $("option:selected", this).data("c");
        
        $(`[data-b="${dataA}"]`).text(dataB)
        $(`[data-c="${dataA}"]`).text(dataC)
      })
    })
})

that's all.

Recommended Posts

[Rails 6] cocoon_ Add id and data attributes to the form to be added
Add Extended Attributes to the file
[Rails] [Memo] When to add = to <%%> and when not
[Rails] How to create a table, add a column, and change the column type
Add empty data to the top of the list
[Java] [XML signature] Add prefix and ID to Signature tag
[Ruby On Rails] How to search and save the data of the parent table from the child table
How to solve the problem when the value is not sent when the form is disabled in rails and sent
Add an icon to the header link using Rails fontawesome
[Rails] Prepare dummy data to be used for testing [Faker]
I want to create a form to select the [Rails] category
Add a shadow to the Swift Button (and also the circle)
Options for rails new and settings to be done after rails new
How to run React and Rails on the same server
[Rails] Where to be careful in the description of validation
[Docker] How to back up and restore the DB data of Rails application on docker-compose [MySQL]
4 Add println to the interpreter
[Rails] Add / Remove Forms (cocoon)
[Rails] Add column to devise
I want to import the pull-down menu items when submitting a form in Rails into CSV and display them from the DB data.
[Rails] How to get the URL of the transition source and redirect
I tried to sort the data in descending order, ascending order / Rails
[Rails 6] How to create a dynamic form input screen using cocoon
[Rails] How to introduce kaminari with Slim and change the design
Method to add the number of years and get the end of the month
[Rails] Add a confirmation screen and a completion screen to devise membership registration.
[Rails] Read the RSS of the site and return the contents to the front
When log data accumulates in Rails and the environment stops working
How to get and add data from Firebase Firestore in Ruby
Things to remember and concepts in the Ruby on Rails tutorial
[Rails] I want to add data to Params when transitioning with link_to
How to erase the process ID (PID) that cannot be erased! (Handling inoperability with Vim and Vi)