Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to add multiple images #3

Open
markrickert opened this issue Aug 13, 2015 · 9 comments
Open

Add ability to add multiple images #3

markrickert opened this issue Aug 13, 2015 · 9 comments

Comments

@markrickert
Copy link
Contributor

This one could be hard but I'm going to need it for an upcoming project :)

Perhaps something like this:

# Requires a minimum of 2 images, up to 12 total.
{
  title: 'Images',
  name:  :picture,
  type:  :image,
  max: 12,
  required: 2
}
@markrickert
Copy link
Contributor Author

Hardest part of this will be the cell displaying the images... having the ability to delete the images and add more, etc. Any ideas on that?

@bmichotte
Copy link
Owner

With the current code, I think it could be done with a on_add and on_remove on the "cell" and change the options: [:insert, :delete] of the cell to add/remove :insert

Something like

{
  title: 'Images',
  name:  :picture,
  type:  :image,
  on_add: -> {
    @current_images += 1
    if @current_images == 12
       a_method_to_get_the_section.options = [:delete]
    else
       a_method_to_get_the_section.options = [:insert, :delete]
    end
  },
  on_remove: -> {
    # same stuff
  }
}

Ideally, this could be done internally. I really like the required: some_number but not sure about max. Maybe something like max_required instead ?

@bmichotte
Copy link
Owner

Or required: [2, 12] ?

@markrickert
Copy link
Contributor Author

I figured I'd piggyback on the required statement. As far as the array, I much prefer a more definitive declaration.

@markrickert
Copy link
Contributor Author

markrickert commented Aug 13, 2015 via email

@bmichotte
Copy link
Owner

Using this code :

def form_data
  [
    {
      title: 'Images',
      name: :images,
      options: [:insert, :delete],
      cells: [
        {
          type: :image,
          name: :image,
          title: 'Image',
          on_add: -> {
            @current_images ||= 1
            @current_images += 1

            if @current_images >= 4
              section = section_with_tag(:images)
              section.options = [:delete]
            end
          },
          on_remove: -> {
            @current_images -= 1

            if @current_images < 4
              section = section_with_tag(:images)
              section.options = [:insert, :delete]
            end
          }
        }
      ]
    }
  ]
end

form

I had to refactor a lot of code... now, I just need to find a good name for this required number

@markrickert
Copy link
Contributor Author

haha. AWESOME! I'll give it a try and let you know how it goes.

@markrickert
Copy link
Contributor Author

Just tried it and it works great! Only change I'm going to try and make at some point is that i want them to add one image and then after they select the image another row pops up. The delete button shouldn't show up till they've actually selected an image. Essentially, I want them to select one image at a time, after selecting, another row shows up.

@bmichotte
Copy link
Owner

To do this kind of thing, it will require some hacking of XLForm, but it will be awesome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants