首页 > 其他 > 详细

art-template实战

时间:2019-03-18 21:09:50      阅读:157      评论:0      收藏:0      [点我收藏+]

技术分享图片

内容div

<div id="sku-cont">
    <div class="form-group col-lg-12">
        <div class="col-lg-2">
            &nbsp;
        </div>
        <input type="hidden" name="sku_id[]"/>
        <div class="col-lg-4">
            <label class="control-label  col-lg-3  text-right"><span class="control-label required-mark">*</span>规格:</label>
            <input class="form-control-erbi col-lg-8 required sku" type="text" name="name[]" placeholder="如:土豪金,128G"/>
        </div>
        <div class="col-lg-4">
            <label class="control-label  col-lg-3  text-right"><span class="control-label required-mark">*</span>价格:</label>
            <input class="form-control-erbi col-lg-6 required price" type="text" name="price[]" placeholder=""/>
        </div>
    </div>
</div>

模板

<!-- 模板 -->
<script id="sku-tpl" type="text/html">
    {{ if data.is_new }}
        <div class="form-group col-lg-12">
            <div class="col-lg-2">
                &nbsp;
            </div>
            <input type="hidden" name="sku_id[]"/>
            <div class="col-lg-4">
                <label class="control-label  col-lg-3  text-right"><span class="control-label required-mark">*</span>规格:</label>
                <input class="form-control-erbi col-lg-8 required sku" type="text" name="name[]" placeholder="如:土豪金,128G"/>
            </div>
            <div class="col-lg-4">
                <label class="control-label  col-lg-3  text-right"><span class="control-label required-mark">*</span>价格:</label>
                <input class="form-control-erbi col-lg-6 required price" type="text" name="price[]" />
            </div>

            <label class="control-label col-lg-1 del-btn">
                <button type="button" class="btn btn-xs btn-danger del-sku" >删除</button>
            </label>
        </div>
    {{ else }}
        {{ each data.list }}
            <div class="form-group col-lg-12">
                <div class="col-lg-2">
                    &nbsp;
                </div>
                <input type="hidden" name="sku_id[]" value="{{ $value.id }}"/>
                <div class="col-lg-4">
                    <label class="control-label  col-lg-3  text-right"><span class="control-label required-mark">*</span>规格:</label>
                    <input class="form-control-erbi col-lg-8 required sku" type="text" name="name[]" placeholder="如:土豪金,128G" value="{{ $value.name }}"/>
                </div>
                <div class="col-lg-4">
                    <label class="control-label  col-lg-3  text-right"><span class="control-label required-mark">*</span>价格:</label>
                    <input class="form-control-erbi col-lg-6 required price" type="text" name="price[]" value="{{ $value.price }}"/>
                </div>
                {{ if $value.is_default == 0 }}
                    <label class="control-label col-lg-1 del-btn">
                        <button type="button" class="btn btn-xs btn-danger del-sku" >删除</button>
                    </label>
                {{ /if }}
            </div>
        {{ /each }}
    {{ /if }}
</script>

模板赋值

// 初始化
var in_data = '<?php echo json_encode($product_sku_list);?>';
in_data = JSON.parse(in_data);
if (in_data) {
    let html = template('sku-tpl', {data:{is_new:false,list:in_data}});
    $('#sku-cont').html(html);

    // 添加移除事件
    $(".del-sku").on("click",function() {
        $(this).parent().parent().remove();
    });
}
$("#make_sku_price").on("click",function() {
    let html = template('sku-tpl', {data:{is_new:true}});
    $('#sku-cont').append(html);

    // 添加移除事件
    $(".del-sku").on("click",function() {
        $(this).parent().parent().remove();
    });
});

注意语法前后空格。
很好用。
灵活巧妙运用,无限的可能。

art-template实战

原文:https://www.cnblogs.com/jiqing9006/p/10554875.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!