Javascript function executed before previous function is done - javascript
I have a webpage that contains a couple of tables.
At the beginning it checks the language and sets the corresponding translations.
At the end of the html file (after all the elements are loaded) the translations are set using javascript.
After that variables are requested via wifi.
This is what I see when I load the page.
The elements that don't need translation are shown before set_language_for_raw_data_() is executed.
I have checked Chrome's developer tools and saw this.
It shows that set_language_for_raw_data_() is executed before Raw_Data_Requests(), but it does not show until Raw_Data_Requests() is done.
Is there a way to show the translations before Raw_Data_Requests() is executed?
Raw_data.html:
<!DOCTYPE HTML>
<html>
<head>
<script>
checkLanguage(); //load language setting or use system default
</script>
</head>
<body>
<div id="site_unresponsive">
<div id="inhalt">
<h2 class="middle white_font expander" onclick="expander('raw_data')" id="raw_data_title"></h2>
<ul class="hidden" id="raw_data">
<table cellspacing="0" id="actual_meas">
<tr>
<td id="loading" style="display:none">Error: Connection lost</td>
</tr>
<tr class="odd">
<td></td>
<td id="Meas(0)">..</td>
</tr>
<tr>
<td></td>
<td id="Meas(1)">..</td>
</tr>
<tr class="odd">
<td>Ubat</td>
<td id="Meas(2)">..</td>
</tr>
<tr>
<td>Ibat</td>
<td id="Meas(5)">..</td>
</tr>
<tr class="odd">
<td>U PWM</td>
<td id="Meas(9)">..</td>
</tr>
<tr>
<td style="color:darkorange">LED</td>
<td id="Meas(11)">•</td>
</tr>
<tr class="odd">
<td style="color:green">LED</td>
<td id="Meas(12)">•</td>
</tr>
<tr>
<td></td>
<td id="Meas(19)">..</td>
</tr>
<tr class="odd">
<td></td>
<td id="Meas(20)">..</td>
</tr>
<tr>
<td></td>
<td id="Meas(100)">..</td>
</tr>
</table>
</ul>
<h2 class="middle white_font expander" onclick="expander('version')" id="version_title"></h2>
<div class="hidden" id="version">
<table cellspacing="0" class="data_tab" id="version_details">
<tr>
<th></th>
</tr>
<tr class="odd">
<td id="device_version">~DeviceVersion~</td>
</tr>
</table>
</div>
<h2 class="middle white_font expander" onclick="expander('cumulatives')" id="cumulatives_title"></h2>
<ul class="hidden" id="cumulatives">
<li class="w300">
<table cellspacing="0" id="cuml">
<tr class="odd">
<td></td>
<td>~Cumulatives(0)~</td>
</tr>
<tr>
<td></td>
<td>~Cumulatives(1)~</td>
</tr>
<tr class="odd">
<td></td>
<td>~Cumulatives(2)~</td>
</tr>
<tr>
<td></td>
<td>~Cumulatives(3)~</td>
</tr>
<tr class="odd">
<td></td>
<td>~Cumulatives(4)~</td>
</tr>
<tr>
<td></td>
<td>~Cumulatives(5)~</td>
</tr>
<tr class="odd">
<td></td>
<td>~Cumulatives(6)~</td>
</tr>
<tr>
<td></td>
<td>~Cumulatives(7)~</td>
</tr>
<tr class="odd">
<td></td>
<td>~Cumulatives(8)~</td>
</tr>
<tr>
<td></td>
<td style="white-space:nowrap;">~Cumulatives(14)~</td>
</tr>
</table>
</li>
</ul>
<h2 class="middle white_font expander" onclick="expander('error')" id="error_title"></h2>
<div class="hidden" id="error" style="overflow-x:scroll;overflow-y:hidden;white-space:nowrap;">
<table cellspacing="0" class="data_tab" id="error_history">
<tr>
<th>Nr</th>
<th></th>
<th></th>
<th></th>
<th>U</th>
<th>I</th>
<th>PWM</th>
<th>T</th>
</tr>
<tr class="odd">
<td>1</td>
<td>~ErrorHistory(0,2,0)~</td>
<td>~ErrorHistory(0,3,0)~</td>
<td>~ErrorHistory(0,4,0)~</td>
<td>~ErrorHistory(0,5,0)~</td>
<td>~ErrorHistory(0,6,0)~</td>
<td>~ErrorHistory(0,7,0)~</td>
<td>~ErrorHistory(0,8,0)~</td>
</tr>
<tr>
<td>2</td>
<td>~ErrorHistory(1,2,0)~</td>
<td>~ErrorHistory(1,3,0)~</td>
<td>~ErrorHistory(1,4,0)~</td>
<td>~ErrorHistory(1,5,0)~</td>
<td>~ErrorHistory(1,6,0)~</td>
<td>~ErrorHistory(1,7,0)~</td>
<td>~ErrorHistory(1,8,0)~</td>
</tr>
<tr class="odd">
<td>3</td>
<td>~ErrorHistory(2,2,0)~</td>
<td>~ErrorHistory(2,3,0)~</td>
<td>~ErrorHistory(2,4,0)~</td>
<td>~ErrorHistory(2,5,0)~</td>
<td>~ErrorHistory(2,6,0)~</td>
<td>~ErrorHistory(2,7,0)~</td>
<td>~ErrorHistory(2,8,0)~</td>
</tr>
<tr>
<td>4</td>
<td>~ErrorHistory(3,2,0)~</td>
<td>~ErrorHistory(3,3,0)~</td>
<td>~ErrorHistory(3,4,0)~</td>
<td>~ErrorHistory(3,5,0)~</td>
<td>~ErrorHistory(3,6,0)~</td>
<td>~ErrorHistory(3,7,0)~</td>
<td>~ErrorHistory(3,8,0)~</td>
</tr>
<tr class="odd">
<td>5</td>
<td>~ErrorHistory(4,2,0)~</td>
<td>~ErrorHistory(4,3,0)~</td>
<td>~ErrorHistory(4,4,0)~</td>
<td>~ErrorHistory(4,5,0)~</td>
<td>~ErrorHistory(4,6,0)~</td>
<td>~ErrorHistory(4,7,0)~</td>
<td>~ErrorHistory(4,8,0)~</td>
</tr>
<tr>
<td>6</td>
<td>~ErrorHistory(5,2,0)~</td>
<td>~ErrorHistory(5,3,0)~</td>
<td>~ErrorHistory(5,4,0)~</td>
<td>~ErrorHistory(5,5,0)~</td>
<td>~ErrorHistory(5,6,0)~</td>
<td>~ErrorHistory(5,7,0)~</td>
<td>~ErrorHistory(5,8,0)~</td>
</tr>
<tr class="odd">
<td>7</td>
<td>~ErrorHistory(6,2,0)~</td>
<td>~ErrorHistory(6,3,0)~</td>
<td>~ErrorHistory(6,4,0)~</td>
<td>~ErrorHistory(6,5,0)~</td>
<td>~ErrorHistory(6,6,0)~</td>
<td>~ErrorHistory(6,7,0)~</td>
<td>~ErrorHistory(6,8,0)~</td>
</tr>
<tr>
<td>8</td>
<td>~ErrorHistory(7,2,0)~</td>
<td>~ErrorHistory(7,3,0)~</td>
<td>~ErrorHistory(7,4,0)~</td>
<td>~ErrorHistory(7,5,0)~</td>
<td>~ErrorHistory(7,6,0)~</td>
<td>~ErrorHistory(7,7,0)~</td>
<td>~ErrorHistory(7,8,0)~</td>
</tr>
<tr class="odd">
<td>9</td>
<td>~ErrorHistory(8,2,0)~</td>
<td>~ErrorHistory(8,3,0)~</td>
<td>~ErrorHistory(8,4,0)~</td>
<td>~ErrorHistory(8,5,0)~</td>
<td>~ErrorHistory(8,6,0)~</td>
<td>~ErrorHistory(8,7,0)~</td>
<td>~ErrorHistory(8,8,0)~</td>
</tr>
<tr>
<td>10</td>
<td>~ErrorHistory(9,2,0)~</td>
<td>~ErrorHistory(9,3,0)~</td>
<td>~ErrorHistory(9,4,0)~</td>
<td>~ErrorHistory(9,5,0)~</td>
<td>~ErrorHistory(9,6,0)~</td>
<td>~ErrorHistory(9,7,0)~</td>
<td>~ErrorHistory(9,8,0)~</td>
</tr>
</table>
</div>
<h2 class="middle white_font expander" onclick="expander('cycle')" id="cycle_title"></h2>
<div class="hidden" id="cycle" style="overflow-x:scroll;overflow-y:hidden;white-space:nowrap;">
<table cellspacing="0" class="data_tab" id="cycle_history" style="layout:fixed">
<tr>
<th>Nr</th>
<th>Ah</th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
<tbody id="extended_cycles">
</tbody>
</table>
</div>
<h2 class="middle white_font expander" onclick="expander('error_tab')" id="error_tab_title">EEPROM data</h2>
<div class="hidden" id="error_tab" style="overflow-x:scroll;overflow-y:hidden;white-space:nowrap;">
<table cellspacing="0" class="data_tab" id="eeprom_data">
<tr>
<td>00: ~EEPROM(0)~</td>
</tr>
<tr>
<td>10: ~EEPROM(1)~</td>
</tr>
<tr>
<td>20: ~EEPROM(2)~</td>
</tr>
<tr>
<td>30: ~EEPROM(3)~</td>
</tr>
<tr>
<td>40: ~EEPROM(4)~</td>
</tr>
<tr>
<td>50: ~EEPROM(5)~</td>
</tr>
<tr>
<td>60: ~EEPROM(6)~</td>
</tr>
<tr>
<td>70: ~EEPROM(7)~</td>
</tr>
<tr>
<td>80: ~EEPROM(8)~</td>
</tr>
<tr>
<td>90: ~EEPROM(9)~</td>
</tr>
<tr>
<td>A0: ~EEPROM(10)~</td>
</tr>
<tr>
<td>B0: ~EEPROM(11)~</td>
</tr>
<tr>
<td>C0: ~EEPROM(12)~</td>
</tr>
<tr>
<td>D0: ~EEPROM(13)~</td>
</tr>
<tr>
<td>E0: ~EEPROM(14)~</td>
</tr>
<tr>
<td>F0: ~EEPROM(15)~</td>
</tr>
</table>
</div>
<br>
</div>
<script>
set_language_for_raw_data_();
Raw_Data_Requests();
</script>
</div>
</body>
</html>
set_language_for_raw_data_():
document.getElementById("menu_title").innerHTML = d12;
document.getElementById("raw_data_title").innerHTML = t5;
document.getElementById("version_title").innerHTML = t7;
document.getElementById("cumulatives_title").innerHTML = t8;
document.getElementById("error_title").innerHTML = t9;
document.getElementById("cycle_title").innerHTML = t11;
checkLanguage():
language = getCookie("language");
if (language == "")
applySystemLanguage();
setTranslations(language);
setTranslations(language):
switch (language){
default:
case "en":
d12="Show RAW data";
t5="Real time measurements";
t7="Version";
t8="Cumulatives";
t9="Error history";
t11="Cycle history";
break;
}
With rxjs you can do someting like
set_language_for_raw_data_(){
//Your code
return Observable.of(language );
}
set_language_for_raw_data_().subscribe(() => Raw_Data_Requests());
Here, you wait until the set_language_for_raw_data_ returns an observable. You can then subscribe on it. From the moment the observable is completed, it will call the next function.
The complete explanation you can find on http://reactivex.io/, But take some time and check how Observables are working
What I did to make the translations show was add a timeout:
set_language_for_raw_data_();
setTimeout(function(){
Raw_Data_Requests();
}, 20);
Apparently this was enough time for the html to be shown/updated.
Related
Copy Paste multiple rows from CSV to input fields in an HTML Form
I am trying to copy paste data from a CSV file to an HTML form using Jquery. My form has an array of input fields so I can do multiple inserts at the same time on submit Now, suppose I copy paste multiple rows from a CSV file to the second column of the first row in the form, the first row of the form shows data correctly but in the second row, the data pasted starts from the first column itself, wherein it should start from the second row as it did on the first row of the form CSV rows and cells 1 4 a 2 5 b 3 6 c Screenshot function csv_paste_datagrid(event){ $(document).ready(function() { $('input').bind('paste', null, function (e) { $this = $(this); setTimeout(function () { var columns = $this.val().split(/\s+/); var i; var input = $this; for (i = 0; i < columns.length; i++) { input.val(columns[i]); if( i % 3 !== 2){ input = input.parent().parent().parent().parent().parent().next().find('input'); } else{ input = input.parent().parent().parent().parent().parent().parent().next().find('input').first(); } } }, 0); }); }); HTML <form style="width : 100%;" id="system_validations" name="system_validations" accept-charset="utf-8" method="POST" class="form-control" enctype="multipart/form-data"> <table style="display : inline;width : 100%;"></table> <table id="" class="system_form_tables_parent"> <tbody> <tr> <th></th> <td> <table id="form_table[0]" class="system_form_tables_child" style="margin-left:auto; margin-right:auto;"> <tbody> <tr> <td style=" " id="container_validation_options[0]"> <table> <tbody> <tr id="tr_validation_options[0]" style=""> <th class="th_class1"><span class=""> validation_options </span></th> </tr> <tr> <td class="td_class"> <input type="text" id="validation_options[0]" name="validation_options[0]" placeholder="" class="" value=""> </td> </tr> <tr> <th></th> </tr> <tr> <th></th> </tr> <tr> <td class="val_error"></td> </tr> </tbody> </table> </td> <td style=" " id="container_validation_display[0]"> <table> <tbody> <tr id="tr_validation_display[0]" style=""> <th class="th_class1"><span class=""> validation_display </span></th> </tr> <tr> <td class="td_class"> <input type="text" id="validation_display[0]" name="validation_display[0]" placeholder="" class="" value=""> </td> </tr> <tr> <th></th> </tr> <tr> <th></th> </tr> <tr> <td class="val_error"></td> </tr> </tbody> </table> </td> <td style=" " id="container_blocked_modules[0]"> <table> <tbody> <tr id="tr_blocked_modules[0]" style=""> <th class="th_class1"><span class=""> blocked_modules </span></th> </tr> <tr> <td class="td_class"> <input type="text" id="blocked_modules[0]" name="blocked_modules[0]" placeholder="" class="" value=""> </td> </tr> <tr> <th></th> </tr> <tr> <th></th> </tr> <tr> <td class="val_error"></td> </tr> </tbody> </table> </td> </tr> <tr> <td style=" " id="container_validation_options[0]"> <table> <tbody> <tr id="tr_validation_options[1]" style=""> <th class="th_class1"><span class=""> validation_options </span></th> </tr> <tr> <td class="td_class"> <input type="text" id="validation_options[1]" name="validation_options[1]" placeholder="" class="" value=""> </td> </tr> <tr> <th></th> </tr> <tr> <th></th> </tr> <tr> <td class="val_error"></td> </tr> </tbody> </table> </td> <td style=" " id="container_validation_display[0]"> <table> <tbody> <tr id="tr_validation_display[1]" style=""> <th class="th_class1"><span class=""> validation_display </span></th> </tr> <tr> <td class="td_class"> <input type="text" id="validation_display[1]" name="validation_display[1]" placeholder="" class="" value=""> </td> </tr> <tr> <th></th> </tr> <tr> <th></th> </tr> <tr> <td class="val_error"></td> </tr> </tbody> </table> </td> <td style=" " id="container_blocked_modules[0]"> <table> <tbody> <tr id="tr_blocked_modules[1]" style=""> <th class="th_class1"><span class=""> blocked_modules </span></th> </tr> <tr> <td class="td_class"> <input type="text" id="blocked_modules[1]" name="blocked_modules[1]" placeholder="" class="" value=""> </td> </tr> <tr> <th></th> </tr> <tr> <th></th> </tr> <tr> <td class="val_error"></td> </tr> </tbody> </table> </td> </tr> <tr style=""> <td style="text-align : left;padding-left:0.5em"> <table id="submit_table"> <tbody> <tr> <td><input type="button" class="common_button" id="system_validations_back" name="system_validations_back" style="" value="Back" onclick="" title="Back"> <input type="reset" class="common_button" id="system_validations_reset" name="system_validations_reset" style="" value="Reset" title="Reset"> <input type="button" class="common_button" id="submit" name="system_validations_submit" onclick="" style="" value="Submit" title="Submit"> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> </form>
Right, I had to clear a few double ids from your HTML first and also added a class attribute (contTD) to your "main" container <td>s. After that the whole thing fell into place fairly easily: to prevent the actual TSV text from being pasted directly into the first input field I used e.preventDefault() I then used .split() twice on the TSV string to turn it into the 2D array vals I identified the .closest() td.contTD element (--> td) and its column and row numbers (col and row) by finding the .index() of td and its parent row. starting form the .closest('tbody')I then worked my way down again through the .slice()of rows starting with row and its (sliced) child input elements starting at column col. in an .each() loop I then assigned the value of the vals-array to the input element, but only if val[i][j] exists! There could be further improvements to the script, as it will run trhough all <tr>s of the table from row row to the end. But I hope this is a starting point for you and has given you a few more ideas on how to work with jquery. In my script I used a delegated paste-event-binding to the <form> element. This should work well with a dynamic table. I did not pack it into an extra function. But, of course, when you use it in your site it should be placed in your onload section. And lastly: in my second .split() I am looking for a tab character as column separator, so this example will work with a TSV file format. If you want to apply it on space or comma separated values you should adapt the regular expression there to something like /\s/ or /,/ . $('form').on('paste', 'input', function (e) { e.preventDefault(); // do not paste the contents into the first cell ... // convert TSV from clipboard into a 2D array: let vals=event.clipboardData.getData('text').trim().split(/\r?\n */).map(r=>r.split(/\t/)); let td=$(this).closest('.contTD'); // closest container TD and work from there let col=td.index(), row=td.parent().index(), tbdy=td.closest('tbody'); // modify input fields of rows >= row and columns >= col: tbdy.children('tr').slice(row).each((i,tr)=>{ $(tr).find('td input:text').slice(col).each((j,ti)=>{ if(vals[i]&&vals[i][j]!=null) ti.value=vals[i][j] } )}); }) <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form style="width : 100%;" id="system_validations" name="system_validations" accept-charset="utf-8" method="POST" class="form-control" enctype="multipart/form-data"> <label>sample data for copying and pasting via clipboard:</label> <table> <tr><td>1</td><td>4</td><td>a</td></tr> <tr><td>2</td><td>5</td><td>b</td></tr> <tr><td>3</td><td>6</td><td>c</td></tr> </table> <table id="" class="system_form_tables_parent"> <tbody> <tr> <th></th> <td> <table id="form_table[0]" class="system_form_tables_child" style="margin-left:auto; margin-right:auto;"> <tbody> <tr> <td class="contTD"><table> <tbody><tr><th class="th_class1"><span class="">extra column</span></th></tr> <tr><td class="td_class"><input type="text" value="00A"> </td></tr> <tr><th></th></tr> <tr><th></th></tr> <tr><td class="val_error"></td></tr></tbody> </table></td> <td class="contTD" id="container_validation_options[0]"> <table> <tbody> <tr id="tr_validation_options[0]"> <th class="th_class1"><span class=""> validation_options </span></th> </tr> <tr> <td class="td_class"> <input type="text" id="validation_options[0]" name="validation_options[0]" placeholder="" class="" value="01"> </td> </tr> <tr> <th></th> </tr> <tr> <th></th> </tr> <tr> <td class="val_error"></td> </tr> </tbody> </table> </td> <td class="contTD" id="container_validation_display[0]"> <table> <tbody> <tr id="tr_validation_display[0]"> <th class="th_class1"><span class=""> validation_display </span></th> </tr> <tr> <td class="td_class"> <input type="text" id="validation_display[0]" name="validation_display[0]" placeholder="" class="" value="02"> </td> </tr> <tr> <th></th> </tr> <tr> <th></th> </tr> <tr> <td class="val_error"></td> </tr> </tbody> </table> </td> <td class="contTD" id="container_blocked_modules[0]"> <table> <tbody> <tr id="tr_blocked_modules[0]"> <th class="th_class1"><span class=""> blocked_modules </span></th> </tr> <tr> <td class="td_class"> <input type="text" id="blocked_modules[0]" name="blocked_modules[0]" placeholder="" class="" value="03"> </td> </tr> <tr> <th></th> </tr> <tr> <th></th> </tr> <tr> <td class="val_error"></td> </tr> </tbody> </table> </td> </tr> <tr> <td class="contTD"><table> <tbody><tr><th class="th_class1"><span class="">extra column</span></th></tr> <tr><td class="td_class"><input type="text" value="00A"> </td></tr> <tr><th></th></tr> <tr><th></th></tr> <tr><td class="val_error"></td></tr></tbody> </table></td> <td class="contTD" id="container_validation_options[1]"> <table> <tbody> <tr id="tr_validation_options[1]"> <th class="th_class1"><span class=""> validation_options </span></th> </tr> <tr> <td class="td_class"> <input type="text" id="validation_options[1]" name="validation_options[1]" placeholder="" class="" value="04"> </td> </tr> <tr> <th></th> </tr> <tr> <th></th> </tr> <tr> <td class="val_error"></td> </tr> </tbody> </table> </td> <td class="contTD" id="container_validation_display[1]"> <table> <tbody> <tr id="tr_validation_display[1]"> <th class="th_class1"><span class=""> validation_display </span></th> </tr> <tr> <td class="td_class"> <input type="text" id="validation_display[1]" name="validation_display[1]" placeholder="" class="" value="05"> </td> </tr> <tr> <th></th> </tr> <tr> <th></th> </tr> <tr> <td class="val_error"></td> </tr> </tbody> </table> </td> <td class="contTD" id="container_blocked_modules[1]"> <table> <tbody> <tr id="tr_blocked_modules[1]"> <th class="th_class1"><span class=""> blocked_modules </span></th> </tr> <tr> <td class="td_class"> <input type="text" id="blocked_modules[1]" name="blocked_modules[1]" placeholder="" class="" value="06"> </td> </tr> <tr> <th></th> </tr> <tr> <th></th> </tr> <tr> <td class="val_error"></td> </tr> </tbody> </table> </td> </tr> <tr> <td style="text-align : left;padding-left:0.5em"> <table id="submit_table"> <tbody> <tr> <td><input type="button" class="common_button" id="system_validations_back" name="system_validations_back" value="Back" title="Back"> <input type="reset" class="common_button" id="system_validations_reset" name="system_validations_reset" value="Reset" title="Reset"> <input type="button" class="common_button" id="submit" name="system_validations_submit" value="Submit" title="Submit"> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> </form>
how can i collapse all table rows
i have table rows inside a table, and also another nested table rows inside each row, my problem is i can collapse and expand in the nested table rows, but when i try to expand in the main table, only the first row is expanded the rest ones are expanded by default when i launch the program, how can i fix it. this is my code tbody.collapse.in { display: table-row-group; } .tigray { background-color: darkcyan; } .zoba { background-color: forestgreen; } <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <!-- jQuery library --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <!-- Latest compiled JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <table class="table table-bordered table-sm "> <thead class="thead-dark"> <tr> <th></th> <th>head1</th> <th>head2</th> </tr> </thead> <tbody> <tr class="clickable" data-toggle="collapse" data-target="#group-of-rows-1" aria-expanded="false" aria-controls="group-of-rows-1"> <td class="tigray">title1</td> <td class="tigray">35</td> <td class="tigray">35</td> </tr> </tbody> <tbody id="group-of-rows-1" class="collapse"> <tr class="clickable" data-toggle="collapse" data-target="#group-of-rows-2" aria-expanded="false" aria-controls="group-of-rows-2"> <td class="zoba">nested 1</td> <td class="zoba">29</td> <td class="zoba">29</td> <tbody id="group-of-rows-2" class="collapse"> <tr class="table-warning"> <td>nested title1</td> <td>13</td> <td>13</td> </tr> <tr class="table-warning"> <td>nested title2</td> <td>18</td> <td>13</td> </tr> <tr class="table-warning"> <td>nested title 3</td> <td>32</td> <td>13</td> </tr> </tbody> </tr> <tr class="clickable" data-toggle="collapse" data-target="#group-of-rows-3" aria-expanded="false" aria-controls="group-of-rows-3"> <td class="zoba">nested 2</td> <td class="zoba">29</td> <td class="zoba">29</td> <tbody id="group-of-rows-3" class="collapse"> <tr class="table-warning"> <td>nested title4</td> <td>13</td> <td>13</td> </tr> <tr class="table-warning"> <td>nested title5</td> <td>18</td> <td>13</td> </tr> <tr class="table-warning"> <td>nested title 6</td> <td>32</td> <td>13</td> </tr> </tbody> </tr> </tbody> </table>
You can not use tbody inside another tbody it is not legal HTML. Follow this code ↓↓ <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <!-- jQuery library --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <!-- Latest compiled JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <table class="table table-bordered table-sm "> <thead class="thead-dark"> <tr> <th></th> <th>head1</th> <th>head2</th> </tr> </thead> <tbody> <tr class="clickable" data-toggle="collapse" data-target=".group-of-rows-1" aria-expanded="false" aria-controls="group-of-rows-1"> <td class="tigray">title1</td> <td class="tigray">35</td> <td class="tigray">35</td> </tr> </tbody> <tbody id="" class="collapse group-of-rows-1"> <tr class="clickable" data-toggle="collapse" data-target="#group-of-rows-2" aria-expanded="false" aria-controls="group-of-rows-2"> <td class="zoba">nested 1</td> <td class="zoba">29</td> <td class="zoba">29</td> </tr> </tbody> <tbody id="group-of-rows-2" class="collapse"> <tr class="table-warning"> <td>nested title1</td> <td>13</td> <td>13</td> </tr> <tr class="table-warning"> <td>nested title2</td> <td>18</td> <td>13</td> </tr> <tr class="table-warning"> <td>nested title 3</td> <td>32</td> <td>13</td> </tr> </tbody> <tbody id="" class="collapse group-of-rows-1"> <tr class="clickable" data-toggle="collapse" data-target="#group-of-rows-3" aria-expanded="false" aria-controls="group-of-rows-3"> <td class="zoba">nested 2</td> <td class="zoba">29</td> <td class="zoba">29</td> </tr> </tbody> <tbody id="group-of-rows-3" class="collapse"> <tr class="table-warning"> <td>nested title4</td> <td>13</td> <td>13</td> </tr> <tr class="table-warning"> <td>nested title5</td> <td>18</td> <td>13</td> </tr> <tr class="table-warning"> <td>nested title 6</td> <td>32</td> <td>13</td> </tr> </tbody> </table> However, you are allowed to have multiple TBODY elements in a single TABLE element, so you could do this: <table> <tbody> <tr> <td>...</td> <td>...</td> </tr> </tbody> <tbody > <tr> <td>...</td> <td>...</td> </tr> </tbody> <tbody> <tr> <td>...</td> <td>...</td> </tr> </tbody> </table>
Nested table with data tables
I have the following nested HTML table. I am using the Datatables API to make my tables searchable. One problem I have faced is with the nested tables I'm not sure how to make the nested tables searchable? I have tried adding additional ID tags to the nested tables HTML code and adding that into the dataTables JS code call but that did not work. Any idea how to make the sub-tables searchable? <link href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css" rel="stylesheet" /> <script src="https://code.jquery.com/jquery-3.3.1.js"></script> <script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script> <script> $(document).ready(function() { $('#example').DataTable(); } ); </script> <div class="container"> <table id="example" class="table table-striped"> <thead> <tr> <th>Image</th> <th>Date</th> <th>TC NAME</th> <th>NS FLOW</th> <th>SN FLOW</th> <th>NS FLOW</th> <th>SN FLOW</th> <th>NS FLOW</th> <th>SN FLOW</th> </tr> </thead> <tbody> <tr> <td>721</td> <td>3/15/20</td> <td> <table id="example1" class="table table-nostriped"> <tr> <td>TC1</td> </tr> <tr> <td>TC2</td> </tr> <tr> <td>TC3</td> </tr> </table> </td> <td> <table class="table table-nostriped"> <tr> <td>100</td> </tr> <tr> <td>300</td> </tr> <tr> <td>600</td> </tr> </table> </td> <td> <table class="table table-nostriped"> <tr> <td>100</td> </tr> <tr> <td>800</td> </tr> <tr> <td>400</td> </tr> </table> </td> <td> <table class="table table-nostriped"> <tr> <td>100</td> </tr> <tr> <td>200</td> </tr> <tr> <td>300</td> </tr> </table> </td> <td> <table class="table table-nostriped"> <tr> <td>100</td> </tr> <tr> <td>300</td> </tr> <tr> <td>200</td> </tr> </table> </td> <td> <table class="table table-nostriped"> <tr> <td>200</td> </tr> <tr> <td>200</td> </tr> <tr> <td>200</td> </tr> </table> </td> <td> <table class="table table-nostriped"> <tr> <td>100</td> </tr> <tr> <td>200</td> </tr> <tr> <td>300</td> </tr> </table> </td> </tr> <tr> <td>722</td> <td>3/16/20</td> <td> <table class="table table-nostriped"> <tr> <td>TC1</td> </tr> <tr> <td>TC2</td> </tr> <tr> <td>TC3</td> </tr> </table> </td> <td> <table class="table table-nostriped"> <tr> <td>200</td> </tr> <tr> <td>300</td> </tr> <tr> <td>600</td> </tr> </table> </td> <td> <table class="table table-nostriped"> <tr> <td>100</td> </tr> <tr> <td>800</td> </tr> <tr> <td>400</td> </tr> </table> </td> <td> <table class="table table-nostriped"> <tr> <td>100</td> </tr> <tr> <td>200</td> </tr> <tr> <td>300</td> </tr> </table> </td> <td> <table class="table table-nostriped"> <tr> <td>100</td> </tr> <tr> <td>300</td> </tr> <tr> <td>200</td> </tr> </table> </td> <td> <table class="table table-nostriped"> <tr> <td>200</td> </tr> <tr> <td>200</td> </tr> <tr> <td>200</td> </tr> </table> </td> <td> <table class="table table-nostriped"> <tr> <td>100</td> </tr> <tr> <td>200</td> </tr> <tr> <td>300</td> </tr> </table> </td> </tr> <tr> <td>633</td> <td>3/17/20</td> <td> <table class="table table-nostriped"> <tr> <td>TC1</td> </tr> <tr> <td>TC2</td> </tr> <tr> <td>TC3</td> </tr> </table> </td> <td> <table class="table table-nostriped"> <tr> <td>300</td> </tr> <tr> <td>300</td> </tr> <tr> <td>600</td> </tr> </table> </td> <td> <table class="table table-nostriped"> <tr> <td>100</td> </tr> <tr> <td>800</td> </tr> <tr> <td>400</td> </tr> </table> </td> <td> <table class="table table-nostriped"> <tr> <td>100</td> </tr> <tr> <td>200</td> </tr> <tr> <td>300</td> </tr> </table> </td> <td> <table class="table table-nostriped"> <tr> <td>100</td> </tr> <tr> <td>300</td> </tr> <tr> <td>200</td> </tr> </table> </td> <td> <table class="table table-nostriped"> <tr> <td>200</td> </tr> <tr> <td>200</td> </tr> <tr> <td>200</td> </tr> </table> </td> <td> <table class="table table-nostriped"> <tr> <td>100</td> </tr> <tr> <td>200</td> </tr> <tr> <td>300</td> </tr> </table> </td> </tr> </tbody> </table> </div>
If you by "searchable" mean DataTables inside DataTables you can use the createdRow callback to initialize tables inside a <tr>'s columns. You must have a columns section in order to compensate for the missing <thead> in the nested tables (avoiding the TypeError: col is undefined error) : const columns = [ { title: '' }, ] Init the nested tables in the createdRow callback: let table = $('#example').DataTable({ createdRow: function(row) { $(row).find('td table') .DataTable({ columns: columns, dom: 'tf' }) } }) Notice the dom section. Here only showing the table itself and the filter box. You can remove the header with table.dataTable td table thead { display: none; } Dont add this CSS if you want sortable columns in the nested tables. demo using the markup in question -> http://jsfiddle.net/davidkonrad/8pzkr6yn/ Update. If your concern just is to be able to search within the content of the nested tables (e.g the HTML markup scraped away) just define the relevant columns type as 'html' (https://datatables.net/reference/option/columns.type) : let table = $('#example').DataTable({ columnDefs: [ { targets: [2,3,4,5,6,7], type: 'html' }, ], ... })
two column html table with group
I expect next to name column is besides iphone, why my table doesn't work as expected? <table> <tbody> <tr> <td> <tr> <td>Name:</td> <td>iphone</td> </tr> <tr> <td>Price:</td> <td>123</td> </tr> <tr> <td>Qty</td> <td>1</td> </tr> </td> <td>next to name:</td> <td>12345</td> </tr> </tbody> </table>
You can't write tr inside of td. You can add table wrapper for the <tr> <table border='1'> <tbody> <tr> <td> <table> <tr> <td>Name:</td> <td>iphone</td> </tr> <tr> <td>Price:</td> <td>123</td> </tr> <tr> <td>Qty</td> <td>1</td> </tr> </table> </td> <td>next to name:</td> <td>12345</td> </tr> </tbody> </table>
Using JQuery to select all <tr> elements with a specific class
The basic structure is a table with a few rows. I would like the top half of the table showing and the bottom half collapsed until a user clicks on the READ MORE cell. I have that functionality working but I can't get the JQuery right that selects all of the ".collapseThis" rows and hides them on page load. Here is the table <div id="tables"> <table class="expandableTable"> <tr> <td></td> <td></td> </tr> <tr class="accordion"> <td colspan="2">READ MORE</td> </tr> <tr class="collapseThis"> <td></td> <td></td> </tr> </table> <table class="expandableTable"> <tr> <td></td> <td></td> </tr> <tr class="accordion"> <td colspan="2">READ MORE</td> </tr> <tr class="collapseThis"> <td></td> <td></td> </tr> </table> </div> Here is the JQuery. $(document).ready(function () { function getCollapsable($row) { var children = []; while ($row.next().hasClass('collapseThis')) { children.push($row.next()); $row = $row.next(); } return children; } function hideTableRows($row) { children = getCollapsable($row); $.each(children, function () { $(this).toggle(); }); } $('#tables').each($('expandableTable'), function () { hideTableRows($(this).children().hasClass('.accordion')); });
You can just use css to set the display value, there is no need to use jQuery to set the initial state. If you want to use jQuery, use a simple selector like $('#tables .expandableTable .collapseThis').hide(). $(document).ready(function() { //if you don't want to use css //$('#tables .expandableTable .collapseThis').hide(); }); #tables .expandableTable .collapseThis { display: none; } <div id="tables"> <table class="expandableTable"> <tr> <td>1.1</td> <td>1.2</td> </tr> <tr class="accordion"> <td colspan="2">READ MORE</td> </tr> <tr class="collapseThis"> <td>2.1</td> <td>2.2</td> </tr> </table> <table class="expandableTable"> <tr> <td>1.1</td> <td>1.2</td> </tr> <tr class="accordion"> <td colspan="2">READ MORE</td> </tr> <tr class="collapseThis"> <td>2.1</td> <td>2.2</td> </tr> </table> <table class="expandableTable"> <tr> <td>1.1</td> <td>1.2</td> </tr> <tr class="accordion"> <td colspan="2">READ MORE</td> </tr> <tr class="collapseThis"> <td>2.1</td> <td>2.2</td> </tr> </table> </div>