1

I'm facing a stupid issue there but I'm sure it would be easy for you, master of javascript !

Here is my symfony form :

$form = $this->createFormBuilder()
            ->add('accept', 'checkbox', array('required' => false, 'label' => 'Do You Accept This News ?'))
            ->add('custom', 'textarea', array('required' => false, 'attr' => array('rows' => 10), 'label' => 'Attach a Custom Message'))
            ->add('line', 'integer', array('required' => false))
            ->add('Send', 'submit',array('attr' => array('class' => 'button')))
            ->getForm();

And here is my twig template rendering it :

<table>
        <thead>
        <tr>
            <th>Time</th>
            <th>Statement</th>
            <th>Action</th>
        </tr>
        </thead>
        <tbody>
        {% set count = 0 %}
        {% for milestone in milestones %}
        <tr>
            <td>{{ milestone.createdAt | date('M-d H:i:s') }}</td>
            <td><a href="{{ path('rudelist_entity',{'slug':milestone.idEntity.slug})}}">{{ milestone.idEntity.name}}</a>{{ newStatements[count] }}
            </td>
            <td>
                <a class="button revealModal" href="#" data-reveal-id="myModalJournalist">Treat News</a>
                <div id="myModalJournalist" class="reveal-modal" data-reveal>   
                    {{ form_start(form) }}
                    <div class="marginBottom">
                        {{ form_widget(form.accept) }}
                        {{form_label(form.accept)}}
                    </div>
                    <div class="fieldContainer fieldTextareaContainer">
                        {{form_label(form.custom)}}
                        {{form_widget(form.custom)}}
                        {{form_errors(form.custom)}}
                    </div>
                    <div class="hide">
                        {{form_widget(form.line)}}
                    </div>

                    {{ form_end(form) }}

                    <a class="close-reveal-modal">&#215;</a>
                </div>
        </tr>
        {% set count = count + 1 %}
        {% endfor %}
        </tbody>
    </table>

So the thing is, I'm displaying a button for each row, but they all open the same modal with an unique form, in which I have a boolean, a textarea and an integer field which I'm hidding because I want to auto-fill the "line" row of my form with the index of the row of the button that was click, so I would know with row I need to update. I've been trying that but unsuccessfully :

$(function(){
    $("body").on('click','a.button.revealModal',function(){
        var position = $(this).parents("tr").index();
        $("#form").find("input#form_line").val(position);
    });
});

Thanks for your help !

1

1 Answer 1

2

See https://stackoverflow.com/questions/469883/how-to-find-the-index-of-a-row-in-a-t‌​able-using-jquery Have you tried:

$("tr").index(this)

The documentation shows just passing this and that the preceding selection should be where the node is found. If you need to find it in a specific table (and there are multiple), you may need to provide some context:

// haven't tested this
$("tr", $(this).closest("table")).index(this) 
Sign up to request clarification or add additional context in comments.

Comments

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.