jQuery loop through td and insert jQueryUI progress bar - jquery-ui

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....

Related

Is there a way to use jQueryUI sortable on table with hidden columns

Recently I had to fix a strange behaviour on a table which use jQueryUI sortable on rows.
When I drag and move a row, the table is shrinked.
After some research, I was able te reproduce the behaviour on simple table :
jsFiddle link
#sort2 .hidden {
display: none;
}
.fullsize {
width: 100%;
}
.fix30 {
width: 30px;
}
.fix100 {
width: 100px;
}
<html>
<body>
<table id="sort1" class="fullsize">
<thead><tr>
<th class="hidden">hide</th>
<th class="hidden">hide</th>
<th class="hidden">hide</th>
<th class="fix30">Test1</th>
<th>Test2</th>
<th class="fix100">Test3</th>
<th class="fix100">Test4</th>
<th class="fix100">Test5</th>
</tr></thead>
<tbody>
<tr>
<td class="hidden">hide</td>
<td class="hidden">hide</td>
<td class="hidden">hide</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td class="hidden">hide</td>
<td class="hidden">hide</td>
<td class="hidden">hide</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td class="hidden">hide</td>
<td class="hidden">hide</td>
<td class="hidden">hide</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</tbody>
</table>
<hr />
<table id="sort2" class="fullsize">
<thead><tr>
<th class="hidden">hide</th>
<th class="hidden">hide</th>
<th class="hidden">hide</th>
<th class="fix30">Test1</th>
<th>Test2</th>
<th class="fix100">Test3</th>
<th class="fix100">Test4</th>
<th class="fix100">Test5</th>
</tr></thead>
<tbody>
<tr>
<td class="hidden">hide</td>
<td class="hidden">hide</td>
<td class="hidden">hide</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td class="hidden">hide</td>
<td class="hidden">hide</td>
<td class="hidden">hide</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td class="hidden">hide</td>
<td class="hidden">hide</td>
<td class="hidden">hide</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</tbody>
</table>
</body>
</html>
var fixHelperModified = function(e, tr) {
var $originals = tr.children();
var $helper = tr.clone();
$helper.children().each(function(index)
{
$(this).width($originals.eq(index).width())
});
return $helper;
};
$("tbody").sortable({
helper: fixHelperModified
}).disableSelection();
My guess, when some columns are hidden, the navigator thinks there is more cols in the row compared to the one in the header.
My question : does anyone had the same issue and how to fix it ?
I was able to fix it by modifying you JS a bit, here is what I did:
var fixHelperModified = function(e, tr) {
var $originals = tr.children();
var $helper = tr.clone();
$helper.children().each(function(index)
{
$(this).outerWidth($originals.eq(index).outerWidth())
});
return $helper;
};
$("tbody").sortable({
start: function(event, ui) {
ui.helper.css('display', 'table')
},
stop: function(event, ui) {
ui.item.css('display', '')
},
helper: fixHelperModified ,
start: function(event, ui) {
return ui.placeholder.children().each(function(index, child) {
var source;
source = ui.helper.children().eq(index);
$(child).removeAttr('class').removeAttr('colspan');
$(child).addClass(source.attr('class'));
if (source.attr('colspan')) {
return $(child).attr('colspan', source.attr('colspan'));
}
});
}
}).disableSelection();
this problem happens because you have a hidden cell (and in the placeholder that the sortable creates for you there are 8 cells and non of them are hidden

Adding field to ASP.NET MVC view

I want to add one more field to my view. So basically, after displaying a town in the dropdown menu, I want to display the (*ABR) field as seen here.
As you can see from the picture, after Advance Ortopedical, I just want to add a filed called *ABR.
<table class="table datatable-responsive datatable-medical-map" id="medProviders" style="width:100%">
<thead>
<tr class="bg-info">
<th>Medical Provider (* Choose one)</th>
<th>Distance (miles)</th>
<th>Duration</th>
</tr>
</thead>
<tbody>
#{
int i = 0;
foreach (var item in medProviders)
{
<tr class="sortList" style="cursor:pointer" id="increment-#i" data-id="#item.Id" data-lat="#item.Latitude" data-long="#item.Longitude">
<td>#item.Firstname</td>
<td id="distance-#i"></td>
<td id="duration-#i"></td>
</tr>
i++;
}
}
</tbody>
</table>
<p id="medicalValidation"></p>
Any suggestions or comments on how to do this in a simple way?
Please use this code. This will add 1 more field at the end of table.
#{
int i = 0;
foreach (var item in medProviders)
{
<tr class="sortList" style="cursor:pointer" id="increment-#i" data-id="#item.Id" data-lat="#item.Latitude" data-long="#item.Longitude">
<td>
#item.Firstname
<br>*ABR</br>
</td>
<td id="distance-#i"></td>
<td id="duration-#i"></td>
</tr>
i++;
}
}
</tbody>

PHP foreach loop and separate rows with a field that has the same value

I have a database table that has a field in the row of which can have the same value. This is a "transaction id" field that will have the order number. The rows can have the same order number but different product id's for each item ordered.
I'm querying the database just fine and outputting the data as well. However, I want to separate the transaction id's that are the same and do something different with that data. I just can figure out how to do that for some reason.
example:
ID txn_id product_id Product_name field5
-------- --------- ----------- --------- ---------
1 265 98 Product Name
2 265 99 Product Name
3 244 98 Product Name
UPDATE
So here is my updated code that still needs help. I have two queries going now. One that checks for duplicates and places them into an array, then the other that loops through the database table with an if statement which checks for matching transaction id numbers.
First Query:
$myquery = $wpdb->get_results("SELECT txn_id , COUNT(*) FROM test_table HAVING COUNT(*) > 1");
global $TXN;
foreach($myquery as $query){
$TXN= $query->txn_id;
}
Then in the main query/loop
global $wpdb;
$prepare_query = $wpdb->get_results("SELECT * FROM test_table" );
foreach($prepare_query as $data){
$txn_id = $data->txn_id;
$product_id = $data->product_id;
$product_name = $data->product_name;
?>
<table class="shop_table my_account_orders" width="100%">
<thead>
<tr>
<th align="left">Order #</th>
<th align="left">Product ID</th>
<th align="left">Product Name</th>
</tr>
</thead>
<tbody>
<tr class="order">
<td>
<?php echo "#" .$txn_id; ?>
<?php
if($txn_id === $TXN){
echo'YES';
}
?>
</td>
<td>
<?php echo $product_id; ?>
</td>
<td>
<?php echo $product_name; ?>
</td>
</tr>
</tbody>
</table>
<?php } ?>
This will output:
<table width="100%">
<thead>
<tr>
<th align="left">Order #</th>
<th align="left">Product ID</th>
<th align="left">Product Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>#265 YES</td>
<td>98</td>
<td>Product Name</td>
</tr>
</tbody>
</table>
<table width="100%">
<thead>
<tr>
<th align="left">Order #</th>
<th align="left">Product ID</th>
<th align="left">Product Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>#265 YES</td>
<td>99</td>
<td>Product Name</td>
</tr>
</tbody>
</table>
<table width="100%">
<thead>
<tr>
<th align="left">Order #</th>
<th align="left">Product ID</th>
<th align="left">Product Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>#244</td>
<td>98</td>
<td>Product Name</td>
</tr>
</tbody>
</table>
When what I want to get is:
<table width="100%">
<thead>
<tr>
<th align="left">Order #</th>
<th align="left">Product ID</th>
<th align="left">Product Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>#265 YES</td>
<td>98</td>
<td>Product Name</td>
</tr>
<tr>
<td>#265 YES</td>
<td>99</td>
<td>Product Name</td>
</tr>
</tbody>
</table>
<table width="100%">
<thead>
<tr>
<th align="left">Order #</th>
<th align="left">Product ID</th>
<th align="left">Product Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>#244</td>
<td>98</td>
<td>Product Name</td>
</tr>
</tbody>
</table>
To get the duplicated ids you could use:
SELECT id,txn_id , COUNT(*)
FROM table_name
HAVING COUNT(*) > 1
Replace of course table_name with the name of your table
I ended up doing it this way:
$myArray = $wpdb->get_results("SELECT * FROM " . $license_table);
$newArray=array();
foreach($myArray as $val){
$newKey=$val->txn_id;
$newArray[$newKey][]=$val;
}
print_r($newArray);
foreach ($newArray as $key => $array){
?>
<table border="0" cellpadding="0" cellspacing="0">
<tr class="header">
<td width="73%" valign="top">KEY</td>
<td width="73%" valign="top">Product Id</td>
<td width="27%" valign="top">Product Name</td>
</tr>
<?php
foreach($array as $values) {
?>
<tr class="details">
<td width="27%" valign="top"><?php echo $key; ?></td>
<td width="73%" valign="top"><?php echo $values->product_id; ?></td>
<td width="27%" valign="top"><?php echo $values->product_name ?></td>
</tr>
<?php } ?>
</table>
<?php
}

How to fit a wide table using jspdf, jspdf-autotable

I'm using jspdf with the plugin jspdf-autotable to create a pdf for a very wide table, is there a way to auto fit the table/column data to any page size?
I tried the code below with overflow: 'linebreak' but it breaks the words halfway not at the empty space
function demoPDF() {
var pdfsize = 'a0';
var pdf = new jsPDF('l', 'pt', pdfsize);
var res = pdf.autoTableHtmlToJson(document.getElementById("rpt_tbl"));
pdf.autoTable(res.columns, res.data, {
startY: 60,
tableWidth: 'auto',
columnWidth: 'auto',
styles: {
overflow: 'linebreak'
}
});
pdf.save(pdfsize + ".pdf");
};
I'm not completely sure I understood your question, but if you want some columns to simply wrap the content and some to linebreak you can to like this.
function demoPDF() {
var pdfsize = 'a0';
var pdf = new jsPDF('l', 'pt', pdfsize);
pdf.autoTable({
html: '#table',
startY: 60,
styles: {
fontSize: 50,
cellWidth: 'wrap'
},
columnStyles: {
1: {columnWidth: 'auto'}
}
});
pdf.save(pdfsize + ".pdf");
};
demoPDF();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.0.272/jspdf.debug.js"></script>
<script src="https://rawgit.com/someatoms/jsPDF-AutoTable/master/dist/jspdf.plugin.autotable.js"></script>
<table id="table" style="display: none;">
<thead>
<tr>
<th title="Field #1">ID</th>
<th title="Field #2">First name</th>
<th title="Field #3">Last name</th>
<th title="Field #4">Email</th>
<th title="Field #5">Country</th>
<th title="Field #6">IP-address</th>
</tr>
</thead>
<tbody>
<tr>
<td align="right">1</td>
<td>Donna</td>
<td>Moore</td>
<td>dmoore0#furl.net</td>
<td>China</td>
<td>211.56.242.221</td>
</tr>
<tr>
<td align="right">2</td>
<td>Janice Janice Janice Janice Janice Janice Janice Janice Janice Janice Janice Janice Janice Janice Janice</td>
<td>Henry</td>
<td>jhenry1#theatlantic.com</td>
<td>Ukraine</td>
<td>38.36.7.199</td>
</tr>
<tr>
<td align="right">3</td>
<td>Ruth</td>
<td>Wells</td>
<td>rwells2#constantcontact.com</td>
<td>Trinidad</td>
<td>19.162.133.184</td>
</tr>
<tr>
<td align="right">4</td>
<td>Jason</td>
<td>Ray</td>
<td>jray3#psu.edu</td>
<td>Brazil</td>
<td>10.68.11.42</td>
</tr>
<tr>
<td align="right">5</td>
<td>Jane</td>
<td>Stephens</td>
<td>jstephens4#go.com</td>
<td>United States</td>
<td>47.32.129.71</td>
</tr>
<tr>
<td align="right">6</td>
<td>Adam</td>
<td>Nichols</td>
<td>anichols5#com.com</td>
<td>Canada</td>
<td>18.186.38.37</td>
</tr>
</tbody>
</table>

jQuery UI sortable with fixed rows

I'm using a jQuery UI sortable with a table (works fine). I would like to make the header and last row fixed (non-moveable).
The jQuery UI docs indicate this can be done using a selector for items, but I am at a loss for the syntax.
Here is the relevant code:
<script type="text/javascript">
$(function () {
$("#response_options tbody.content").sortable();
$("#response_options tbody.content").disableSelection();
});
</script>
<table id="response_options" class="data-table">
<tbody class="content">
<tr>
<th>Links</th><th>Response</th>
</tr>
<tr class="sortable-row">
<td>Edit</td>
<td>Item 1</td>
</tr>
<tr class="sortable-row">
<td>Edit</td>
<td>Item 2</td>
</tr>
<tr class="sortable-row">
<td>Edit</td>
<td>Item 3</td>
</tr>
<tr class="sortable-row">
<td>Edit</td>
<td>Item 4</td>
</tr>
<tr class="sortable-row">
<td>Edit</td>
<td>Item 5</td>
</tr>
<tr>
<td>Edit</td>
<td>Item 1</td>
</tr>
</tbody>
</table>
The selector goes inside .sortable(...):
$("#response_options tbody.content").sortable();
as
$("#response_options tbody.content").sortable( items: ??? );
and it should be possible to select only items with class="sortable-row"; but again, I am at a loss for the syntax.
This should work:
$("#response_options tbody.content").sortable({items: 'tr.sortable-row'});
This worked for me:
jQuery(".sortable tbody").sortable({
items: 'tr:not(:first)'
});
For this markup:
<table id="tableid">
<thead>
<tr>
<th>namr</th>
<th>id</th>
</tr>
</thead>
<tbody>
<tr>
<td>jagadeesh</td>
<td>1</td>
</tr>
<tr>
<td>jagadeesh</td>
<td>2</td>
</tr>
</tbody>
</table>
Use this jQuery:
$('#tableid tbody').sortable();
It will move only body content of table you cannot move header part.

Resources