How to implement conditional render in JS?

Posted by mare on Stack Overflow See other posts from Stack Overflow or by mare
Published on 2011-02-15T22:57:34Z Indexed on 2011/02/15 23:25 UTC
Read the original article Hit count: 227

Filed under:
|
|

Below is the JS (jQuery) code of autocomplete's result function. You can see there's some lines where I print out <li>s containing some data properties (that come in as a result of automcomplete's AJAX call).

How could I rewrite this so that <li> would be conditionally rendered based on whether the property contains any value being either int or string (not empty string or whitespace) or something else that can be represented as string?

$(".clients-dropdown").result(function (event, data, formatted) {
    if (data) {
        // set the hidden input that we need for Client entity rematerialize
        $(".client-id").val(data.client_id);
        if (data.ClientName && data.Address1 && data.postalcode && data.postname) {
            $(".client-address").html(
                    "<li>" + data.ClientName + "</li>" +
                    "<li>" + data.Address1 + "</li>" +
                    "<li>" + data.postalcode + " " + data.postname + "</li>"
            );
            $(".client-details").html(
                    "<li>" + data.PrettyId + "</li>" +
                    "<li>" + data.VatNo + "</li>" +
                    "<li>" + data.Phone + "</li>" +
                    "<li>" + data.Mobile + "</li>" +
                    "<li>" + data.Email1 + "</li>" +
                    "<li>" + data.Contact + "</li>"
            );
        }
    }

Also, for the AJAX call, should my server side action return null when there's a null for a property in the database or empty string?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery