<div class="fsh-pop">
    <div id="add_form" class="layui-form">
        <table id="fans_chose" class="layui-hide" lay-filter="fans_chose"></table>
    </div>
</div>
<script>
    layui.use(['layer', 'table'], function () {
        var layer = layui.layer;
        var table = layui.table;
        GLOBAL.choosed=[];
        var tableObj = table.render({
            id: 'fans_chose',
            elem: '#fans_chose',
            url: '/accountfans/list',
            align: "center",
            cols: [[ //表头
                {type: 'checkbox'},
                {type: 'numbers', title: '序号',width:50},
                {field: 'nicknameStr', title: '昵称', width: 150, align: 'center',},
                {field: 'headimgurl', title: '头像', width: 80, align: 'center',templet:function (d) {
                    return "<img class='fans-portrait' style='width:50px;height:50px;' src=" + d.headimgurl + " />"
                }},
                {field: 'gender', title: '性别', align: 'center',templet:function (d) {
                    return d.gender == 1 ? '男' : '女';
                }},
                {field: 'city', title: '省/市', width: 150,align: 'center',templet:function (d) {
                    return (d.province + '-' + d.city);
            }}
            ]],
            success: function (result) {
                for (var i = 0; i < GLOBAL.choosed.length; i++) {
                    util.indexOfArray(GLOBAL.choosed,result.data[i].openId)>-1 && (result.data[i].checked=true);
                }
                return result;
            }
        });

        // 选择事件
        table.on('checkbox(fans_chose)', function(obj){
            if(obj.type=="all"){
                //全选
                var data=table.cache["fans_chose"] || [];
                if(obj.checked){
                    for(var i=0;i<data.length;i++){
                        GLOBAL.choosed=util.pushArray2(GLOBAL.choosed,data[i].openId);
                    }
                }else{
                    for(var i=0;i<data.length;i++){
                        GLOBAL.choosed=util.removeFromArray(GLOBAL.choosed,data[i].openId);
                    }
                }
            }else{
                //单选
                if(obj.checked){
                    GLOBAL.choosed.push(obj.data.openId);
                }else{
                    GLOBAL.choosed=util.removeFromArray(GLOBAL.choosed,obj.data.openId);
                }
            }
        });
    });
</script>