Index.js 4 KB
/**
 * Created by wangqianjun on 16/3/22.
 */

'use strict';
var $ = require('jquery'),
    common = require('../../../common/common');


var g = new common.grid({
    el: '#color-list',
    columns: [

        {display: "ID", name: "uid"},
        {display: "作者", name: "username"},
        {display: "描述", name: "authorDesc"},
        {display: "作者头像", name: "colorValue", render: function (item) {

            item.avatar = common.util.__template(item.avatar,{mode:2,width:100,height:100});
            return '<img style="width:40px; height:40px" src="'+item.avatar+'"/>';
        }},

        {display: "名下文章", name: "articleCount"},

        {
            display: '操作',
            //}
            name: "status",
            render: function (items) {
                var HtmArr = [];

                HtmArr.push('<a data-index="' + items.__index + '" href="JavaScript:;" class="btn btn-primary btn-xs info-modify">编辑</a>');
                HtmArr.push('<a data-index="' + items.__index + '" href="JavaScript:;" class="btn btn-danger btn-xs info-del">删除</a>');
                if(items.status==0){
                    HtmArr.push('<a data-index="' + items.__index + '" href="JavaScript:;" class="btn btn-success btn-xs info-open">开启</a>');
                }else{
                    HtmArr.push('<a data-index="' + items.__index + '" href="JavaScript:;" class="btn btn-danger btn-xs info-close">关闭</a>');
                }
                return HtmArr.join('');
            }
        }
    ]

});

g.init('/guang/author/getList');

//==================== 按钮点击事件 =====================//

//添加
$(document).on('click', '#add-btn', function() {
    articleCategoryOP("新增", '/guang/author/addAuthor', {});

});

//编辑
$(document).on('click', '.info-modify', function() {
    var item = g.rows[$(this).data("index")];
    articleCategoryOP("修改", '/guang/author/updateAuthor', item);
});

//删除
$(document).on('click', '.info-del', function() {
    var item = g.rows[$(this).data("index")];

    common.dialog.confirm("温馨提示","确定要删除该作者?" , function() {
        common.util.__ajax({
            url: '/guang/author/delAuthor',
            data: {
                uid: item.uid,
            }
        }, function(res) {
            if (res.code == 200) {
                g.reload();
            } else {
                common.util.__tip(res.message);
            }
        });
    });
});
//开启
$(document).on('click', '.info-open', function() {
    var item = g.rows[$(this).data("index")];
    common.util.__ajax({
        url: '/guang/author/updateAuthorStatus',
        data: {
            uid: item.uid,
            status:1
        }
    }, function(res) {
        if (res.code == 200) {
            g.reload();
        } else {
            common.util.__tip(res.message);
        }
    });
});
//关闭
$(document).on('click', '.info-close', function() {
    var item = g.rows[$(this).data("index")];
    common.util.__ajax({
        url: '/guang/author/updateAuthorStatus',
        data: {
            uid: item.uid,
            status:0
        }
    }, function(res) {
        if (res.code == 200) {
            g.reload();
        } else {
            common.util.__tip(res.message);
        }
    });
});

function articleCategoryOP(prefix, url, item) {

    var a =new common.edit(".confirm", {
        "bucket" : "author"
    });

    common.dialog.confirm(prefix+'作者', common.util.__template2($("#template").html(), item), function () {

        //
        return a.submit(url,function(option){
            option.success=function(res){
                if(res.code=="200"){
                    a.$tip("提交成功", function() {
                        g.reload();
                    }, 'growl-success');
                }else{
                    a.$tip(res.message);
                }
                return false;
            },
                option.error=function(res){
                    a.$tip(res.message);
                }
        });

    });

    a.init();



}