bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

Pass data arguments to modal?

Open andriijas opened this issue 15 years ago • 48 comments

Is this possible? https://gist.github.com/1336532

andriijas avatar Nov 03 '11 13:11 andriijas

$('[data-controls-modal="countryModal"]').data().country

fat avatar Nov 03 '11 20:11 fat

That doesnt work when I have multiple lins using data-controls-modal="countryModal"

I know I can bind click event on my own but would just be insanely nice not to. :-)

andriijas avatar Nov 04 '11 06:11 andriijas

ahh interesting... well I think for now we aren't going to pass the data options around for you, as i don't think this is a very common thing. sorry about that :(

fat avatar Nov 04 '11 18:11 fat

Common way is to use one modal for page.

Then, by clicking one of the links modal appears. So we should know which link shows modal to load information into it.

In my case I have got a row of items and a "show log" button for each. When I click "show log" modal appears but I need to understand what particular log to load.

fl00r avatar Mar 29 '12 08:03 fl00r

@fl00r i agree it would be nice to know the origin of the show event, in the same way we know previous and next tab in tabbables. Im going to check the code and see if I can submit a pull request.

andriijas avatar Mar 29 '12 08:03 andriijas

+1 for this. Consider a case where multiple elements trigger the same modal with a slight variation in state (like a single attribute difference, or so).

@andriijas Have you made any progress on this?

jimryan avatar Apr 01 '12 19:04 jimryan

Not yet. I Had an AFK weekend.

Sent from my frank phone, mhm

On Sunday 1 April 2012 at 21:20, Jim Ryan wrote:

+1 for this. Consider a case where multiple elements trigger the same modal with a slight variation in state (like a single attribute difference, or so).

@andriijas Have you made any progress on this?


Reply to this email directly or view it on GitHub: https://github.com/twitter/bootstrap/issues/531#issuecomment-4868681

andriijas avatar Apr 02 '12 05:04 andriijas

i agree, perhaps having any attributes that start data-* be passed to the modal. would be nice if multiple modals are required.

encodes1 avatar Apr 03 '12 08:04 encodes1

Good idea encodes

+1

bjensen avatar Apr 14 '12 15:04 bjensen

I havent figured out a good way to implement this actually.

I would like to use the naming convention of tabs with relatedTarget, and the easy part is easy:

doing $target.modal(option, this) and $.fn.modal = function (option, relatedTarget) {

But then Im stuck - need to somehow jinx in the relatedTarget into the modal instance data, or pass it in as argument on each call, which I dont like.

There is also the thing that the modal plugin should probably still work without the data-api which makes it more complicated.

andriijas avatar Apr 16 '12 06:04 andriijas

i havn't wrote an implementation yet. but in my head, i feel that data-id='32' etc in the source code would be a good possible way of implementing it. would be easy and powerful, there would need to be other ways to set/get this data. but its a problem that can and im sure will be solved many ways

encodes1 avatar Apr 16 '12 16:04 encodes1

@encodes yeah problem is to pass the elem in somehow to the modal instance so that it can read the $(elem).data(), since the modal is instanciated once, and then reused the related target element from where the click originates needs to be passed in each time since the idea of this is that several links/sources could open the same modal.

(all this goes for collapsables as well)

andriijas avatar Apr 16 '12 18:04 andriijas

+1 for this. In my case, I want to use modals everywhere the user can input something. For instance, on my calendar, when the user clicks a day it opens an "Add event" modal with the date pre-populated.

kxhawkins avatar Apr 20 '12 23:04 kxhawkins

+1

palamedes avatar May 06 '12 05:05 palamedes

+1

danger2k7 avatar May 07 '12 15:05 danger2k7

Big +1 for this. Right now I'm $('body').off('.data-api') and installing my own click handlers. Seems like a waste.

bronson avatar May 11 '12 03:05 bronson

Turns out this is actually pretty easy in 2.0.3. First, add your custom data as any number of data parameters.

<li><a data-person="Zim" data-toggle="modal" data-target="#person-modal"></a></li>
<li><a data-person="Dib" data-toggle="modal" data-target="#person-modal"></a></li>
...

Next, modify bootstrap-modal.js to update your modal's options every time a link is clicked. Otherwise, the modal always displays the data for the first person clicked.

diff --git a/app/assets/javascripts/bootstrap/bootstrap-modal.js b/app/assets/javascripts/bootstrap/bootstrap-modal.js
index 0465cb5..9228636 100644
--- a/app/assets/javascripts/bootstrap/bootstrap-modal.js
+++ b/app/assets/javascripts/bootstrap/bootstrap-modal.js
@@ -209,6 +209,9 @@
       var $this = $(this), href
         , $target = $($this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7
         , option = $target.data('modal') ? 'toggle' : $.extend({}, $target.data(), $this.data())
+      if(option == 'toggle') {
+        $.extend($target.data('modal').options, $this.data())
+      }

       e.preventDefault()
       $target.modal(option)

Finally, use the show event to stuff your custom information into the modal.

    $('#person-modal').on('show', function (event) {
        name = $(this).data('modal').options.person
        $(this).find('.person').html(name)
    })

If @fat can make this patch presentable and commit it, this feature req can be closed.

bronson avatar May 11 '12 06:05 bronson

yeah - would love to see that incorporated. It makes sense, breaks nothing and adds a lot of value to that component.

d1rk avatar May 11 '12 10:05 d1rk

+1

cipater avatar May 17 '12 02:05 cipater

+1!

Laurentvw avatar May 21 '12 15:05 Laurentvw