Search code examples
jqueryjtemplates

jQuery jTemplates Show "empty" message


I'm using jTemplates with jQuery. I'd like to show a message when there are no results, instead of just nothing. I have tried including the message in the template and using a function in the {#foreach} loop to hide it, but that does not work. Please tell me this is possible!

{#template JobCodeAssignment}
    <tr class="standard normal" id="jobCode-{$T.JobCode.JobCode_ID}-blank">
        <td colspan="5" style="text-align:center;"><em>No users</em></td>
    </tr>
{#foreach $T as JobCodeAssignment}
    <!-- {hideNoUsersMessage($T.JobCodeAssignment.JobCode_ID)} -->
    <tr class="standard {#cycle values=['normal','alternate']}">
        <td class="firstColumn"><strong>{$T.JobCodeAssignment.User.FirstName} {$T.JobCodeAssignment.User.LastName}</strong></td>
        <td>{formatCurrency($T.JobCodeAssignment.HourlyRate)}</td>
        <td>{$T.JobCodeAssignment.ShiftReportRequired}</td>
        <td>{$T.JobCodeAssignment.MileageReportRequired}</td>
        <td class="lastColumn" style="text-align:right;"><img id="jobCodeAssignmentEditButton-{$T.JobCodeAssignment.JobCodeAssignment_ID}" src="Images/pencil-small.png" class="clickable" onclick="editJobCodeAssignmentOpen({#var $T.JobCodeAssignment});" /></td>
    </tr>
{#/for}
{#/template JobCodeAssignment}

function hideNoUsersMessage(jobCode_ID) {
    $('#jobCode-' + jobCode_ID + '-blank').hide();
    return jobCode_ID;
}

Solution

  • I figured out how to accomplish what I was looking for in a totally different way, using a parameter within the loop to count the number of records. If it's zero after the loop, it displays the message.

    {#template JobCodeAssignment}
        {#param name=count value=0}
    {#foreach $T as JobCodeAssignment}
        {#param name=count value=$P.count+1}
        <tr class="standard {#cycle values=['normal','alternate']}">
            <td class="firstColumn"><strong>{$T.JobCodeAssignment.User.FirstName} {$T.JobCodeAssignment.User.LastName}</strong></td>
            <td>{formatCurrency($T.JobCodeAssignment.HourlyRate)}</td>
            <td>{$T.JobCodeAssignment.ShiftReportRequired}</td>
            <td>{$T.JobCodeAssignment.MileageReportRequired}</td>
            <td class="lastColumn" style="text-align:right;"><img id="jobCodeAssignmentEditButton-{$T.JobCodeAssignment.JobCodeAssignment_ID}" src="Images/pencil-small.png" class="clickable" onclick="editJobCodeAssignmentOpen({#var $T.JobCodeAssignment});" /></td>
        </tr>
    {#/for}
        {#if $P.count == 0}
        <tr class="standard alternate">
            <td colspan="5" style="text-align:center;"><em>No users</em></td>
        </tr>
        {#/if}
    {#/template JobCodeAssignment}