resourceSortManage.js 3.83 KB
/**
 * Created by yoho on 2016/3/23.
 * 资源分类管理
 */

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

var ENUM = [];
var platformList = [];

var g = new common.grid({
    el: "#content-list",
    hash: false,
    columns: [
        {display: "sortId", name: "sortId"},
        {display: "名称", name: "sortName"},
        {display: "所属平台", name: "", render: function(item) {
            return ENUM[item.platformId];
        }},
        {display: "状态", name: "", render: function(item) {
            if(item.status == 0) {
                return "关闭";
            } else if(item.status == 1) {
                return "开启";
            }
        }},
        {display: "添加时间", name: "", render: function(item) {
            return Bll.getLocalTime(item.createTime);
        }},
        {
            display: "操作",name: "", render: function (item) {
            var arr = [];
            arr.push('<a class="btn btn-primary add2" data-index="' + item.__index + '">编辑</a>');
            arr.push('<a class="btn btn-primary delbtn" data-index="' + item.__index + '">删除</a>');
            return arr.join("");
        }
        }
    ]

});

var Bll = {
    getLocalTime:function(nS) {
        var date = new Date(parseInt(nS) * 1000);
        var mm = date.getMonth() + 1;
        var dd = date.getDate();
        var h = date.getHours();
        var min = date.getMinutes();
        var second = date.getSeconds();
        return date.getFullYear() + "-" + (mm < 10 ? "0" + mm : mm) + "-" + (dd < 10 ? "0" + dd : dd) + " " + (h < 10 ? "0" + h : h) + ":"
            + (min < 10 ? "0" + min : min) + ":" + (second < 10 ? "0" + second : second);
    },
    toast:function(url, item, hint) {
        var e = new common.edit("#base-form");

        e.on('validate', function() {
            if(!$("#status").val()) {
                return "请填写状态";
            }
        });

        item.platforms = platformList;

        var dialog=common.dialog.confirm(hint,
            common.util.__template2($("#sourceSort-template").html(), item),
            function() {
                e.submit(url,function(option){
                    //option.data;
                    //console.log(option.data);
                    option.success=function(res){
                        dialog.close();
                        util.__tip(res.data.message, 'success');
                        Bll.init();
                    };
                    option.error=function(res){
                        dialog.close();
                        util.__tip(res.data.message);
                    }
                });
                return false;
            });
        //

        e.init();
    },
    init: function() {
        common.util.__ajax({
            url:'/resource/sort/resSortIndex',
            data:{}
        },function(res){
            platformList = res.data.platform;
            for(var i = 0; i < platformList.length; i++) {
                ENUM[platformList[i].platformId] = platformList[i].platformName;
            }
            g.init(res.data.list);
        },true);
    }
};

Bll.init();

$(document).on('click', '#add-resource', function() {
    var item = {};
    Bll.toast("/resource/sort/addResSort", item, "添加资源分类");
});

$(document).on('click', '.add2', function() {
    var item = g.rows[$(this).data("index")];
    Bll.toast("/resource/sort/updateResSort", item, "修改资源分类");
});

$(document).on('click', '.delbtn', function() {
    var item=g.rows[$(this).data("index")];
    common.dialog.confirm("警告",
        "确认删除?",
        function() {
            common.util.__ajax({
                url:'/resource/sort/deleteResourcesSort',
                data:{sortId:item.sortId}
            },function() {
                Bll.init();
            });
        });
});