Answered step by step
Verified Expert Solution
Question
1 Approved Answer
JavaScript (Simple) Framework Homework Overview: In this homework you will implement provider code (a function in an external JS file) that can produce a click
JavaScript (Simple) Framework Homework Overview: In this homework you will implement provider code (a function in an external JS file) that can produce a click sortable HTML table from any JSON string that contains an array of simple objects (see JSON string example below left) "Click sortable" means that whenever the user clicks on a column heading the HTML table sorts by the data in that column. You'll type in two different JSON files (like below left). In NetBeans you can right click on a folder and do a File New-JSON file. NetBeans will help by identifying syntax errors if you make any You will then create a tester/driver (consumer code) HTML page that uses your provider code to create two click sortable tables from the two different JSON files (that you typed in yourself). Example JSON File (that you would type in) Example HTML table created by provider code cars.json x Image Make Condition Price make":AudS" image":"pics/audi png" condition": fair", price" :21000 Audi fair $21,000.00 make":"Disney" image": "pics/pixar png Disney condition": "superb" price" $0000 10 $50,000.00 12 14 15 16 17 18 19 20 21 make": "Buick" image": "pics/buick-png" condition": "great" price":15000 Buick great $15,000.00 make"Ford" image": "pics/ford.png" condition": "poor", price":"5000 23 24 25 26 Ford poor $5,000.00 Requirements for MakeTable (provider code, function in external JS file) In an external JavaScript file, you shall have a single function named MakeTable that creates and returns a click sortable HTML table. You will clearly need other functions such as sorting and formatting currency, so how do you manage this? Place needed private functions inside of MakeTable and use them freely within MakeTable The input parameters of MakeTable shall include: the id of a div inside of which you are to place the click sortable HTML table, and the name of a JSON file that holds the data to be placed inside of the HTML table o o Here is an example of how MakeTable might be called from the HTML page // reads from the json file, creating a click sortable HTML table inside of the DOM object with id "firstDiv var table1 MakeTable("firstDiv", "jsonationalParks.json"); // reads from the json file, creating a click sortable HTML table inside of the DOM object with id = secondDiv" var table2 MakeTable("secondDiv", "json/airplanes.json") MakeTable shall get the column heading names from the field names within the JSON file. Here is some sample code that shows how you can iterate over all the properties in an object. ar list JSON.parse(httpRequest.responseText); var obj list[0)://get the 1st object in the array (maybe add some code to be sure you have at least one object) for (var prop in obj) //"prop" iterates through the properties that are in the object obj even though "prop" is a character string (field name), you can use it as if it were an index value inside of square brackets and it returns the value of the property. This is called "associative array notation" (when you use a property name as if it were an index value). 1 console.log('property is'+propand its value is'objlpropl): MakeTable has to understand the data type of each field of the objects in the JSON string. Field types that shall be handled by MakeTable include: String (left justified, the value shown is the same value used for sort order) Image file (aligned center, data showing in the HTML table as the src attribute of an image tag, these columns are not click sortable) Dollar Amount (right justified, formatted like currency-see sample code, numeric value used for sort order, for example 2 comes before 10 in numeric sort order but "2" does not come before "10 in character sort order You can implement more data types if you like, but the above 3 are required. You might want your MakeTable function to accept a third parameter (array) that indicates the types of the JSON fields within the object array. For example, "s" might mean string (left justify, sort as is), "i" might mean image (center, do not sort), "$" might mean dollar amount( right justify, format as currency, sort numerically), etc. Example // For my cars JSON file, the array would show 1st field of each object is string, 2nd image, 3rd string,& 4th field dollar amt var table1 MakeTable("firstDiv", "json/cars.json", ["s","i", "s", "$"]); Or (instead of passing in an array of type indicators), you might just iterate over the fields of the first object to try to figure out the data type of each field. To iterate over the properties of the first object in the array, you could use code as previously shown: var list JSON.parse(httpRequest.responseText); var obj - list[O: // get the 1st object in the array (maybe add some code to be sure you have at least one object) for (var prop in obj) /I "prop" iterates through the properties that are in the object obj console.logl'property is'+propand its value is'+objlpropl: Here is some code that you might find helpful in determining if a string contains something that could be converted to a number or not. You can use JS built-in function isNan() (stands for Is Not a Number) which returns true if a string contains something that cannot be converted to a number. Example converting to Number: var s "123" f (isNaN(s)) // true means string cannot be converted to a number // so just return the string as it came in //false means string can be converted to a number return s else return Number(s); /returns 123 (a number) Number is also a built in JS function you can use To figure out if a field is the name of an image file, you can just look for the common file name extensions as part of the string, e.g., look for "jpg" or "png" or "gif". Just google something like "in javaScript how to see if a string contains another string This might seem a bit silly, but in an attempt to give you some exposure to data persistence, but each time the clicksortable HTML table is sorted, the sort order shall be stored in local storage (this shall be handled in MakeTable.js) Whenever the page is refreshed, the two tables should be displayed in the most recent same sort order (this also shall be handled in MakeTable.js) Since you can assume that the ids passed into the MakeTable function are unique within the page, you can use these as ids as the unique identifiers for storing key/value pair into local storage Here's how to test if you got this right: refresh your page and note the sort order of both HTML tables. Change the sort on your first table, change the sort on your second table. Then refresh the page-the sort order of both tables should stick and not revert to the original sort order The object returned by MakeTable shall have at least one public method that the HTML coder can invoke. It's OK if this method has something to do with styling SON Files You'll type in two (syntactically correct) JSON files, each containing an array of like objects. Each JSON file shall have at least one image (references images you have saved), at least one string, and at least one dollar amount type field Styling: Create an external style sheet that your MakeTable code can rely upon to make the generated HTML table look good. Avoid "hard coding" styling in MakeTable.js wherever possible. You can require the HTML coder to include a reference to this external style sheet. This is the default styling they get it they do not want to change any ot the style. Then if the HTML coder wishes, they are free to follow the external style sheet link with their own internal style sheet that overrides a few of your style settings-if they make things look bad, it's their fault and they should back out that change and keep what you had Remember to limit the scope of your styling. For example, instead of having styles like these table td Have styles like these and require the HTML coder to style the divs that will hold the click sortable tables as class-"clickSort" clickSort table styles any table that is inside of a DOM element classed "clickSort"/ clickSort td In your JavaScript code, you can add a classname like this var element document.createElement("td" element.classList.add("numeric") And then provide styling for that class in the external style sheet: clickSort.numeric text-align: right: Index.html index.html shall have at least two divs that get HTML tables inserted inside with code like previously shown var table1 MakeTable("firstDiv", "jsonationalParks.json"); var table2 MakeTable("secondDiv", "json/airplanes.json"); index.html shall invoke a public method of at least one of the returned table objects (e.g., table1 or table2 above Demonstrate the concept that the framework should make things easy by providing a nice looking default format, but the HTML coder can override it. Have one of the click sortable tables accept the default styling provided by the external style sheet, and the other table have a few style rules overwritten by an internal style sheet. Other Requirements: use strict. The first line of your JavaScript code (both in the
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started