I need to create a table using JAVASCRIPT. The web page need to contain a title,
ID: 3657945 • Letter: I
Question
I need to create a table using JAVASCRIPT. The web page need to contain a title, a table with two columns, and two buttons, as shown here. The first column of the table will contain the names of people and the second column will contain their corresponding ages. The table will initially be empty and a user will be able to type values into it. One button will cause the entries in the table to be sorted based on the name in column 1 and the second button will cause the entries to be sorted based on the ages in column 2. Any help with the code would be great!Explanation / Answer
Tables can be made sortable via client-side JavaScript by adding class="wikitable sortable" to their top line. These tables need to be properly formatted, with the right amount of cells. Additionally you need to make sure that the headers of your column are properly indicated in the wikicode. For this the ! character is used in the table syntax. [edit]Simple example This is an example of the table listed in the first section and shows the typical way of how a table should be authored in order to enable table sorting. {| class="wikitable sortable" border="1" |- ! name ! data ! more data |- | cats | 273 | 53 |- | dogs | 65 | 8,492 |- | mice | 1,649 | 548 |} [edit]Initial sort order of rows When users are first presented with a table, the rows will always appear in the same order as in the wikitext. If you want a table to appear sorted by a certain column, you must sort the wikitext itself in that order. This is usually done for the first column. [edit]Restrictions Only the columns can be made sortable. Sorting of rows is not possible. [edit]Setting the data type of a column [edit]Forcing a column to have a particular data type The sort mode for a column can now be manually specified by adding a data-sort-type to the column header. For more info see meta:Help:Sorting#Forcing the sort mode for a column. The data-sort-type="..." attribute can be added inside the header of a column to ensure that the cells underneath are all treated as a specified type of data. The following (case-insensitive) values are valid for data-sort-type: text number currency url for website addresses IPAddress for numeric internet protocol addresses date isoDate for dates in the ISO yyyy-mm-dd format usLongDate for dates in the US format (with the month before the day) time For example: Wikitext Without any data-sort-type With data-sort-type="text" {| class="wikitable sortable" |- ! data-sort-type="text" | Album |- ... etc ... |} Album 21 19 21 21 19 21 Matinée 21 19 Everything Is New Love & War Album 21 19 21 21 19 21 Matinée 21 19 Everything Is New Love & War Without data-sort-type="text" in the header, the tablesorter gets confused by the numeric titles in the first few rows into treating the entire column as numeric. This results in it wrongly sorting the non-numeric titles as zero regardless of the alphabetical ordering of their text. [edit]Default data type of a column If you do not specify a data-sort-type, the sort modes (the data types, which, in addition to the choice "ascending" or "descending", determine the sorting order) are as follows: date (see also below) criterion: the first non-blank element is of the form "dd-dd-dddd", "dd-dd-dd", or "dd aaa dddd" order: the string abcdefghij of length 10 is positioned as ghijdeab, the string abcdefghijk of length 8 as 19ghdeab if gh>=50 (string comparison) and 20ghdeab otherwise (i.e., the assumed format is DD-MM-YYYY or DD-MM-YY), and the string "dd aaa dddd" with aaa an abbreviated month name: chronologically "currency" (this mode can be useful for other data also) criterion: the first non-blank element starts with $, £, €, or ¥ order: numeric, ignoring these symbols and all ordinary letters and commas, but not spaces; note that scientific notation cannot be used, as e and E are removed numeric criterion: the first non-blank element consists of just digits, points, commas, spaces, "+", "-", possibly followed by "e" or "E" and a string consisting of "+", "-", digits order: after removing the commas and spaces, if any, if the string starts with a number the order is numeric according to the first number in the string (parseFloat is applied); it is regarded as zero if it is empty; in other cases (parseFloat returns NaN), the element is positioned like -8. proposed internationalisation: in German etc., treat comma as a decimal point string criterion: all other cases; to avoid one of the other modes, start e.g. with a hidden "&"; this can be done conveniently with m:Template:sms (backlinks, edit), which also allows more hidden text, as a sort key; while the similar templates above are called at the end of a table element, call this one at the start order: after conversion of capitals to lowercase the order is ASCII - partial list showing the order: !"#$%&'()*+,-./09:;?@[]^_'az{|}~é— (see also below; a blank space comes before every other character; an nbsp code counts as a space; two adjacent ordinary blank spaces count as one; for multiple blank spaces one can use nbsps or alternate nbsps and ordinary blank spaces) If more than one possible type matches, the first type in the above order is chosen. For example, "24-12-2007" matches as a date, so is not treated as a number. Formatting and markup tags are ignored when determining the matching type. The sort mode is determined by the table element that is currently in the first non-blank row below the header. In the case of different data types within one column (according to the criteria mentioned above) the sort mode may change after sorting, which can give a cycle of four or even more instead of two. This is confusing and gives undesired sorting orders, so that can better be avoided. However, it can be complicated to assess whether an element can ever be at the top after any sorting operations on the same and other columns, and this can also change after deleting a row, or adding a column. Therefore it is wise to make sure that every element matches the criterion for the required data type. Using a row template this can be done very conveniently.Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.