How do exchange sorting with tablesorter - tablesorter

I want to sort the list of exchange. it looks the values ​​of foreign currencies. but, the country with the sort of value for money. transformed values ​​and the sort, to show the actual values​​.
I could not sort by a value converted. was the sort of values ​​that appear. How to sort with the values ​​calculated. But the secret values ​​calculated?
<script id="js">
var dolar = 1.7849;
var euro=2.3643;
var yen=1;
$(function() {
$("table").tablesorter({
theme: 'blue'
,headers: {
0: {
sorter: false
},
1: {
sorter: 'custom_sort_function'
},
2: {
sorter: false
}
}
});
});
</script>
<table class="tablesorter">
<thead>
<tr>
<th>ID</th>
<th>Money</th>
<th>Symbol</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td><!-- exchenge value dolar * cellvalue -->
<td>USD</td>
</tr>
<tr>
<td>2</td>
<td>1</td><!-- exchenge value euro * cellvalue -->
<td>EUR</td>
</tr>
<tr>
<td>3</td>
<td>1YEN</td><!-- exchenge value yen * cellvalue -->
<td></td>
<td>TL</td>
</tr>
</tbody>
</table>
Sample View:
Sort Money Field ASC
ID Money Symbol
-- ------- ----------
1 1YEN YEN
2 1USD USD
3 1EURO EUR
Sort Money Field Desc
ID Money Symbol
-- ------- ----------
3 1EURO EUR
2 1USD USD
1 1YEN YEN

I'm not exactly sure what you want, but I put together this demo which will sort the Money column on the calculated exchange rate, but you can't tell what's going on, so I included a line in the parser to add the calculated value to the table cell.
I modified the HTML slightly:
<tbody>
<tr>
<td>1</td>
<td data-value="usd">1</td><!-- exchenge value dolar * cellvalue -->
<td>USD</td>
</tr>
<tr>
<td>2</td>
<td data-value="eur">1</td><!-- exchenge value euro * cellvalue -->
<td>EUR</td>
</tr>
<tr>
<td>3</td>
<td data-value="yen">1</td><!-- exchenge value yen * cellvalue -->
<td>YEN</td>
</tr>
</tbody>
then used this code:
// add the exchange rate out here
var exchange = {
usd : 1.7849,
eur : 2.3643,
yen : 1
};
// add parser through the tablesorter addParser method
$.tablesorter.addParser({
// set a unique id
id: 'exchange',
is: function(s) {
// return false so this parser is not auto detected
return false;
},
format: function(s, table, cell, cellIndex) {
// format your data for normalization
var $c = $(cell),
cur = $c.attr('data-value'),
val = $.tablesorter.formatFloat(s, table) * (cur ? exchange[cur] : 1);
$c.append(' (' + val.toFixed(2) + ')');
return val;
},
// set type, either numeric or text
type: 'numeric'
});
$('table').tablesorter({
theme : 'blue',
headers: {
1: { sorter: "exchange" }
}
});
If you don't want to add the calculated value to the cell, then just remove this line:
$c.append(' (' + val.toFixed(2) + ')');
Hopefully, I understood what you wanted.

Related

i want to display the sum of salary in the text box of a company form , below the form we can also create new employee (name,role,dob,salary)

enter image description hereat first we create a company by asking user to enter required info after that last 2 form fields are total employee count and total salary which are auto filled once we fill the required details of employee which contain (name,DOB,role,salary) also we can add multiple employees by clicking the '+' button beside the first row (new empty row will appear below the first row),so how can we get total salary for given employee field and display total salary in the company form above which has the Totalsalary field by using jquery
this is the code i tried
$('.Salary').last.change(function () {
$('.Salary').each(function () {
Totalsalary = parseInt(Totalsalary) + parseInt($('.Salary').val());`enter code here`
console.log(Totalsalary);
$('.Totalsalary').val(Totalsalary);
});
Totalsalary = 0;
});
If the class of the
salary inputs is Salary,here is a sample demo,calculating the salary when page loads and when the data of Salary inputs changes:
html:
<table>
<tr>
<td>Salary</td>
</tr>
<tr>
<td><input type="number" class="Salary" /></td>
</tr>
<tr>
<td><input type="number" class="Salary" /></td>
</tr>
<tr>
<td><input type="number" class="Salary" /></td>
</tr>
</table>
js:
<script>
$(".Salary").change(function () {
calcualteSalary();
})
function calcualteSalary() {
var Totalsalary = 0;
$('.Salary').each(function () {
if ($(this).val() != "") {
Totalsalary += parseInt($(this).val());
}
});
$('.Totalsalary').val(Totalsalary);
}
$(function () {
calcualteSalary();
})
</script>
result:

Tablesorter: don't show specific column values as filters

Is there a way to remove a column value as a filter on a column that has a "filter-select" attribute.
Here's an example from #mottie in jsfiddle
http://jsfiddle.net/Mottie/856bzzeL/1085/. I just added a "filter-select" on column Animal column. is there a way for example to remove Koala from the drop down filter values?
HTML
<table class="tablesorter">
<thead>
<tr>
<th>AlphaNumeric</th>
<th>Numeric</th>
<th class="filter-match filter-select">Animals</th>
<th>Sites</th>
</tr>
</thead>
<tbody>
<tr>
<td>abc 123</td>
<td>10</td>
<td>Koala</td>
<td>http://www.google.com</td>
</tr>
<tr>
<td>abc 1</td>
<td>234</td>
<td>Ox</td>
<td>http://www.yahoo.com</td>
</tr>
<tr>
<td>abc 9</td>
<td>10</td>
<td>Girafee</td>
<td>http://www.facebook.com</td>
</tr>
<tr>
<td>zyx 24</td>
<td>767</td>
<td>Bison</td>
<td>http://www.whitehouse.gov/</td>
</tr>
<tr>
<td>abc 11</td>
<td>3</td>
<td>Chimp</td>
<td>http://www.ucla.edu/</td>
</tr>
</tbody>
Script: Tablesorter
/* Documentation for this tablesorter FORK can be found at
* http://mottie.github.io/tablesorter/docs/
*/
// See http://stackoverflow.com/q/40899404/145346
$(function(){
$('table').tablesorter({
theme: 'blue',
widgets: ['zebra', 'filter'],
widgetOptions: {
filter_defaultFilter: {
// Ox will always show
2: '{q}|Ox'
}
}
});
});
In this case, you will need the filter_selectSource option to manipulate the options of the select (demo)
$(function() {
$('table').tablesorter({
theme: 'blue',
widgets: ['zebra', 'filter'],
widgetOptions: {
filter_defaultFilter: {
// Ox will always show
2: '{q}|Ox'
},
filter_selectSource: function(table, column, onlyAvail) {
// get an array of all table cell contents for a table column
var array = $.tablesorter.filter.getOptions(table, column, onlyAvail);
// remove Koala (multiple entries) from array
return $.grep(array, function(animal) {
return animal !== "Koala";
});
}
}
});
});

Knockout JS foreach nested, value updates in all fields

I am using knockout js to acheive a task. My model is like:
var ServiceLevelRates = function(data, availableClasses) {
return {
TaxTypeID: ko.observable(data.Key),
TaxTypeName: ko.observable(data.Name),
ExtendedTaxTypeName: data.Name.replace(/\s+/g, ''),
ApplyAfter: ko.observable(-1),
TaxClasses: ko.observableArray(availableClasses)
};
};
var TaxClass = function(data, availableServices) {
return {
ServiceClassID: data.ServiceClassID,
ServiceClassName: ko.observable(data.ServiceClassName),
TaxServices: ko.observableArray(availableServices)
};
};
var TaxService = function(data) {
return {
ServiceID: ko.observable(data.ServiceID),
ServiceName: ko.observable(data.ServiceName),
ServiceRate: ko.observable(data.ServiceRate > 0 ? data.ServiceRate : "").extend({ numeric: 2 })
};
};
and my html is like:
<tbody data-bind="foreach: ServiceLevelRates">
<tr>
<td style="width:100%;">
<table width="100%">
<tr>
<td style="width:2%;">
<img src="../../Images/del_up.gif" onclick="HideMyChilds(this);" />
</td>
<td data-bind="text: TaxTypeName">
</td>
</tr>
<tr>
<td></td>
<td>
<table width="100%">
<tr>
<td style="width:20%;">
<label id="lblApplyAfter" myId="lblApplyAfter" runat="server">Apply After</label>
</td>
<td></td>
</tr>
<tr>
<td>
<select id="sltApplyAfter" SkinID="drpFields" name="sltApplyAfter" runat="server" myId="sltApplyAfter">
<option value="-1">Charge</option>
</select>
</td>
<td>
<input type="checkbox" />Apply for All Services<input type="text" onkeypress="ValidateDecimalValue(event,this)"; onblur="ApplyForAllServices(this);" data-bind="attr: { 'class': ExtendedTaxTypeName }" /> %
</td>
</tr>
<tr>
<td colspan="2">
<table width="100%">
<tbody data-bind="foreach: TaxClasses">
<tr>
<td style="width:2%;">
<img src="../../Images/del_up.gif" onclick="HideMyChilds(this);" />
</td>
<td style="width:100%;" class="tdRepeaterHeaderBG" data-bind="text: ServiceClassName">
</td>
</tr>
<tr>
<td></td>
<td>
<table width="100%">
<thead>
<tr>
<td style="width:1%;">
<td style="width:24%;" class="tdRepeaterHeaderBG">Service Name</td>
<td style="width:75%;" class="tdRepeaterHeaderBG">Amount</td>
</tr>
</thead>
<tbody data-bind="foreach: TaxServices">
<tr>
<td style="width:1%;">
<td style="width:24%;" data-bind="text: ServiceName"></td>
<td style="width:75%;">
<input type="text" data-bind="value: ServiceRate, attr: { 'class': $parents[1].ExtendedTaxTypeName, 'id': $parents[1].ExtendedTaxTypeName + ServiceID }" />%
</td>
</tr>
<tr>
<td></td>
<td colspan="2">
<div style="font-size: 11px; width:98%;height:5px; border-top: 1px dotted gray;"> </div>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</tbody>
The problem is when I provide ServiceRate for a taxservice in one class, it is updated into text field of same service in all other classes. Any help on it will be great.
Your code have several issues.
First, mostly a cosmetic one. You are using tables for layout. They should only be used when you truly need tabular data. Div's or lists are much better in most cases, and if you need to layout something, you could use css margins.
You are mixing, and mixing up, different object schemes.
One is to return an object literal:
function Foo() {
return {
Property: ko.observable(),
}
}
This schema could, but shouldn't, be called with the new operator.
The other one is prototype-based:
function Foo() {
var self = this;
self.Property = ko.observable();
}
This schema must be called with the new operator.
It is easiest to stick to one schema. With knockout, the latter is easier to use in some cases.
You are not using observables for all properties. It is a little confusing to be using observables for some properties, and not for others. You have to go back to the source-code to confirm for each property.
Your object model does not take into account object reuse. You are passing the same objects to each ServiceLevelRate, so when you are updating one TaxService, the same TaxService in all other TaxClass will also be updated.
One simple solution for this, is to factor out the fields that needs updating into mapping objects.
// This part is constructed once, based on server data.
function TaxService(data) {
var self = this;
self.ServiceID = ko.observable(data.ServiceID);
self.ServiceName = ko.observable(data.ServiceName);
}
// This part is constructed for each TaxClassMapping
function TaxServiceMapping(svc) {
var self = this;
self.TaxService = ko.observable(svc);
self.ServiceRate = ko.observable("");
}
Lastly; To conditionally update the rates based on the check-box, you can bind the it with the checked-binding. In the subscription for the ServiceLevelRate-wide rate, you just check if the check-box was checked, before proceeding to update the other fields.
self.ApplyForAll.subscribe(function (newValue) {
if (self.ApplyForAllCheckBox()) {
ko.utils.arrayForEach(self.Classes(), function (clsMapping) {
ko.utils.arrayForEach(clsMapping.ClassServices(), function (svcMapping) {
svcMapping.ServiceRate(newValue);
});
});
}
});
Here is an updated fiddle:
http://jsfiddle.net/MizardX/V8DTj/
I scaled down the models to the essential parts, to make them easier to work with.
To make the TaxServices show only for certain TaxClasses, you could filter which TaxService-objects you want to include for each TaxClass.
function TaxClassMapping(taxClass, availableServices) {
var self = this;
self.TaxClass = ko.observable(taxClass);
var classID = taxClass.ServiceClassID();
var filtered = ko.utils.arrayFilter(availableServices, function (svc) {
// svc.ServiceClassID is a new property in TaxService
return svc.ServiceClassID() === classID;
});
var mapped = ko.utils.arrayMap(filtered, function (svc) {
return new TaxServiceMapping(svc);
});
self.ClassServices = ko.observableArray(mapped);
}

get the row data using jquery by clicking buttons for that row

I am having issues to get the table data in the row if a button is selected. I have two buttons approve and deny and based on what button user clicks I want to grab the data using query. can get the row numbers and stuff just not the row data. I need to get the id and tester.
here is what I have
<table id="mytable" width="100%">
<thead>
<tr>
<th>ID</th>
<th>Tester</th>
<th>Date</th>
<th>Approve</th>
<th>Deny</th>
</tr>
</thead>
<tbody>
<tr class="test">
<td class="ids">11565 </td>
<td class="tester">james</td>
<td>2012-07-02 </td>
<td><Button id="Approved" type="submit" >Approved</button>
</td>
<td><Button id="deny_0" type="submit" >Denied</button>
</td>
</tr>
</tbody>
</table>
here is my javascript to get the tr and td number but I am not sure how to use it to get what I need
$(document).ready(function() {
/*$('#cardsData .giftcardaccount_id').each(function(){
alert($(this).html());
}); */
$('td').click(function(){
var col = $(this).parent().children().index($(this));
var row = $(this).parent().parent().children().index($(this).parent());
alert('Row: ' + row + ', Column: ' + col);
// alert($tds.eq(0).text());
console.log($("tr:eq(1)"));
// $("td:eq(0)", this).text(),
});
});
$(document).ready(function(){
$('#Approved').click(function(){
var id = $(this).parent().siblings('.ids').text();
var tester = $(this).parent().siblings('.tester').text();
console.log(id);
console.log(tester);
});
});​
JSFiddle
$(function(){
$('button').on('click', function(){
var tr = $(this).closest('tr');
var id = tr.find('.ids').text();
var tester = tr.find('.tester').text();
alert('id: '+id+', tester: ' + tester);
});
});​
FIDDLE
I would use closest() to get the tr and then descend from there.
var tr = $('td').closest('tr')
Also I think this is unnecessary, in your example that would be $(this):
$(this).parent().children().index($(this)) // === $(this)
$('table').on('click', 'button', function() {
var parentRow = $(this).parent().parent();
var id = $('td.ids', parentRow).text();
var tester = $('td.tester', parentRow).text();
alert('id: ' + id + ', tester: ' + tester);
});​

jQuery loop through td and insert jQueryUI progress bar

I am trying to use the jQuery's progressbar method on the value in my table. I've been able to traverse the table and add the proper div's for the required progressbar's selector. The progress bar does not display the val variable correctly.
var i = 0;
var val = 0;
var id = "";
$("document").ready(function() {
$('#progress tr').find('td').each(function() {
//$(this).append("<div></div>");
if ($(this).html() >= 0)
{
//alert($(this).html());
val = $(this).html();
id = "p_"+i;
$(this).html('<div id="'+id+'"></div>');
$('#'+id).progressbar({
"value": val
});
i++;
//$('#'+id).attr('aria-valuenow',val);
alert(val);
}
});
});
$(function() {
$("#progressbar").progressbar( "option", "value", 37 );
});
<table cellspacing="0" cellpadding="0" border="0" id="progress">
<caption>Class Performance</caption>
<tbody>
<tr>
<th>Student Name</th>
<th>Grade 1</th>
<th>Grade 2</th>
<th>Grade 3</th>
<th>Grade 4</th>
<th>Grade 5</th>
<th>Grade 6</th>
</tr>
<tr>
<td>Wayne, Bruce</td>
<td>100</td>
<td>100</td>
<td>67</td>
<td>14</td>
<td>6</td>
<td>0</td>
</tr>
<tr>
<td>Dent, Harvey</td>
<td>100</td>
<td>100</td>
<td>33</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
</tbody>
</table>
val = $(this).html();
will retrieve all the 'html'.. i.e along with the tags of the elements within the TD....
If you want to get the integer value alone, try saving it in a hidden field inside the td and accessing it when you want to get the value....

Resources