batch_create.php 6.41 KB
<?php

use yii\helpers\Url;


$this->title = '批量添加型号';
$this->params['breadcrumbs'][] = '设备管理';
$this->params['breadcrumbs'][] = ['label' => '设备列表', 'url' => ['/device/device/index']];
$this->params['breadcrumbs'][] = ['label' => '型号管理', 'url' => ['/device/model/index']];
$this->params['breadcrumbs'][] =  $this->title;


?>

<form action="<?php echo Url::toRoute(['/device/brand/do-add']); ?>" name="brandForm" id="brandForm" method="post" enctype="multipart/form-data">
    <div class="panel panel-default">
        <div class="panel-body">
            <div class="form-group col-sm-12">
                <label class="control-label text-right col-sm-2">品牌:</label>
                <div class="text-left col-sm-2">
                    <select class="form-control " id="brand" name="brand">
                        <option value="">--请选择品牌--</option>
                        <?php foreach ($brandList as $brand) : ?>
                            <option value="<?=$brand['id'] ?>" ><?=$brand['chinese_name'] ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <label class="control-label text-right col-sm-2">品牌:</label>
                <div class="text-left col-sm-6 form-inline">
                    <select class="form-control" id="deviceParentCat" name="deviceParentCat"></select>
                    <select class="form-control" id="deviceChildCat" name="deviceChildCat"></select>
                </div>
            </div>
        </div>

        <div class="panel-body col-sm-12 text-center">
            <div class="form-group col-sm-12">
                <div class="col-sm-3"></div>
                <div class="col-sm-6">
                    <table class="table no-border" id="table_model">
                        <tr><th class="text-center">型号</th></tr>
                        <tr><td><input type="text" class="form-control" name="model"></td></tr>
                        <tr><td><input type="text" class="form-control" name="model"></td></tr>
                        <tr><td><input type="text" class="form-control" name="model"></td></tr>
                    </table>
                </div>
                <div class="col-sm-3"></div>
            </div>
            <div class="form-group col-sm-12 text-center">
                <button type="button" class="btn btn-success" id="batch_add">继续添加</button>&nbsp;&nbsp;&nbsp;&nbsp;
            </div>
        </div>
        <div class="panel-footer text-center">
            <button type="button" class="btn btn-primary" id="save">提&nbsp;&nbsp;交</button>&nbsp;&nbsp;&nbsp;&nbsp;
            <a class="btn btn-default" href="<?=Url::toRoute("/device/model/batch-create")?>">重&nbsp;&nbsp;&nbsp;&nbsp;置</a>
        </div>
    </div>

</form>

<script>
    $(document).ready(function(){
        $("#batch_add").bind("click",function(){
            var html = "<tr><td><input type=\"text\" class=\"form-control\" name=\"model\"></td></tr>";
            $("#table_model").append(html);
        });



        $("#save").bind("click",function(){
            var modelList=new Array();
            $("input[name='model']").each(function(){
                var model = $.trim($(this).val());
                if (model != null && model != ""){
                    modelList.push(model);
                }
            });
            if (modelList.length == 0){
                alert("请添加型号");
                return false;
            }

            var deviceChildCat = $("#deviceChildCat").val();
            var brand_id = $("#brand").val();
            if (deviceChildCat == null || deviceChildCat == ""){
                alert("请选择二级分类");
                return false;
            }
            if (brand_id == null || brand_id == ""){
                alert("请选择品牌");
                return false;
            }

            $.ajax({
                type: "post",
                url: "do-add-batch",
                dataType:"json",
                data: $.csrf({"brand_id":brand_id,"cat_id":deviceChildCat,"model":modelList}),
                success:function(msg){
                    alert(msg['msg']);
                    if (msg['status'] == 1){
                        location.href = "<?=Url::toRoute("/device/model/index")?>";
                    }else{
                    }
                },
                error:function(msg){
                }
            });
        });




        function loadDeviceParentCat(){
            $.ajax({
                type: "post",
                url: "<?=Url::toRoute("/device/model/get-device-cat-list")?>",
                dataType:"json",
                data: $.csrf({"parentId":0}),
                success:function(msg){
                    if (msg['status'] == 1){
                        var data = eval(msg['modelList']);
                        var html = "";
                        for(var s in data){
                            html = html +"<option value='"+data[s]['id']+"'>"+data[s]['name']+"</option>";
                        }
                        $("#deviceParentCat").html(html);

                        $("#deviceParentCat").trigger("change");
                    }else{
                        $("#deviceParentCat").html("<option value='0'>--无一级类型--</option>");
                        $("#deviceChildCat").html("<option value='0'>--无二级类型--</option>");
                    }
                },
                error:function(msg){
                }
            });
        }
        loadDeviceParentCat();


        //获取子级分类
        $("#deviceParentCat").bind("change",function(){
            var pid = $(this).val();
            $.ajax({
                type: "post",
                url: "<?=Url::toRoute("/device/model/get-device-cat-list")?>",
                dataType:"json",
                data: $.csrf({"parentId":pid}),
                success:function(msg){
                    if (msg['status'] == 1){
                        var data = eval(msg['modelList']);
                        var html = "";
                        for(var s in data){
                            html = html +"<option value='"+data[s]['id']+"'>"+data[s]['name']+"</option>";
                        }
                        $("#deviceChildCat").html(html);

                    }else{
                    }
                },
                error:function(msg){
                }
            });
        });
    });
</script>