Nicholas Kreidberg Nicholas Kreidberg - 5 months ago 18
jQuery Question

Server-side rendered HTML -> JS Table?

I am working on an older Symfony2 project and using Twig heavily.

Here's the template for a single row:

<tr>
<td class="centerCell">
<img src="/images/social-icons/{{ review.type }}.png" title="{{ review.type }}"/>
</td>
<td data-rating="{{ review.rating }}" class="centerCell">
<div class="starContainer">
{% for i in 1..5 %}
<span class="gfsStar{% if i <= review.rating %} on{% endif %}"></span>
{% endfor %}
</div>
<div class="ratingBadge">{{ review.rating }} / 5</div>
</td>
<td>
<div {% if review.content|length > 275 %} class="block-with-text" {% endif %}>{{ review.content }}</div>
{% if review.content|length > 275 %} <div class="over-flow-indicator">[Read more]</div> {% endif %}
</td>
<td>{{ review.time }}</td>
<td class="centerCell">{{ review.author }}</td>
<td>
<a class="viewButton" href="{{ review.url }}" target="_blank">View</a>
</td>
</tr>


I use ajax to call a controller which grabs model data, then calls the renderView() method to generate the HTML.

I want to add sorting/pagination to the front end but all of the plugins I have found don't support adding rows (via ajax) of rendered HTML. On page load I only want to display 10-20 reviews, and request more from the server when the user pages further along. Bootstrap-table seemed like a great solution but due to the complexity of the template I didn't find a good way to implement their ajax functionality.

Clearly this isn't a new problem, how have people solved this before?

Answer

I hate to answer my own question here but after much investigation I did come to a decent solution. I had to avoid rendering the HTML server-side and instead implemented a simple set of functions in JavaScript which accomplish the same thing that Smart was doing previously. Here is the JS equivalent to the Twig template above:

function transformData(data) {

    var transformedArray = [];

    for(var i = 0; i < data.length; i++) {
        var obj = {
            id : data[i].id,
            type : getTemplate('type', data[i].type),
            author : data[i].author,
            time : data[i].time,
            rating : getTemplate('rating', data[i].rating),
            content : getTemplate('content', data[i].content),
            url : getTemplate('url', data[i].url)
        };

        console.log("In loop: " + i);
        console.dir(obj);

        transformedArray.push(obj);
    }

    return transformedArray;
}

function getTemplate(key, value) {

    var html = '';

    switch (key) {
        case "type" :
            html = '<img src="/images/social-icons/' + value + '.png" style="height:32px;" title="' + value + '"/>';
            break;
        case "rating" :
            html = getRatingHTML(value);
            break;
        case "content" :
            html = getContentHTML(value);
            break;
        case "url" :
            html = '<a class="viewButton" href="' + value + '" target="_blank">View</a>';
            break;
    }

    return html;
}

function getContentHTML(value) {

    var html = '<div',
        indicator = '';

    if (value.length > 275) {
        html += ' class="block-with-text"';
        indicator += '<div class="over-flow-indicator">[Read more]</div>';
    }

    html += '>' + value + '</div>' + indicator;

    return html;
}

function getRatingHTML(value) {
    var html = '<div class="starContainer" style="min-width: 72px">';

    for (var i = 1; i <= 5; i++) {
        html += '<span class="gfsStar ';
        if (i <= value) {
            html += 'on';
        }

        html += '"></span>';
    }

    html += "</div>";
    html += '<div class="ratingBadge">' + value + ' / 5</div>';

    return html;
}

I am using the bootstrap-table plugin with a custom ajax method. Here is what that looks like:

function getReviewData(params, offset) {

    $.post("/extended-review-monitor/reviews", {"limit": 10, "offset": offset})
        .done(function (data) {
            console.dir(data);

            processedData = transformData(data.data);

            console.log("processed data");
            console.dir(processedData);

            setTimeout(function() {
                params.success({
                    total: data.data[0].total,
                    rows: processedData
                });

            }, 1000);

        })
        .fail(function () {
            console.log("ajax error");
        });

    console.log("ajax complete");
}

I really hope this ends up helping somebody else down the road.