I have a JSFiddle which demonstrates my create page. Essentially, I have a table where someone can enter an action and a date. When a new row is added, some javascript is used to clone the row and reset the datepicker for the new row. As you can see in the fiddle, that works fine.
My data is then saved to a database table, one row for each action. Not including all the columns, my table basically looks like the following
$("#dataTables-example").on('click', '.btn-danger', function () {
$(this).parent().remove();
});
$("#dataTables-example2").on('click', '.btn-danger', function () {
$(this).parent().remove();
});
This is all good up until this point. The issue is with the edit page for this table. I pass the page the variables it needs, and then I do the following
action | deliveryDate |
---------------------------------
Walk dog | 01/01/2016 |
---------------------------------
Wash clothes | 08/02/2016 |
---------------------------------
Party | 09/02/2016 |
---------------------------------
If I view the sourcecode of what is outputted, I see what I expect to see
@foreach($actionPoints as $key => $action)
<tr id='actionRow{{$key}}'>
<td>
{!! Form::text('actionInput['.$key.'][action]', $action->action, array('class' => 'form-control', 'id' => 'actionInput'.$key)) !!}
</td>
<td>
{!! Form::text('actionInput['.$key.'][deliveryDate]', $action->deliveryDate, array('class' => 'form-control dateControl', 'id' => 'dateInput'.$key)) !!}
</td>
</tr>
@endforeach
So each datepicker element has a unique id. The problem is, because my datepickers were declared dynamically via javascript before, it does not apply to my edit page. As such, only the first datepicker will work. I can't declare a load of datepickers within my javascript because I don't know how many I will need.
Is there any way to get the datepickers working on my edit page? I need to somehow re declare them for every row I have, but not too sure how I can achieve this?
Thanks
答案 0 :(得分:1)
Why won't you simpy use class CreateArticlesCategories < ActiveRecord::Migration
def change
create_table :articles_categories, :id => false do |t|
t.references :article
t.references :category
end
end
def self.down
drop_table :articles_categories
end
end
instead of $('.dateControl')
?
This way you create N instances of datepicker, keeping them unique via $('.dateControl:first')
attribute.