I’m still very inexperienced in javascript, nevertheless I’d like to try my hand at it and would be very happy to get some support. I have the following scenario:
I have a table, with several rows and 3 columns. Column 1 would be a name, column 2 would be a number (ID), column 3 would be a value.
I have a value (ID) in a WebApp that I would like to search for in the table in column 2 and would like to display only that row. What is the best way to start? What exact topics should I look at more closely to implement this? I would be very happy to receive an answer!
<div id="datatable" align="center">
<table>
<tr id="class1">
<td>Person 1</td>
<td>1234561</td>
<td>800</td>
</tr>
<tr id="class1">
<td>Person 2</td>
<td>1234562</td>
<td>1800</td>
</tr>
<tr id="class1">
<td>Person 3</td>
<td>1234563</td>
<td>1400</td>
</tr>
</table>
</div>
I have already searched online for similar scripts, however this doesn’t seem to be needed that often! So I would like to tackle this myself now. But how to start?
5
Answers
Wow, a big thanks to you guys for your answers. This helps me a lot and I think I will not use the fastest solution, but fight my way through the Mimo JS section to expand my basic understanding. Your approaches are really great and thanks for the support! When I am ready I will post my solution here.
First of all, you should research DOM manipulation, so that you can actually
access and manipulate the values necessary. Also, you should research how to work with classes. A knowledge of loops is also needed. Finally, some understanding of HTML & CSS is necessary. All of this material is available on
W3schools, so check there.
EDIT: You didn’t ask for the coded solution, which is why I’m not giving it.
You can select the
table
and loop through itsrows
withforEach
and check if therow.cells[1]
( which is the ID cell ), its textContent start withinput
value, If not hide it withdisplay: none
and if it match show it withdisplay: table-row
There are so many skills required it’s quite hard to work out which without making it.
One thing that I have changed is your data from html into an array. I find arrays can be much easier to work with once you get your head around them — and how you can output them to HTML.
There are many ways to do this — that is important to know. There isn’t really such thing as a "right" answer here.
For a quick and not-necessarily dirty solution, check out https://datatables.net/ . This library solves most of your problems or desires with a table.
So you add the library first in your html:
Then in a script tag better after your , initialise Datatable on your table and select the table and add functionalities like this:
Then little by little and depending on your learning hunger get your hands on learning the basics of JS, DOM, HTML, as other mentioned.