Is there a way to create rating images (5 stars, 4 stars, 3 stars, etc...) inside an array in JavaScript and output it all as a table in HTML? -
the code below i've been working on. think logic flawed when rate products based on calculated scores, i'm getting mixed up. , i'm unsure how create rating images based on scores. first column in arrays score value, second column product id, third column name of product, , fourth column link brand websites. want output image in fifth column reflect score of product. example, if i'm sorting products based on score , want products highest scores listed first, should products[1] 5 star image, products[3] 4 star image, products[0] 3 star image, products[4] 2 star image, , products[2] 1 star image. note: reason language="javascript" works me , type="text/javascript" not. if provide guidance code sincerely , appreciate. thank in advance :)
html:
<table id="table"> <tr id="tbody"> <th>score</th> <th>id</th> <th>name</th> <th>website</th> <th>rating</th> </tr> </table>
javascript:
var products = new array(5); products[0] = [26, 4859, "panasonic tv", "http://www.panasonic.com"]; products[1] = [37, 4762, "sony tv", "http://www.sony.com"]; products[2] = [9, 4899, "lg tv", "http://www.lg.com"]; products[3] = [34, 5001, "samsung tv", "http://www.samsung.com"]; products[4] = [22, 3425, "vizio tv", "http://www.vizio.com"]; function comparator(a,b) { if (if (a[0] > b[0]) return -1; if (a[0] < b[0]) return 1; return 0; } var productssorted = products.sort(comparator); (i=0; i<products.length, i++) { if (products[0] === 37) { document.getelementbyid("img5"); } else if (products[0] === 34) { document.getelementbyid("img4"); } else if (products[0] === 26) { document.getelementbyid("img3"); } else if (products[0] === 22) { document.getelementbyid("img2"); } else { document.getelementbyid("img1"); }; table.appendchild(tbody); productssorted.foreach(function(item) { var row = document.createelement("tr"); var score = document.createelement("td"); score.textcontent = item[0]; var id= document.createelement("td"); id.textcontent = item[1]; var name = document.createelement("td"); name.textcontext = item[2]; var link_td = document.createelement("td"); var link = document.createelement("a"); link.textcontent = item[3] link.href = item[3] link_td.appendchild(link); var rating = new array(5); var rating_td = document.createelement("td"); var rating[0] = document.createelement("img5"); rating.setattribute("src","http://dkcoin8.com/images/five-star-clipart-4.png"); var rating[1] = document.createelement("img4"); rating.setattribute("src","http://aliviogroup.com.au/wp-content/uploads/2016/04/four-stars.jpg"); var rating[2] = document.createelement("img3"); rating.setattribute("src","https://waytoomuchtelevision.files.wordpress.com/2011/01/3star.jpg"); var rating[3] = document.createelement("img2"); rating.setattribute("src","https://authorjanedoe.files.wordpress.com/2012/07/2star.jpg"); var rating[4] = document.createelement("img1"); rating.setattribute("src","http://clipart.printcolorcraft.com/wp-content/uploads/stars/smooth%20star.jpg"); rating_td.appendchild(rating); row.appendchild(score); row.appendchild(id); row.appendchild(name); row.appendchild(link); row.appendchild(rating); table.appendchild(row); });
create png of star background white , star transparent. put row of 5 of them side-by-side in div (or td in case). create image of yellow rectangle. set background of div/td of stars yellow rectangle. set background-size parameter of div/td percentage based on score. if average score 70% 70% of background yellow, resulting in 3 yellow stars, 1 star partially filled in yellow, , 1 unfilled star.
speaking of score... appear sums of scores. if that's case should either calculate average instead of sum , put in first column, or include count of number of ratings can calculate average score. example if score 9 result of 4/5 , 5/5, actual score 9/10 or 90%.
function createrows() { var products = new array(5); products[0] = [84, 4859, "panasonic tv", "http://www.panasonic.com"]; products[1] = [73, 4762, "sony tv", "http://www.sony.com"]; products[2] = [90, 4899, "lg tv", "http://www.lg.com"]; products[3] = [88, 5001, "samsung tv", "http://www.samsung.com"]; products[4] = [44, 3425, "vizio tv", "http://www.vizio.com"]; var items = document.getelementbyid("items"); array.prototype.foreach.call(products, function(prod) { var row = document.createelement("div"); row.classname = "row"; var fields = new array(4); var field_num = 0; array.prototype.foreach.call(prod, function(field) { fields[field_num] = document.createelement("div"); fields[field_num].classname = "field"; if (field_num > 0) { fields[field_num].innerhtml = prod[field_num]; fields[field_num].id = "field" + field_num; } row.append(fields[field_num]); field_num++; }); fields[0].id = "rating"; fields[0].style.backgroundsize = prod[0] + "% 50px"; (i=0; < 5; i++) { var star = document.createelement("img"); star.src = "https://i.imgur.com/jxqw2cc.png"; fields[0].append(star); } items.append(row); }); } createrows();
.field { display: inline-block; line-height: 19px; vertical-align: top; padding: 3px 5px; } #field1 { width: 50px; border-right: 1px solid black; height: 100%; } #field2 { width: 100px; border-right: 1px solid black; } .row { border: 1px solid black; margin: 5px; font: 14px arial, sans-serif; background: white; } #rating { height: 25px; width: 125px; padding: 0px; border-right: 1px solid black; background: #ccc url(http://weclipart.com/gimg/827e9394ec65e95f/12e902ef8c564cdfb22cf736428a35cf-800.png) no-repeat; } #rating img { height: 25px; padding: 0px; }
<div id="items"> </div>
Comments
Post a Comment