javascript - Putting a function in a return statement -
i'm making social network graph displays information writer in tooltip. however, of data fields in database i'm working empty, when hover on node displays nothing (if field "") or displays undefined (if entire field particular writer deleted). example biography: ""
in dataset returns "biography: ", , if delete biography field writer, returns "biography: undefined".
i tried make function return back-up statement if there's no information in field or if it's undefined, (i think) since function within return statement, doesn't work. returns written out function.
if turns out putting function in return statement doesn't work, alternatives try (aside obvious fixing database)? i'm new programming, might obvious mistake.
fiddle: http://jsfiddle.net/lv48w/
//code code code biobackup = function () { if (d.biography = undefined || "") { return "there no listed biography " + d.name } else { return d.biography} }; //add tooltip var tip = d3.tip() .attr('class', 'd3-tip') .offset([-10, 0]) .html(function(d) { return d.name + "<br><span class='tiptitle'>location:</span>" + " " + d.location + "<br><span class='tiptitle'>floruit date:</span>" + " " + d.floruitdate + "<br><span class='tiptitle'>biography:</span>" + " " + biobackup + '<br>click through more information.'; }); //code code code
i played around jsfiddle link bit , came solution.
if replace tooltip variable code with
var tip = d3.tip() .attr('class', 'd3-tip') .offset([-10, 0]) .html(function(d) { return d.name + "<br><span class='tiptitle'>location:</span>" + " " + d.location + "<br><span class='tiptitle'>floruit date:</span>" + " " + d.floruitdate + "<br><span class='tiptitle'>biography:</span>" + " " + (d.biography ? d.biography : "there no listed biography " + d.name) + '<br>click through more information.'; });
you can rid of biobackup
function entirely , works fine.
Comments
Post a Comment