Thymeleaf页面三元运算符

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nangeali/article/details/82215853

三元运算符

根据0、1显示用户性别
可以使用三元运算符,两种写法

写在括号内

<td th:text="${emp.gender==0?'女':'男'}"></td>

写在括号外

<td th:text="${emp.gender}==0?'女':'男'"></td>

推荐
写在括号外

<table class="table table-striped table-sm">
    <thead>
    <tr>
        <th>#</th>
        <th>lastName</th>
        <th>email</th>
        <th>gender</th>
        <th>department</th>
        <th>birth</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="emp:${emps}">
        <td th:text="${emp.id}"></td>
        <td>[[${emp.lastName}]]</td>
        <td th:text="${emp.email}"></td>
        <td th:text="${emp.gender}==0?'女':'男'"></td>
        <td th:text="${emp.department.departmentName}"></td>
        <td th:text="${#dates.format(emp.birth, 'yyyy-MM-dd HH:mm')}"></td>
        <td>
            <a class="btn btn-sm btn-primary" th:href="@{/emp/}+${emp.id}">编辑</a>
            <button th:attr="del_uri=@{/emp/}+${emp.id}" class="btn btn-sm btn-danger deleteBtn">删除
            </button>
        </td>
    </tr>
    </tbody>
</table>

猜你喜欢

转载自blog.csdn.net/nangeali/article/details/82215853