🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

dynamic_nested

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dynamic_nested

Add/Remove nested associations dynamically in forms that generates markup like Phoenix Framework.

0.1.4
latest
npm
Version published
Maintainers
1
Created
Source

Dynamic Nested

Actions Status

Add dynamic support to add and remove nested associations generated by Phoenix.HTML.inputs_for.

Examples

In order to get it working, the following attributes must be added in the markup:

[dynamic-nested]                 - to active this component.
[dynamic-nested-index=${index}]  - to get nested association.
[dynamic-nested-add]             - to add nested.
[dynamic-nested-remove]          - to remove nested.
<div dynamic-nested>
 <%= inputs_for @f, :categories, [skip_hidden: true], fn c -> %>
   <%= content_tag :div, dynamic_nested_index: c.index do %>
     # PS: generate hidden fields inside rows group to handle them easily.
     = for {key, value} <- row.hidden do
       = hidden_input c, key, value: value, dynamic_nested_field_id: true
     <%= text_input c, :name %>
     <button type="button" dynamic-nested-remove>Remove</button>
   <% end %>
 <% end %>
</div>

<button type="button" dynamic-nested-add>Add</button>

Also, make sure to initialize this script after importing it on your application.

import DynamicNested from 'dynamic_nested'

document.querySelectorAll('[dynamic-nested]').forEach(element => DynamicNested(element))

It supports the following callbacks:

  • beforeClone - You might want to do something before cloning the element.
  • afterAdd - You might want to do something after adding the new element.
  • afterRemove - You might want to do something after removing the element.
const beforeClone = (element) => { ... }
const afterAdd    = (element, newElement) => { ... }
const afterRemove = (elements) => { ... }

document
  .querySelectorAll('[dynamic-nested]')
  .forEach(element => DynamicNested(element, { beforeClone, afterAdd, afterRemove }))

Everytime a User adds a new row, it is going to generate a new index for that row incrementing +1 from the last row on the page. As soon as an User removes a row, all indexes will be updated accordingly to reflect their position on the page.

Instalattion

npm install --save dynamic_nested

Know caveats

  • It must contains at least one nested markup rendered on the page since DinamicNested will use it as a template to clone.
  • You must be using the last version of Phoenix.HTML that supports skip_hidden fields.

FAQs

Package last updated on 12 Dec 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts