extract data from a tablesorter table with javascript - javascript

I have a tablesorter table that is generated dynamically with javascript and ajax. There are inputs for the user to change values, and when they click a button to go to the next or previous record, it needs to save the information in the table to a MySQL table. I have looked at many of the posts on here and tried numerous examples, and I still can't get any of the data in the table to post to my PHP page to save it.
Here is a sample of the first row of my table:
$("#guides").append('<tr><td id="amCodeOld">'+data.amCodeOld+'</td><td><input type="text" class="qty" id="amOldQty"/></td><td>'+data.amOldPrice+
'</td><td>'+data.am+
'</td><td>'+data.amCodeNew+'</td><td><input type="text" class="qty" id="amNewQty"/></td><td>'+data.amNewPrice+
'</td><td><input type="checkbox" id="amS" '+
'"/></td><td><input type="checkbox" id="amR"'+'"/></td><td><input type="checkbox" id="amCall"'+
'"/></td><td><input type="text" id="amComm" value="'+
'"/></td></tr>');
There are more rows like this one, each for a different guide I am using. What I need to find is the value of the inputs with id of amOldQty, amNewQty etc, and the different checkboxes.
I have buttons for Next, Prev, and Print, and when the button is clicked I am trying to call a function called save() that will get the info, and send it via ajax to another PHP page to be saved.
save() looks like this:
function save() {
var amOldQty
$('#guides tr').each(function(){
alert("searching ");
amOldQty= $(this).find('#amOldQty').val();
if (amOldQty=='' || amOldQty== null) {
amOldQty = "Not Showing Number";
}
console.log(amOldQty);
});// END OF EACH FUNCTION
I have tried:
var amOldQty;
$('#guides tbody tr').each(function() {
amOldQty = $(this).find('td:nth-child(1) input').val();
});
console.log(amOldQty);
and the log shows undefined. I have also tried a more direct approach by using
$('#guides tbody tr').each(function() {
amOldQty = $(this).find('#amOldQty').val();
});
and still have nothing. I am getting the alert of Searching, but in the console.log(amQtyOld) all it shows me is "Not Showing a number". I have even tried to have the input populated when the table first is created, and it still does not find that number.
Update if I add td to:
$(guides tr td).each(function(){...
It does not even give me the alert of "Searching".
How do I get all the data out of this table so I can save it? it seems that everything I am trying should be working.

If your users use browsers that support contenteditable elements, then you can check out the contenteditable widget I have available for my fork of tablesorter.
If you don't want to use contenteditable elements, then you can try the following code (copied and modified from the contenteditable demo, but not tested)
var $table = $('table');
$table.children('tbody').on('change', 'input', function(event){
var $input = $(this),
$cell = $input.closest('td'),
newContent = $input.val(),
cellIndex = $cell[0].cellIndex, // there shouldn't be any colspans in the tbody
rowIndex = $this.closest('tr').attr('id'); // unique row id
// update tablesorter
$table.trigger("updateCell", [$cell]);
$.post("mysite.php", {
"row" : rowIndex,
"cell" : cellIndex,
"content" : newContent
});
});
Hopefully, you're also using an input parser for that column.

The final answer seems to be to just hardcode the value of the inputs $("#amOldQty:).val() and pass them as data through ajax to my php file to save the information. Here is the completed code in case anyone has a similar issue:
function save() {
function GuidesSave(){
this.update();
}
GuidesSave.prototype.list = ['am','fol','tich','book','neb','ster','c','byte','ing'];
GuidesSave.prototype.update = function(){
for( var i = 0 ; i < this.list.length ; i++ ){
var guide = this.list[i];
this[ guide + 'S' ] = $("#" + guide+'S' ).is(":checked") ? 1 : 0;
this[ guide + 'R' ] = $("#" + guide+'R' ).is(":checked") ? 1 : 0;
this[ guide + 'Call' ] = $("#" + guide+'Call' ).is(":checked") ? 1 : 0;
}// end of for loop
}
var guides = new GuidesSave();
$.ajax({
type: "POST",
url: "poSave.php",
dataType: "json",
data: ({po: $('#po').val(),isbn:$("#isbn13").val(),amOldQty:$("#amOldQty").val(),amNewQty:$("#amNewQty").val(),amS:guides.amS,amR:guides.amR, amCall:guides.amCall,amComm:$("#amComm").val(),
folOldQty:$("#folOldQty").val(),folNewQty:$("#folNewQty").val(),folS:guides.folS,folR:guides.folR, folCall:guides.folCall,folComm:$("#folComm").val(),
tichOldQty:$("#tichOldQty").val(),tichNewQty:$("#tichNewQty").val(),tichS:guides.tichS,tichR:guides.tichR, tichCall:guides.tichCall,tichComm:$("#tichComm").val(),
bookOldQty:$("#bookOldQty").val(),bookNewQty:$("#bookNewQty").val(),bookS:guides.bookS,bookR:guides.bookR, bookCall:guides.bookCall,bookComm:$("#bookComm").val(),
nebOldQty:$("#nebOldQty").val(),nebNewQty:$("#nebNewQty").val(),nebS:guides.nebS,nebR:guides.nebR, nebCall:guides.nebCall,nebComm:$("#nebComm").val(),
sterOldQty:$("#sterOldQty").val(),sterNewQty:$("#sterNewQty").val(),sterS:guides.sterS,sterR:guides.sterR, sterCall:guides.sterCall,sterComm:$("#sterComm").val(),
cheggOldQty:$("#cOldQty").val(),cheggNewQty:$("#cNewQty").val(),cheggS:guides.cS,cheggR:guides.cR, cheggCall:guides.cCall,cheggComm:$("#cComm").val(),
byteOldQty:$("#byteOldQty").val(),byteNewQty:$("#byteNewQty").val(),byteS:guides.byteS,byteR:guides.byteR, byteCall:guides.byteCall,byteComm:$("#byteComm").val(),
ingOldQty:$("#ingOldQty").val(),ingNewQty:$("#ingNewQty").val(),ingS:guides.ingS,ingR:guides.ingR, ingCall:guides.ingCall,ingComm:$("#ingComm").val(),
qty1: $('#topqty').val(),price1: $('#topVal').html(),comp1:$('#topCo').html(),
qty2: $('#secqty').val(),price2: $('#secVal').html(),comp2: $('#secCo').html(),
qty3: $('#thrqty').val(),price3: $('#thrVal').html(),comp3: $('#thrCo').html()}),
success: function(data){
}
});
}// END OF SAVE FUNCTION
The GuideSave function loops through all the check boxes (27 different ones) to see whichones are checked so I can save them as either a 1 or 0 and then have them checked or not when the record is recalled.

It is not really clear what or how you are wanting to present the data. However, here is a JSFiddle that does what it appears you desire.
The function createRowsForTesting() just creates the rows in the table and populates the <input> fields to make testing easier.
The function getDataFromTable() walks the rows of a <table>, or all <tables>, sending the data you said you wanted to the console. From your own answer to this question it became clear that you really wanted to access many more <input> elements than was mentioned in your question. Thus, this this function now builds an array of Objects which contain key:value pairs for the data in <input> fields. There is one Object in the array for each row. The array is returned to the calling function.
The function wrapGetDataFromTable() wraps getDataFromTable() providing the table ID, that we are only looking in rows within the <tbody> tag and that we want output to the console. The array returned by getDataFromTable() is output so we can see the data structure. The function is set up to run each time you press the [print to console] button.
For a table that looks like (with header row):
The output is:
amOldQty=amOldQty0_text amNewQty=amNewQty0_text amS=on amR=off amCall=off amComm=amComm0_text
amOldQty=amOldQty1_text amNewQty=amNewQty1_text amS=off amR=on amCall=off amComm=amComm1_text
amOldQty=amOldQty2_text amNewQty=amNewQty2_text amS=off amR=off amCall=on amComm=amComm2_text
The array of row input data objects is:
[0] Object { amOldQty="amOldQty0_text", amNewQty="amNewQty0_text", amS="on", more...}
amOldQty: "amOldQty0_text"
amNewQty: "amNewQty0_text"
amS: "on"
amR: "off"
amCall: "off"
amComm: "amComm0_text"
[1] Object { amOldQty="amOldQty1_text", amNewQty="amNewQty1_text", amS="off", more...}
amOldQty: "amOldQty1_text"
amNewQty: "amNewQty1_text"
amS: "off"
amR: "on"
amCall: "off"
amComm: "amComm1_text"
[2] Object { amOldQty="amOldQty2_text", amNewQty="amNewQty2_text", amS="off", more...}
amOldQty: "amOldQty2_text"
amNewQty: "amNewQty2_text"
amS: "off"
amR: "off"
amCall: "on"
amComm: "amComm2_text"
JavaScript:
/**
* Runs through a table getting the values from <input> fields.
* It only looks in the <tbody>, not the <thead>
* #param tableId
* The DOM ID of the table from which we desire to obtain the
* input values.
* If tableId is not a string, then look in all table rows in all tables.
* #param keyAttr
* The attribute of the <input> which contains the value which will
* be used as the key for the key:value pair within the Object returned.
* This needs to be a value which is unique, wihin the table row.
* A normal use would be "id".
* If a value is duplicated a message is sent to the console and only
* the last value is kept.
* The default is "id".
* #param justBody
* If true, look only within the <tbody> tag, not any other part of
* the table (e.g. <thead>).
* The default is true.
* #param includeBlank
* Boolean indicating if the returned array should contain an entry for
* rows which are found to be blank.
* The default is true.
* #param consoleOutput
* Send a line to the console with the key:value pairs separated by
* tabs for each row.
* The default is false.
* #return Object
* Returns an Array of Objects with key:value pairs for the rows.
* If there were no <input>
* Copyright 2014 by Makyen.
* Released under the MPL 2.0. http://mozilla.org/MPL/2.0/.
*/
function getDataFromTable(tableId, keyAttr, justBody, includeBlank, consoleOutput) {
//This assumes that within the row each input has a unique attribute keyAttr.
//Set defaults:
var tableSelector = (typeof tableId === "string") ? "#" + tableId : "table";
keyAttr = (typeof keyAttr === "string") ? keyAttr : "id";
includeBlank = (typeof includeBlank === "boolean") ? includeBlank : true;
justBody = (typeof justBody === "boolean") ? justBody : true;
consoleOutput = (typeof consoleOutput === "boolean") ? consoleOutput : false;
var bodySelector = (justBody) ? " tbody" : "";
var toReturn = [];
var selector = tableSelector + bodySelector + ' tr';
$(selector).each(function () {
var inputs = {};
$(this).find('input').each(function () {
//Get the value for all inputs on this line.
var attrValue = $(this).attr(keyAttr);
if (typeof inputs[attrValue] !== "undefined") {
console.log("Warning: When attempting to get data from the table id=" //
+ tableId + " the value of the key attribute, " + keyAttr //
+ ", was not unique for value=" + attrValue);
}
//Get the value of the <input>.
if ($(this).is(':checkbox')) {
//Special case the checkboxes because .val() does not return
//the correct informaiton for them.
//First indicate that all checkboxes are off.
inputs[attrValue] = "off";
//Specifically determine if the current one is checked.
if ($(this).is(':checked')) {
inputs[attrValue] = "on";
}
} else {
//Add this input to the object
inputs[attrValue] = $(this).val();
}
});
var inputKeys = Object.keys(inputs);
if (inputKeys.length > 0) {
//There were <input> tags on this row.
var outputText = "";
if (consoleOutput) {
inputKeys.forEach(function (value) {
outputText += value + "=" + inputs[value] + "\t";
});
console.log(outputText);
}
toReturn.push(inputs);
} else {
//No <input> tags on this row
if (includeBlank) {
if (consoleOutput) {
console.log("A row without <input> tags was found.");
}
toReturn.push(inputs);
}
}
});
return toReturn;
}
function wrapGetDataFromTable() {
//This wraper is so the getDataFromTable() function remains
// generic. The wrapper merely defines which table from which to
// get the data,
// the attribute to use for unique keys = "id"
// to look only in the <tbody>
// to not include an object for the lines which are blank
// and output the row data to the console.
var toReturn = getDataFromTable("guides", "id", true, false, true);
if (typeof console.dir === "function") {
//Make sure console.dir() exists prior to using it.
console.log("The array of row input data objects is:");
console.dir(toReturn); //Let us see the Object in the console for checking.
}
return toReturn;
}
$('#to-console-button').click(wrapGetDataFromTable);
//The rest is setup for creating the table header and rows.
//It is only for testing.
function createRowsForTesting() {
const numRowsToCreate = 3;
var i;
var data = {
amCodeOld: "amCodeOld",
amOldPrice: "amOldPrice",
am: "am",
amCodeNew: "amCodeNew",
amNewPrice: "amNewPrice"
};
//Create the table
//First add a header.
$("#guides thead").append('<tr><th>amCodeOld_H</th>' //
+ '<th>amOldQty_H</th>' //
+ '<th>amOldPrice_H</th>' //
+ '<th>am_H</th>' //
+ '<th>amCodeNew_H</th>' //
+ '<th>amNewQty_H</th>' //
+ '<th>amNewPrice_H</th>' //
+ '<th>amS_H</th>' //
+ '<th>amR_H</th>' //
+ '<th>amCall_H</th>' //
+ '<th>amComm_H</th></tr>');
//Now the body rows.
for (i = 0; i < numRowsToCreate; i++) {
//From stackoverflow question: http://stackoverflow.com/questions/25998929/extract-data-from-a-tablesorter-table-with-javascript
$("#guides tbody").append('<tr><td id="amCodeOld">'+data.amCodeOld+'</td><td><input type="text" class="qty" id="amOldQty"/></td><td>'+data.amOldPrice+ //
'</td><td>'+data.am+ //
'</td><td>'+data.amCodeNew+'</td><td><input type="text" class="qty" id="amNewQty"/></td><td>'+data.amNewPrice+ //
'</td><td><input type="checkbox" id="amS" '+ //
'"/></td><td><input type="checkbox" id="amR"'+'"/></td><td><input type="checkbox" id="amCall"'+ //
'"/></td><td><input type="text" id="amComm" value="'+ //
'"/></td></tr>');
}
//*
//Fake having the table filled in, as I am tired of entering the input
//You have to try it without this, but with manual input in order to truly verify
var row = 0;
$('#guides tbody tr').each(function () {
$(this).find('#amOldQty').val("amOldQty" + row + "_text");
$(this).find('#amNewQty').val("amNewQty" + row + "_text");
$(this).find('#amComm').val("amComm" + row + "_text");
row++;
});
//*/
}
createRowsForTesting();
HTML:
<table class="tablesorter" id="guides">
<thead></thead>
<tbody></tbody>
</table>
<button type="button" id="to-console-button">print to console</button>
<!-- values to use to fill input:
amOldQty0_text
amOldQty1_text
amNewQty0_text
amNewQty1_text
amComm0_text
amComm1_text -->
Note: The selector ($("#guides").append('<tr>...) used in your line to add the table row to the table might be your problem. It currently adds your rows as the last elements in the <table>, not the last in the <tbody>. While the browser should compensate for this, it is possible that it is not doing so in your environment. Try $("#guides tbody").append('<tr>...
However, it appears more likely that the issue is a header row (or a row without input cells) in the <tbody>. The code now accounts for this possibility.

Related

How to create an array from the columns of a table in Javascript?

I have an HTML table like this:
SALES RENTS
ROME MILAN ROME MILAN MONEY
The HTML code is the following:
<TR>
<TD CLASS=HD1 COLSPAN=2>SALES</TD>
<TD CLASS=HD1 COLSPAN=2>RENTS</TD>
<TD CLASS=HDCOLSEP> </TD>
</TR>
<TR>
<TD>ROME</TD>
<TD>MILAN</TD>
<TD>ROME</TD>
<TD>MILAN</TD>
<TD>MONEY</TD>
</TR>
What I need, is to create with javascript an array like this:
(ROME-SALES, MILAN-SALES, ROME-RENTS, MILAN-RENTS, MONEY).
I have already created the array that contains the element of the first row.
Below you can find my code as it was in the past (At the beginning I needed just to take the elements of the first TR). Now I need to modify it and to create an array as specified before.
I don't know if it is clear from the table, but the first ROME and MILAN columns are referred to the column SALES, the second ROME and MILAN are referred to the RENTS column, while MONEY doesn't have any dependence.
Do you have any idea to do this?
Thanks in advance.
function getColumnsVal(id) {
var header = $("table#" + id + " thead tr:eq(1)");
var header_fields = $("td", header);
var header_vals = [];
header_fields.each(function(idx, val) {
var $$ = $(val);
header_vals.push($$.text());
});
return header_vals;
}
It is definitely possible to read values from the table cells. I edited the post a bit to illustrate the code.
I presume that HTML structure is rigid and you always have two rows of titles in the thead and somewhat random number of merged cells in the first row.
You’d want to match the number of columns in both rows, i.e. take the colspan number into account when traversing the cells.
Read both rows and generate strings by combining cell values in corresponding columns.
For example:
function readTableRow(row) {
var values = [];
$("td", row).each(function(index, field) {
var span = $(field).attr("colspan");
var val = $(field).text();
if (span && span > 1) {
for (var i = 0; i<span; i++ ) {
values.push(val);
}
} else {
values.push(val);
}
});
return values;
}
function getColumnsVal(id) {
// Read the first row, taking colspans into account
var first_row = $("table#" + id + " thead tr:eq(0)");
var first_row_vals = readTableRow(first_row);
// Read the second row, taking colspans into account
var second_row = $("table#" + id + " thead tr:eq(1)");
var second_row_vals = readTableRow(second_row);
if (first_row_vals.length != second_row_vals.length) {
return null;
}
var results = [];
for (var i = 0; i<first_row_vals.length; i++) {
results.push([first_row_vals[i].trim(), second_row_vals[i].trim()].filter(function (el) {return el}).join("-"));
}
return results;
}
function displayResults(results) {
var result = "RESULT: <br />";
results.forEach(function(r) {
result = result + r + "<br />";
});
$("#result").html(result);
}
displayResults(getColumnsVal("sample"));
JSFiddle:
https://jsfiddle.net/adanchenkov/n61dqfrs/

Output values from the array in desired order?

I have array with the multiple records, each record has ID, event name and customer name. My algorithm that I use does not seem to work the way I want. Before I start to loop through array I set the empty variable outside of the loop var eventId; then I started looping and inside of the loop I have multiple if statement. So if eventId is empty I want to append tr with event name and below that another tr with customer name. Then next time if my eventId is not empty I want to check if name match with the record, if they match I just want to append td inside of existing tr and output customer name. If they do not match I want to output new tr with event name and new tr with customer name. I have working example here, it looks like that I have Test 1 outputted twice on the screen and I do not want that. I should have event name only once and all customers for that event below that event.
Here is my code:
https://jsfiddle.net/dmilos89/t1v2r8jm/3/
Please if you see where my code is breaking let me know. I think that I have problem with appending elements or my algorithm is missing something. Thanks in advance.
Javascript code:
$( document ).ready(function() {
var myRes = [];
myRes.push({'myId':"42",'eventName':"Test 1",'rCustomer':"Mike, Allan"});
myRes.push({'myId':"42",'eventName':"Test 1",'rCustumer':"Gates, Bill"});
myRes.push({'myId':"19",'eventName':"Spring 2016",'rCustomer':"John, Bill"});
myRes.push({'myId':"19",'eventName':"Spring 2016",'rCustomer':"Adams, Ron"});
myRes.push({'myId':"31",'eventName':"May Test 1",'rCustomer':"Steve, Marie"});
myRes.push({'myId':"42",'eventName':"Test 1",'rCustomer':"Ariel, Bill"});
myRes.push({'myId':"32",'eventName':"Fall 2016",'rCustomer':"Ron, Nill"});
myRes.push({'myId':"32",'eventName':"Fall 2016",'rCustomer':"Kim, Alen"});
myRes.push({'myId':"32",'eventName':"Fall 2016",'rCustomer':"Will, Huges"});
myRes.push({'myId':"32",'eventName':"Fall 2016",'rCustomer':"Seth, Peak"});
var eventId = '';
var count = 1;
for(var i=0; i< myRes.length; i++){
if(eventId != ''){
if(eventId == myRes[i].myId){
$('#row_' + count).append('<td>'+myRes[i].rCustomer+'</td>');
}else{
eventId = myRes[i].myId;
count++;
$('.myReservation').append('<tr><td><b>Event: '+myRes[i].eventName+'</b></td></tr>');
$('.myReservation').append('<tr id="row_"'+count+'><td>'+myRes[i].rCustomer+'</td></tr>');
}
}else{
eventId = myRes[i].myId;
$('.myReservation').append('<tr><td><b>Event: '+myRes[i].eventName+'</b></td></tr>');
$('.myReservation').append('<tr id="row_"'+count+'><td>'+myRes[i].rCustomer+'</td></tr>');
}
}
});
HTML code:
<table>
<tbody class="myReservation">
</tbody>
</table>
You misplaced the double quote when inserting tr elements with ids.
Replace <tr id="row_"'+count+'> with <tr id="row_'+count+'">.
Update: this achieves what you (probably) want:
var found = [];
for (var i = 0; i < myRes.length; i++) {
if (found[myRes[i].myId] === 1) {
$('#row_' + myRes[i].myId).append('<td>' + myRes[i].rCustomer + '</td>');
} else {
found[myRes[i].myId] = 1;
$('.myReservation').append('<tr><td><b>Event: ' + myRes[i].eventName + '</b></td></tr>');
$('.myReservation').append('<tr id="row_' + myRes[i].myId + '"><td>' + myRes[i].rCustomer + '</td></tr>');
}
}
Your problem is that you append a new element on every loop:
First, you are testing if "eventId" is not empty. If it is, you are setting it with the current loop iteration "myId"
eventId = myRes[i].myId;
$('.myReservation').append('<tr><td><b>Event: '+myRes[i].eventName+'</b></td></tr>');
$('.myReservation').append('<tr id="row_"'+count+'><td>'+myRes[i].rCustomer+'</td></tr>');
According to your data, eventId is now 42 and your output is :
<tr><td><b>Event: Test 1</b></td></tr>
<tr id="row_" 1=""><td>Mike, Allan</td></tr>
(by the way, there is a syntax error here and it explains why the next iteration doesn't append data)
Then, on the next iteration, eventId is still 42 so you are doing this
$('#row_' + count).append('<td>'+myRes[i].rCustomer+'</td>');
The output is the same at this point (see above the syntax error on the first answer)
On the next loop iteration, myId will be now 19 so you are overriding eventid and appending tr in consequence
loops keep going...
Until myId is again equal to 42 :
myRes.push({'myId':"42",'eventName':"Test 1",'rCustomer':"Ariel, Bill"});
Here you doesn't want "Event: Test 1" appearing two times, but on the last loop iteration myId was 31
myRes.push({'myId':"31",'eventName':"May Test 1",'rCustomer':"Steve, Marie"});
So on the if test, eventId is NOT equal to myRes[i].myId, therefore your script appends a new tr with duplicate event.
One solution could be this one:
$( document ).ready(function() {
var myRes = [];
myRes.push({'myId':"42",'eventName':"Test 1",'rCustomer':"Mike, Allan"});
myRes.push({'myId':"42",'eventName':"Test 1",'rCustomer':"Gates, Bill"});
myRes.push({'myId':"19",'eventName':"Spring 2016",'rCustomer':"John, Bill"});
myRes.push({'myId':"19",'eventName':"Spring 2016",'rCustomer':"Adams, Ron"});
myRes.push({'myId':"31",'eventName':"May Test 1",'rCustomer':"Steve, Marie"});
myRes.push({'myId':"42",'eventName':"Test 1",'rCustomer':"Ariel, Bill"});
myRes.push({'myId':"32",'eventName':"Fall 2016",'rCustomer':"Ron, Nill"});
myRes.push({'myId':"32",'eventName':"Fall 2016",'rCustomer':"Kim, Alen"});
myRes.push({'myId':"32",'eventName':"Fall 2016",'rCustomer':"Will, Huges"});
myRes.push({'myId':"32",'eventName':"Fall 2016",'rCustomer':"Seth, Peak"});
myRes.forEach(function(value, key) {
var table = $('.myReservation2');
var elemInTable = table.find('tr#row_'+value.myId); // $('#row_42') as an example
// If row doesn't exists, create it with it's customers
if(!elemInTable.length) {
table.append('<tr id="row_'+value.myId+'"><td>'+value.eventName+'</td></tr>');
elemInTable = table.find('tr#row_'+value.myId);
elemInTable.after('<tr><td>'+value.rCustomer+'</td></tr>');
}
// If it already exists, simply add the customers
else {
elemInTable.after('<tr><td>'+value.rCustomer+'</td></tr>');
}
})
});
However, I don't think it is the proper way to do it, actually, I think that you should reorganize your data (if your context permits it) like this :
myRes.push({'myId':"42",'eventName':"Test 1",'rCustomer': ["Mike, Allan", "Gates", "Bill", "..."]})
(sorry for my english by the way)

How to get complete data from (html) bootstrap table when pagination is turned on

I am using bootstrap table in my web page and want to get complete textual data from all table cells, when pagination is on. I have tried the following method and it returns all the data:
var data = $('#' + tableID).bootstrapTable('getData')
Now when i traverse data object to get value for every cell it works fine but, for those cells which have some nested html , for example:
<td class="danger">cell 4</td>
<td>
google
</td>
Now, in this case, i want to get value for second cell as google but it returns me whole html as
google
Any idea, how i can get only textual value.
I can't do any server side operation, I have to achieve this using javascript/jquery. I have also tried using jquery:
function getColData(tableID,colIndex) {
var colArray = $('#' + tableID + ' td:nth-child'+'('+colIndex+')').map(function(){
return $(this).text();
}).get();
return colArray
}
it returns data correctly but only which is visible on active page and i want all the data.
Based on your file on JSFiddle I have modified the JS part as follows, this will get you the text on every td(i.e. text or text content) and not the values of their attributes. Basically this traverses through the DOM searching for tags embedded in ones - except for those on the table header - then obtains the text value.
var table = $('#table'), button = $('#button');
button.click(function() {
var data = [];
table.find('tr:not(:first)').each(function(i, row) {
var cols = [];
$(this).find('td').each(function(i, col) {
cols.push($(this).text());
});
data.push(cols);
});
alert(data);
});
You can see it in action here
UPDATE:
This will get you all data regardless of pagination, also it will strip tags and nested tags.
var table = $('#table'), button = $('#button');
button.click(function() {
var messedData = table.bootstrapTable('getData');
var data = [];
$.each(messedData, function(i, row) {
var rowData = {
'name': row['0'],
'star': row['1'],
'forks': row['2'],
'desc': row['3'],
}
for (prop in rowData) {
var tmp = document.createElement("div");
tmp.innerHTML = rowData[prop];
rowData[prop] = tmp.textContent || tmp.innerText || "";
}
data.push(rowData);
});
console.log(data);
});
You can see it here
Since the actual data is coming in as a string, I don't think bootstrap-table can't differentiate it from the other data. The simple solution I can think of is to use substring() to extract the data from the cells that contain custom html.
http://jsfiddle.net/vwg5Lefz/
The alternative is to go through the generated table <td> and use text() to get the text data from the cells.
http://jsfiddle.net/n0djy60v/

How to add columns from a Common Table to New Table in Enterprise Architect

I am using Enterprise Architect 10 tool for design schema for SQL Database.
Every time a new table is added to EA Schema (lets say with columns 'Name' and 'Description'), common columns (UserId, Status, Role, 15 such columns) has to be added to the new table.
Destination table columns = Source table columns + Common table Columns.
The Destination table shall have the columns - Name, Description, UserId, Status and Role.
I have more than 100 tables and can't add manually all the common columns to each table.
Is there any provision or scripting (javascript or vbscript) can perform this action when I add new table and execute the script ?
-- Added this code after trying...
!INC Local Scripts.EAConstants-JScript
/*
* Script Name:
* Author:
* Purpose:
* Date:
*/
function main()
{
// TODO: Enter script code here!
// Show the script output window
Repository.EnsureOutputVisible( "Script" );
// Get the currently selected element in the tree to work on
var theElement as EA.Element;
var theDestElement as EA.Element;
theElement = Repository.GetTreeSelectedObject();
//theDestElement = Repository.GetTreeSelectedObject();
//theDestElement.Name='Newtable1';
if( theElement != null && theElement.ObjectType == otElement)
{
Session.Output( "Working on element '" + theElement.Name + "' (Type=" + theElement.Type +
", ID=" + theElement.ElementID + ")" );
var srcAttributes as EA.Collection;
srcAttributes = theElement.Attributes;
theDestElement = Repository.
Session.Output(theDestElement);
theDestElement.Name='NewTable1';
var dstAttributes as EA.Collection;
dstAttributes = theDestElement.Attributes;
newAttribute = null;
// List attributes
for ( var i = 0 ; i < srcAttributes.Count ; i++ )
{
var currentAttribute as EA.Attribute;
currentAttribute = srcAttributes.GetAt( i );
dstAttributes.AddNew(currentAttribute.,currentAttribute);
//theDestElement
Session.Output( "Attribute: " + currentAttribute.Name );
}
Session.Output( "Done!" );
}
else
{
// No item selected in the tree, or the item selected was not an element
Session.Prompt( "This script requires an element be selected in the Project Browser.\n" +
"Please select an element in the Project Browser and try again.", promptOK );
}
}
main();
I could able to select all the attributes of one table using
theElement = Repository.GetTreeSelectedObject();
but the destination table to which all the attributes to be copied is unclear to me.
Please help.
Thank you,
Ramm
To start with, you could have used a template package, where your common table with columns is placed.
TO do it retrospectively, you can achieve it using a the internal scripting support. Goto Tools-> Scripting in EA and look for "VBScript - Attribute Lifecycle Example" or 'JScript - Attribute Lifecycle Example' to see how attributes are added / removed from an element.

Message always appears in first row?

I have a delete function within a function where if the user clicks on the "Delete" button, it displays a message stating that a file has been deleted. The code which does this is below:
$("#imagemsg").html(data);
But the problem is that let's say that I have 4 table rows and I delete a file in the 3rd row, the message should be displayed in the 3rd row only but instead it is displayed in the first row. Another example is that let's say that I have 8 table rows and I delete a file in the 6th row, the message should be displayed in the 6th row only but instead it is displayed in the first row.
Why is the message that is suppose to appear after a file is deleted is always displayed in the first row and not within the row the file has been deleted from?
Below is full code:
var counter = 0;
counter++;
function stopImageUpload(success, imagefilename){
var result = '';
if (success == 1){
result = '<span id="imagemsg'+counter+'">The file was uploaded successfully!</span><br/><br/>';
$('.listImage').eq(window.lastUploadImageIndex).append('<div>' + htmlEncode(imagefilename) + '<button type="button" class="deletefileimage" image_file_name="' + imagefilename + '">Remove</button><br/><hr/></div>');
}
else {
result = '<span id="imageemsg">There was an error during file upload!</span><br/><br/>';
}
$(".deletefileimage").on("click", function(event) {
var image_file_name = $(this).attr('image_file_name');
jQuery.ajax("deleteimage.php?imagefilename=" + image_file_name)
.done(function(data) {
$("#imagemsg" + counter).html(data);
});
$(this).parent().remove();
});
return true;
}
Below is the deleteimage.php script where the delete message is retrieved from:
<?php
$image_file_name = $_GET["imagefilename"];
echo "$image_file_name was Deleted";
unlink("ImagesFilesFolder/$image_file_name");
?>
The problem seems to be this:
.done(function(data) {
$("#imagemsg" + counter).html(data);
You set counter like this
var counter = 0;
counter++;
But you never seem to refer to the variable again. In any case, this variable is global - the command above will always target the ID with the current number of the counter, so it will not target the tr corresponding to the clicked button.
Since you use
$(this).parent().remove();
I assume that the parent is the tr concerned? In this case you could use a class instead of an ID 'imagemsg' and then do
$(this).parent().find(".imagemsg").html(data);
This would target the message inside the same row of the button.

Categories