index.js 2.91 KB
/*
 *@time: 2016/1/26
 *@author: chenglong
 */


var $ = require('jquery');
var edit = require('../common/edit');
var dropDown = require('../common/dropDown');

exports.init = function () {

    //列表展示效果

    var $toggleTd = $('.toggle-td');

    $toggleTd.click(function () {

        var $this = $(this),
            $thisNext = $this.closest('tr').nextAll(),
            thisVal = $(this).closest('tr').attr('data-val');

        if ($this.closest('tr').attr('data-status') === 'close') {

            $thisNext.filter(function (index) {

                return $(this).attr('data-parent') === thisVal;

            }).removeClass('hidden');

            $this.closest('tr').attr('data-status', 'open').find('.toggle-td').find('span')
                .removeClass('glyphicon-folder-close').addClass('glyphicon-folder-open');

        } else {

            $thisNext.filter(function (index) {

                var thisR = $(this).attr('data-parent') === thisVal;

                if (thisR && $(this).attr('data-status') === 'open') {
                    $(this).find('.toggle-td').trigger('click');
                }

                return thisR;

            }).addClass('hidden');

            $this.closest('tr').attr('data-status', 'close').find('.toggle-td').find('span')
                .removeClass('glyphicon-folder-open').addClass('glyphicon-folder-close');
        }

    });

    //列表编辑

    var $table = $('.toggle-table');

    $table.click(function (event) {

        var $target = $(event.target),
            id;

        //编辑
        if ($target.hasClass('edit-class-btn')) {

        } else if ($target.hasClass('open-close-btn')) {

            var status;

            if ($target.attr('data-status') === '1') {
                status = 0;
            } else if ($target.attr('data-status') === '0') {
                status = 1;
            }

            console.log(status);

            $.ajax({
                type: 'POST',
                dataType: 'json',
                url: '/product/sort/update',
                data: {
                    id: $target.closest('td').attr('data-id'),
                    status: status
                }
            }).then(function (data) {

                console.log(data);

                if (data.data.code === 200) {

                    $target.attr('data-status', status);
                    if ($target.hasClass('btn-danger')) {
                        $target.text('开启');
                        $target.removeClass('btn-danger')
                            .addClass('btn-warning').closest('td').prev('td').text('关闭');
                    } else {
                        $target.text('关闭');
                        $target.removeClass('btn-warning')
                            .addClass('btn-danger').closest('td').prev('td').text('开启');
                    }

                }
            });

            event.preventDefault();
        }
    });


};