jquery multiple ids same function - jquery-ui

I have a table that has a date input
<td style="background-color:#c6efce"><input type="text" id="datepicker0"></td>
<td style="background-color:#c6efce"><input type="text" id="datepicker1"></td>
<td style="background-color:#c6efce"><input type="text" id="datepicker2"></td>
<td style="background-color:#c6efce"><input type="text" id="datepicker3"></td>
<td style="background-color:#c6efce"><input type="text" id="datepicker4"></td>
I am trying to access it via for the first one
<script>
$(function() {
$( "#datepicker0" ).datepicker({
showButtonPanel: true
});
});
</script>
How do I access everything?

You could use the "attribute starts-with" selector:
$(function() {
$("input[id^='datepicker']").datepicker({
showButtonPanel: true
});
});
That selector will match any input element whose id value starts with "datepicker". An alternative would be to give all the required elements a common class.
You can also select multiple elements by id using a comma-separated list:
$("#datepicker0, #datepicker1, #datepicker2"); //List as many as necessary
But that's not particularly scalable if you ever need to add more inputs.

The best way is to use a class:
<td style="background-color:#c6efce"><input type="text" class="dp" id="datepicker0"></td>
<td style="background-color:#c6efce"><input type="text" class="dp" id="datepicker1"></td>
<td style="background-color:#c6efce"><input type="text" class="dp" id="datepicker2"></td>
<td style="background-color:#c6efce"><input type="text" class="dp" id="datepicker3"></td>
<td style="background-color:#c6efce"><input type="text" class="dp" id="datepicker4"></td>
<script>
$(function() {
$( ".dp" ).each(function(){
$(this).datepicker({
showButtonPanel: true
});
})
});
</script>
but you can also use this:
<td style="background-color:#c6efce"><input type="text" id="datepicker0"></td>
<td style="background-color:#c6efce"><input type="text" id="datepicker1"></td>
<td style="background-color:#c6efce"><input type="text" id="datepicker2"></td>
<td style="background-color:#c6efce"><input type="text" id="datepicker3"></td>
<td style="background-color:#c6efce"><input type="text" id="datepicker4"></td>
<script>
$(function() {
$( "#datepicker0,#datepicker1,#datepicker2,#datepicker3,#datepicker4" ).datepicker({
showButtonPanel: true
});
});
</script>
The second approach is not advised.

As I understand your question, you're trying to select multiple IDs using jQuery. Here's how you do that:
$('#1,#2,#3')
You just separate the IDs by commas.
But, this isn't the best way to accomplish this. You should really use a class: Assign each td a class and use:
$('td.myClass')
Alternatively, you could assign an ID to the table and select all of its td children. HTML:
<table id="myTable">
<td>text</td>
<td>text</td>
</table>
jQuery:
$('table#myTable td')

You can use this as well
$('#datepicker0,#datepicker1,#datepicker2,#datepicker3,#datepicker4)

Instead of IDs, you should use a CSS class named something like has-datepicker and search for that.

jQuery UI automatically adds the "hasDatePicker" class to all elements that have a date picker. You can query the page for something like $("input.hasDatePicker") to get all of the date pickers.

Related

How to dynamically add and remove table rows and add/update them to the database?

I'm working on a project that includes Asp.net Core, Entity Framework and Razor pages. I want to be able to click on an add button and show more table rows that i can fill and update/add to the database.
At this moment there is one table row and if i save it it will add or update to the database. The thing i cant figure out is how to dynamicly add more table rows and add/update them to the database aswell.
We have found a workaround by using an API call and using vue.js but this seems unneeded and there should be an easier way right?
The examples below don't work...
<tbody>
#for (int i = 0; i < Model.BookingModel.HourRows.Count; i++)
{
<tr>
<td><a class="btn-remove-time-row" v-on:click="removeHourRow(index)"><i class="fas fa-times"></i></a></td>
<td class="has-text-input" style="display:none;"><input type="number" class="form-control" placeholder="Werksoort" asp-for="#Model.BookingModel.HourRows[i].Id" value="#Model.BookingModel.HourRows[i].Id" readonly style="display:none;"/></td>
<td class="has-text-input"><input type="number" class="form-control" placeholder="Werksoort" asp-for="#Model.BookingModel.HourRows[i].WorkTypeId" value="#Model.BookingModel.HourRows[i].WorkTypeId" /></td>
<td class="has-text-input"><input type="text" class="form-control" placeholder="Project" asp-for="#Model.BookingModel.HourRows[i].ProjectId" value="#Model.BookingModel.HourRows[i].ProjectId" /></td>
<td class="has-text-input"><input type="number" class="form-control" placeholder="0,00" asp-for="#Model.BookingModel.HourRows[i].Monday" value="#Model.BookingModel.HourRows[i].Monday" /></td>
<td class="has-text-input"><input type="number" class="form-control" placeholder="0,00" asp-for="#Model.BookingModel.HourRows[i].Tuesday" value="#Model.BookingModel.HourRows[i].Tuesday" /></td>
<td class="has-text-input"><input type="number" class="form-control" placeholder="0,00" asp-for="#Model.BookingModel.HourRows[i].Wednesday" value="#Model.BookingModel.HourRows[i].Wednesday" /></td>
<td class="has-text-input"><input type="number" class="form-control" placeholder="0,00" asp-for="#Model.BookingModel.HourRows[i].Thursday" value="#Model.BookingModel.HourRows[i].Thursday" /></td>
<td class="has-text-input"><input type="number" class="form-control" placeholder="0,00" asp-for="#Model.BookingModel.HourRows[i].Friday" value="#Model.BookingModel.HourRows[i].Friday" /></td>
<td class="has-text-input"><input type="number" class="form-control" placeholder="0,00" asp-for="#Model.BookingModel.HourRows[i].Saturday" value="#Model.BookingModel.HourRows[i].Saturday" /></td>
<td class="has-text-input"><input type="number" class="form-control" placeholder="0,00" asp-for="#Model.BookingModel.HourRows[i].Sunday" value="#Model.BookingModel.HourRows[i].Sunday" /></td>
</tr>
}
<tr class="times-row-control">
<td colspan="11">
<button class="btn btn-primary btn-add-time-row" id="addBtn" v-on:click="addHourRow()"><i class="fas fa-plus"></i>Regel toevoegen</button>
</td>
</tr>
</tbody>
addHourRow: function () {
data.booking.hourRows.push({
workTypeId: undefined,
projectId: undefined,
monday: undefined,
tuesday: undefined,
wednesday: undefined,
thursday: undefined,
friday: undefined,
saturday: undefined,
sunday: undefined
})
}
I expect a button which will add a tabelrow to the table and if i fill everything in i expect it to be added or updated to the database.
As you observed, the function addHourRow adds data to a data structure (I can't see what kind) in the browser's page state, but doesn't do anything to change the data at its source, in your database.
To update the database, you'll need to add a request handler to a controller, and call the controller from your javascript code.
This video tutorial of Entity Framework may be useful: https://www.youtube.com/watch?v=ZX7_12fwQLU

ng-submit won't work with turbolinks

I have a Rails app using Angular and ngResource to display the content of my "Order" model. Inside the Angular part of the app, I can add and delete objects and it works fine. Only if I navigate to the page through my website (using turbolinks) the "addOrder()" function won't work. The deleteOrder() function and all the rest works, though. I checked what's going on with several console.logs and test functions and discovered that only ng-submit doesn't seem to trigger the function.
This is what the essential parts of my code look like:
app.js
var app = angular.module('shop', ['ngResource']);
$(document).on('ready page:load', function() {
angular.bootstrap(document.body, ['shop'])
});
app.factory('models', ['$resource', function($resource){
...
}]);
app.controller('OrdersCtrl', ['$scope', 'models', function($scope, models){
...
$scope.addOrder = function(){
// doesn't work
...
};
$scope.deleteOrder = function(order){
// works
...
};
}])
index.html.erb
...
<form ng-submit="addOrder()">
<tr>
<td>
<input type="number" step="0.01" ng-model="newOrder.total" class="form-control">
</td>
<td>
<select ng-model="newOrder.product_id" class="form-control">
<option ng-repeat="product in products" value="{{product.id}}">{{product.name}}</option>
</select>
<?td>
<td>
<input type="submit" value="+" class="btn btn-success">
</td>
</tr>
</form>
...
<tr ng-repeat="order in orders | orderBy:'-id':reverse">
{{order.id}}
<button ng-click="deleteOrder(order)" class="btn btn-danger btn-sm"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button>
</tr>
...
The fact that confuses me the most is, that seemingly only ng-submit doesn't work - but all the rest does.
EDIT: SOLUTION
So I did some more research and finally figured out that the problem had nothing to do with turbolinks, Angular or Rails. The problem was, that I couldn't have a form inside a table. That caused the DOM to generate a form closing tag right after the opening tag with none of the inputs inside.
I would try defining something like page:change as I guess that is the only part where turbolinks is not working.
$(document).on('ready page:load page:change', function() {});
Just my first guess on this
I found the solution myself.
So I did some more research and finally figured out that the problem had nothing to do with turbolinks, Angular or Rails. The problem was, that I couldn't have a form inside a table. That caused the DOM to generate a form closing tag right after the opening tag with none of the inputs inside.
So this code:
<form ng-submit="addOrder()">
<tr>
<td>
<input type="number" step="0.01" ng-model="newOrder.total" class="form-control">
</td>
<td>
<select ng-model="newOrder.product_id" class="form-control">
<option ng-repeat="product in products" value="{{product.id}}">{{product.name}}</option>
</select>
<?td>
<td>
<input type="submit" value="+" class="btn btn-success">
</td>
</tr>
</form>
would look like this in the DOM:
<form ng-submit="addOrder()"></form>
<tr>
<td>
<input type="number" step="0.01" ng-model="newOrder.total" class="form-control">
</td>
<td>
<select ng-model="newOrder.product_id" class="form-control">
<option ng-repeat="product in products" value="{{product.id}}">{{product.name}}</option>
</select>
<?td>
<td>
<input type="submit" value="+" class="btn btn-success">
</td>
</tr>

Dojo - Upload two files in one Form

I use Dojo and want to send two files and one String to a REST Service. The HTML for that is the following:
//...
<script>
dojo.require("dijit.form.Button");
dojo.require("dijit.form.TextBox");
dojo.require("dijit.form.CheckBox");
dojo.require("dojox.form.Uploader");
dojo.require("dojox.embed.Flash");
if(dojox.embed.Flash.available){
dojo.require("dojox.form.uploader.plugins.Flash");
}else{
dojo.require("dojox.form.uploader.plugins.IFrame");
}
</script>
//..
<form action="http://localhost:8080/service" enctype="multipart/form-data" method="POST" name="inputDataForm" id="inputDataForm">
<table border="1" cellpadding="1" cellspacing="1" height="88" width="925">
<tbody>
<tr>
<td>Dataset1</td>
<td><input name="train" type="file" value="Browse" data-dojo-type="dojox/form/Uploader"/></td>
</tr>
<tr>
<td>Dataset2</td>
<td><input name="test" type="file" value="Browse" data-dojo-type="dojox/form/Uploader"/></td>
</tr>
<tr>
<td>Number of Customers</td>
<td><input name="numberCustomers" size="40" type="text" data-dojo-type="dijit/form/TextBox" /></td>
</tr>
</tbody>
</table>
<p><input name="runButton" type="submit" value="Run" data-dojo-type="dijit/form/Button" id="submitButton" /></p>
</form>
However: If I press the submit button, I can see via Firebug that there are sent TWO POST-Requests. One with the first file and the "Number of Customers" field and one with the second file and the "Number of Customers" field. However, my REST Service wants to have both files and the "Number of Customers" fields in one POST Request. How can i do that? What am I doing wrong?
Thanks a lot in advance
Natan

jquery ui spinner malfunctioning with jquery mobile

I'm trying to use jquery ui spinners in a table with jquery mobile 1.4.0
<div class="table_data">
<table data-role="table" data-mode="reflow" class="ui-responsive table-stroke">
<thead>
<tr>
<th>Address</th>
<th>Doorbells</th>
<th>Mailboxes</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>
<input type="text" class="spinner" name="doorbells"/>
</td>
<td>
<input type="text" class="spinner" name="mailboxes"/>
</td>
</tr>
<tr>
<td>2</td>
<td>
<input type="text" class="spinner" name="doorbells"/>
</td>
<td>
<input type="text" class="spinner" name="mailboxes"/>
</td>
</tr>
<tr>
<td>3</td>
<td>
<input type="text" class="spinner" name="doorbells"/>
</td>
<td>
<input type="text" class="spinner" name="mailboxes"/>
</td>
</tr>
</tbody>
</table>
</div>
<script>
$( ".spinner" ).spinner();
</script>
In the jsfiddle link below you can see my problem. The buttons don't show up in the correct place and they don't even work anymore.
http://jsfiddle.net/murtho/ZZqu6/5/
When I do not include the jquery ui js and css file the spinners work just fine. I need the mobile ui for other features in my application, but I also wish to implement the jquery spinner (of an alternative solution)
This is how it should be working:
http://jsfiddle.net/murtho/tf89L/2/
jQuery UI and jQuery mobile don't play together that well.
For your number spinner, you can fix the formatting with one extra line of code to remove the jQM classes from the spinner button divs:
$(document).on("pagebeforeshow", "#page1", function(){
$( ".spinner" ).spinner();
$(".ui-spinner div" ).removeClass(function() {
return $( this ).attr( "class" );
});
});
Here is your updated FIDDLE
Another option is to set type="number" on the input boxes; however, not all browsers understand this.
You will need to see if jQuery UI causes any other problems with your particular application...

reading from a file using the input type=file in grails

Hi I have the following code in my grails gsp
<form action="upload-script-url" method="post" enctype="multipart/form-data">
<table class="table"style="width: 75%">
<tr>
<td>
<span style="font-weight: bold; ">Select the Source File:</span>
<input size="75" type="file" id="payload" name="payload"/>
</td>
</tr>
<tr>
<td>
<input type="submit" class="red" id="Run">Run</button>
</td>
</tr>
</table>
</form>
I read the form parameters from: here
Are those right parameters in the html form?
Now how should I proceed to read the data from the selected file? do I have to use the apache commons fileupload api ?
Thanks
request.getFile("payload")
and you will get a CommonsMultipartFile
If you take some time to (again) actually look at the documentation you will see how to do it...

Resources