html5 onclick update table data - html

I have a table that has js to allow me to sort the data. But I need to be able to change the data and have js still sort it when I click the header. I have a input box off to the side with a button. I am looking for a onclick way to change the table data from X to what is ever in that input box.
<td id="tabledata1">
1
</td>
I need to be able to change the "1" in that table data. I can not find the function code to effect that specific number. I am guessing it is something like this document.getElementById("tabledata1").style.color but instead of style.color there is something to reference table data.

It would look something like this
var cell = document.getElementById("tabledata1");
var button = document.getElementById("my-button");
var input = document.getElementById("my-input");
var par = document.getElementById("result");
button.addEventListener("click", function() {
cell.textContent = input.value;
});
<input id="my-input" type="text" />
<button id="my-button">Change Table Data</button>
<table>
<tr>
<td id="tabledata1">1</td>
</tr>
</table>
<p id="result"></p>
Breakdown
First query the document for the ids you want and store them into variables. You can then listen for the click event with the addEventListener function.
You can change the text of the table cell, by using the textContent property and setting it equal to the value of the input element.

Related

Testcomplete click on Checkbox using Xpath

I do have the following html text:
<tr class="off" onmouseout="this.className='off'"onmouseover="this.className='over'">
<td><input type="checkbox" name="caller_enrollment_form:enrollmentParticipantSelectionTable:0:personSelecti‌​on" id="caller_enrollment_form:enrollmentParticipantSelectionTable:0:personSelection‌​" value="true" />
</td>
<td><span id="caller_enrollment_form:enrollmentParticipantSelectionTable:0:extClientId">11‌​1</span>
</td>
<td><span id="caller_enrollment_form:enrollmentParticipantSelectionTable:0:clientName">SAM‌​UEL</span>
</td>
I am trying to click on the checkbox which has the data 111 in the same row.
I am trying to something like this:
page.FindChildByXPath("//span[contains(text(),'111')]/parent::td/preceding-sibling::td/input[#name='caller_enrollment_form:enrollmentParticipantSelectionTable:0:personSelection']",false)
but I get object not found error.
Your checkox has an ID, and IDs are unique within a page. So you can use:
// JScript
var checkBox = page.FindChildByXPath("//input[#id='caller_enrollment_form:enrollmentParticipantSelectionTable:0:personSelection‌​']");
checkBox.ClickChecked(true); // Check the checkbox
The ID contains a number (0); you can use a variable to specify this number:
var checkBox = page.FindChildByXPath("//input[#id='caller_enrollment_form:enrollmentParticipantSelectionTable:" + index + ":personSelection‌​']");
If you specifically need to identify by the text "111", you can use something like this:
var checkBox = page.FindChildByXPath("//tr[td/span[.='111']]/td/input");
checkBox.ClickChecked(true); // Check the checkbox
You can also use a variable instead of "111":
var checkBox = page.FindChildByXPath("//tr[td/span[.='" + text + "']]/td/input");
XPath explanation:
//tr - find a table row
[ - that matches this condition:
td/span[.='111'] - any of the row's cells contains a SPAN with the text '111'
]
- then in this row
/td/input - find an INPUT in any cell

Angularjs: Onchange event when the user hits enter

I have a HTML table in which one of the column is editable
<td ng-model="my.grade">
<div contenteditable>
{{list.grade}}
</div>
</td>
I have an angular function getInformation which does some calculation and connects to back end and then
makes the table. My goal is that when the user changes the value of above column and hits the enter I want to update the table and basically re-run the function getInformation.
I read that I should ng-model and ng-change but how should I update the table value on the enter?
You can do it like this:
<td ng-model="row.grade"
contenteditable ng-keypress='keyPressed($event)'></td>
So, ng-model and contenteditable must be on the same element.
Also, if you specify ng-model on a element, all of its content will be replaced by the value from a model. So it should be empty.
And
$scope.keyPressed = function (e){
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 13) { // 'Enter' keycode
$scope.getInformation(); // your function here or some other code
e.preventDefault();
}
}
Here is working Plunkr:
http://plnkr.co/edit/sHHlqF
Why do you use contenteditable? Maybe you could use just <input ng-model...>?

AngularJS, checkboxes, and generating dynamic html

