juicer.js的使用

juicer.js的使用

​ juicer.js是阿里的一个轻量级模板引擎,使用他可以在纯html上使用el表达式。

先举例代码:

html:

<div>
    <button type="button" id="query-dlq-config" class="btn btn-sm btn-primary"><i
    class="fa fa-dot-circle-o"></i> 查询</button>
</div>
<div class="row" id="student-info">
     <!--学生信息查询功能模版......-->
</div>

<!--学生信息查询功能模版-->
<script id="student-info-script" type="text/template">
  <table class="table">
    <thead>
    <tr>
      <th>学生ID</th>
      <th>学生名字</th>
      <th>是否新生</th>
      <th>更改</th>
    </tr>
    </thead>
    <tbody>
    {@each studentInfos as studentInfo}
    <tr>
      <td>${studentInfo.studentId}</td>
      <td>${studentInfo.studentName}</td>
      <td>
        <select id="is-active">
          {@if studentInfo.isNew === "false"}
          <option value="false" selected>false</option>
          <option value="true">true</option>
          {@else}
          <option value="false">false</option>
          <option value="true" selected>true</option>
          {@/if}
        </select>
      </td>
      <th><a href="#" name="${student.sudentId}" id="update-student-info">更改</a></th>
    </tr>
    {@/each}
    </tbody>
  </table>
</script>

javascript:

 function queryStudentInfo(studentId) {
    $.ajax({
      type: "post",
      dataType: "json",
      contentType: "application/json",
      url: "/student/queryStudentInfo",
      async: false,
      data: JSON.stringify({
        "studentId": studentId
      }),
      success: function (res) {
        //新建数组用于存调到的数据
        var studentInfos = [];
        if (res) {
          res.forEach(function (entry) {
            studentInfos.push(entry);
          })
        }
        //新建变量用于存页面模版
        var $studentInfos = $("#student-info-script").html();
        //利用juicer将模版和数据放入页面指定位置
        $("#student-info").html(
            juicer($studentInfos, {"studentInfos": studentInfos})
        );
        dlqConfigUpdate();
      },
      error: function (reserr) {
        notie.alert({
          type: 3,
          text: reserr,
          stay: false,
          time: 3,
          position: "top"
        })
      }
    })
  }

jar:

我用的<script src="js/libs/juicer-min.js"></script>

使用介绍:

  1. 在html上要建立个模版,即一个用script标签包住的html,这样模版里便可以使用juicer的标签和表达式了;

  2. 在js里调juicer方法,即juicer(“模版”,”放入模版的数据”),并指定将模版放入html页面什么位置;

  3. 模版里可以用 {@each studentInfos as studentInfo} 。。。 {@/each}遍历数据

    可以用 {@if studentInfo.isNew === “false”}。。。 {@else}。。。{@/if}的选择标签

    还可以自定义函数等等。

本文出自:https://blog.csdn.net/renhuan28/article/details/81867141

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值