I am wanting to generate a table dynamically using Angular JS, based on what is checked on some checkboxes. The problem is that there are a few fields, we will call them relation/column, that I want to display ALWAYS, and the remaining fields only if their box is checked. The relation is searched for via a search box (relations can have multiple columns), and I want to display only the properties of that relation that are relevant to a user.
So
Update Time [X]
Update Status [ ]
Time Zone [ ]
Would display some html along the lines of
<table>
<tr>
<th> Relation </th>
<th> Column </th>
<th> Update Time </th>
</tr>
<tr ng-repeat= "result in results">
<td> {{result.relation}} </td>
<td> {{result.column}} </td>
<td> {{result.update_time}}</td>
</tr>
If no boxes were checked, only the relation and column fields would be populated. The documentation for Angular JS is taking me all over the place, so would anyone have an idea on how to do this?
edit : controller isn't working quite yet, I still need to filter the search results, but basically it goes
$scope.search = function(){
//validate form input
//create url with the input recieved
$http.get(url).success(function(data){
$scope.results = angular.fromJson(data);
});
}
I use mojolicious backend to grab the data I want. Again, the problem isn't that I can't get any data, or that I can't filter the results based on the relation. I want to be able to search based on relation, and only display the attributes of that relation that I want to, based on what is checked. THAT part, I can't figure out.
edit again : the firewall where I'm at prevents me from writing comments/upvoting. You shall be rewarded for your help when I get home tonight. Thank you thank you!
I think the best way to do this would be using ng-show expressions tied to a variable in the model.
For example.
<input type="checkbox" ng-model="updateTime">
makes a checkbox and ties the result to $scope.updateTime. You can then use this variable later on via the ng-show directive like so...
<th ng-show="updateTime"> Update Time </th>
...
<td ng-show="updateTime"> {{result.update_time}}</td>
this means that these elements will only show when updateTime is set to true (i.e the checkbox is checked.)
You can see an example here, I've only implemented the one field but it should be possible to extend it pretty easily!
http://plnkr.co/edit/W6Ht6dnGw4fBplI83fB1?p=preview
I would suggest using a custom filter with the checkbox scope variables passed in. Something like this:
html
<input type="checkbox" ng-model="checkbox1" />
<input type="checkbox" ng-model="checkbox2" />
<input type="checkbox" ng-model="checkbox3" />
... ng-repeat= "result in results|checkboxFilter:{checkbox1,checkbox2,checkbox3}"
filter.js
.filter('checkboxFilter', function() {
return function (results, checkbox1, checkbox2, checkbox3) {
var filtered_objects = angular.copy(results);
for (var i = 0, len = filtered_objects.length; i < len; i++) {
**modify your filtered_objects based on your checkboxes**
if (checkbox1) ...
if (checkbox2) ...
if (checkbox3) ...
}
return filtered_objects;
}
});
Maybe something like that.

How to pass the listid to servlet when I click a hyperlink in jsp

I have a list of items being displayed in a html table, where there is a hyperlink in every row.
When I click the hyperlink, I want to call the servlet with that particular item id.
how can i achieve it?
for 10 items.
<tr>
<td>
<input type="hidden" name="" value="%=request.getAttribute("item_id")%>"
</td>
<td align="center">
<font> (String)providerDetail.get("tripTime")<font />
</td>
<td align="center">
<font>(String) providerDetail.get("noOfSeats")</font>
<td align="center">
<font> Select font </font>
</td>
</tr>
endfor
So, when I click the hyperlink, I need to pass the appropriate item_id to the servlet.
How to have the appropriate input element for the running item_id and to pass the same to servlet?
I am not being able to add html elements as it is not formatted correctly.
On assumption you are using ajax, onclick call a javascript method, pass item_id as parameter to that method.
Then send this to your server as data (I am using jQuery to do this, you can use any other library )
function sendData(item_id){
jQuery.ajax({
type:"POST",
data:"itemId="+item_id,
success: function(data){
//Your code for a successful request goes here.
},
error: function(data){
//Errors treatment goes here.
}
})
}
In case you are doing something like download of a document based on the item id, this is a good example code for that:
var form = '<form action="downloadme"><input type="hidden" name="itemid" value='+item_id+'</form>';
jQuery(form).submit();
Create a form and submit it, and as an hidden parameter pass item_id.
On completion remove that form from body(as you do not need it anymore).
Hope this helps.

Change default figures in textbox via dropdown

I'm working on this page - http://www.medilogicuk.com/v1/products/calculate-savings/
It's a simple calculator that calculates savings. At the moment the default values in the "Pharmacist" & "Dispenser/Technician" inputs are per annum figures, when the user clicks on the drop down and selects "per hour" I want the figures to automatically change to per hour (via a simple calculation)... and vice versa. The input needs to (re)calculate whatever number the user enters, so if they enter 50.00 and change to per annum then the per annum figure needs to reflect that.
How could I implement this?
Here's my code to create that table:
<table border="0">
<tr>
<td colspan="3"><h3>Current service costs</h3></td>
</tr>
<tr>
<td width="440"><p>Pharmacist</p></td>
<td><p style="padding-left:5px!IMPORTANT;">£
<input value="42500" type="text" name="pharmacist" />
</p></td>
<td width="5" rowspan="2"><select>
<option value="perannum">per annum</option>
<option value="perhour">per hour</option>
</select></td>
</tr>
<tr>
<td><p>Dispenser / Technician</p></td>
<td><p style="padding-left:5px!IMPORTANT;">£
<input value="17500" type="text" name="dispenser" />
</p></td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
</table>
You need to do something like below.
assuming that the "select" has got and id "ddlDuration"
using JQuery
$('#ddlDuration').change(function(){
if($(this.val) == 'perhour'{
pharmacist = $('input[name="pharmacist"]');
pharmacist.val( calculateHourlyFromAnnual( pharmacist.val() ) );
}
});
function calculateHourlyFromAnnual( annumRate )
{
return 100; // calculate the value of the hourly rate based on the per-annum rate
}
Worked out a solution for you: http://jsfiddle.net/tive/Gya43/
javascript
$(function() {
$('#ddlDuration').change(function() {
var pharmacist = $('#pharmacist');
var dispenser = $('#dispenser');
if ($(this).val() == 'perhour') {
pharmacist.val(toHour(pharmacist.val()));
dispenser.val(toHour(dispenser.val()));
} else {
pharmacist.val(toAnnual(pharmacist.val()));
dispenser.val(toAnnual(dispenser.val()));
}
});
function toHour(annumRate) {
var num = (annumRate / 8760);
return num.toPrecision(annumRate.length);
}
function toAnnual(hourRate) {
var num = (hourRate * 8760);
return num.toFixed(0);
}
});
If you run into troubles with num.toFixed(2) try num.toPrecision(2) since that will be more precise.
EDIT:
I noticed when using the length of your number in the .toPrecision method, you will always return the original value. It's not so well rounded down but at least you won't get any mistakes. If you want to tweak this behaviour for rounded numbers I suggest to use a placeholder field/attribute/something to store value.
Okay, I'll break it down.
Firstly, this solution uses the jQuery library so you need to reference this in the <HEAD> section. You will so want to reference the javascript file where you will be placing your code:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<!-- You need to define the file name and path as appropriate for yourself. -->
<script type="text/javascript" src="http://path-to-your-url.com/scripts/script.js"></script>
</head>
...
</html>
Next you need to make some small changes to your markup.
<input value="42500" type="text" name="pharmacist" />
<select>
<input value="17500" type="text" name="dispenser" />
becomes:
<input value="42500" type="text" name="pharmacist" id="pharmacist" />
<select id="rate">
<input value="17500" type="text" name="dispenser" id="dispenser" />
The key change being the new id attributes. These will be used by your javascript to identify the key elements in the code (you could use the name attributes for this, as JQone suggests - I prefer using id from a style point of view and because the jQuery/CSS selector code is smaller).
Finally, you create a javascript file (I'm calling it "script.js" here) and place it in the correct folder in the website (in keeping with the path used in the HEAD section of the HTML doc, this will be a sub-folder of the website's root folder called "scripts"). The file will have the following contents:
// This code sets up the javascript. It is saying: 'when the document is ready, register the "changeRate" function with the "change" event of the select box'. So whenever the select box's value is changed, the function will be called.
$( document ).ready( function() {
$( 'select#rate' ).change( changeRate );
} );
// This sets the values of the text values based upon the selected rate and the existing value.
var changeRate = function() {
var rate = $( this );
var pharmacist = $( 'input#pharmacist' );
var dispenser = $( 'input#dispenser' );
if ( rate.val() == 'perhour' ) {
pharmacist.val( calculateHourlyFromAnnual( pharmacist.val() ) );
dispenser.val( calculateHourlyFromAnnual( dispenser.val() ) );
}
else if ( rate.val() == 'perannum' ) {
pharmacist.val( calculateAnnualFromHourly( pharmacist.val() ) );
dispenser.val( calculateAnnualFromHourly( dispenser.val() ) );
}
};
// Calculates an hourly rate based upon the supplied per annum rate. At the moment this doesn't take into account scenarios where the provided value is empty or is not a number so you will need to adjust appropriately.
function calculateHourlyFromAnnual( annumRate )
{
-- Making the assumption that a per-annum rate of $50,000 translates to an hourly rate of $50
return annumRate / 1000;
}
// Calculates a per-annum rate based upon the supplied hourly rate. At the moment this doesn't take into account scenarios where the provided value is empty or is not a number so you will need to adjust appropriately.
function calculateAnnualFromHourly( hourlyRate )
{
-- Making the assumption that an hourly rate of $50 translates to a per-annum rate of $50,000
return hourlyRate * 1000;
}
More than likely, the formula I've used for calculating the rate change is overly simplistic but I don't know the business requirements in your situation. You'll have to figure out the correct formula yourself.
Basically, if you follow these steps the values should be changed as you switch the select list between per annum and per hour.
You have to use JavaScript to solve this.
With jQuery e.g. register a change event listener on your dropdown element and update the value of the two input fields when the selected value of the dropdown element changes.