guochao.page.js 3.67 KB
/* eslint-disable eqeqeq */
require('admin/user.page.css');
require('bootpag/lib/jquery.bootpag.min');

const _ = require('lodash');
let obj = {where: {}, order: []};

function bind_table_pagination() {
    const $ul = $('.guochao-list');
    const fetchRender = () => {
        $.ajax({
            url: '/admin/api/guochao/list',
            method: 'post',
            contentType: 'application/json',
            data: JSON.stringify(obj)
        }).then(result => {
            const list = result;

            let html = '';

            _.each(list, item => {
                if (item.place) {
                    item.placeText = '下';
                } else {
                    item.placeText = '上';
                }
                let str = item.place ? `<a class="btn btn-info btn-update btn-place-up" data-id="${item.id}">更变到上方</a>` : `<a class="btn btn-info btn-success btn-place-down" data-id="${item.id}">更变到下方</a>`;

                html += `
                    <tr class="even pointer">
                        <td class="">${item.id}</td>
                        <td class=""><img src="${item.logo}" style="background: #000000" width="100" height="50"/></td>
                        <td class="">${item.shop_name}</td>
                        <td class=""><input class="sort" data-id="${item.id}" value="${item.sort}"/></td>
                        <td class="">${item.placeText}</td>
                        <td class="">${item.collect_count}</td>
                        <td class="">
                            ${str}
                        </td>
                    </tr>`;
            });
            $ul.html(html);

            $('.sort').blur(function() {
                let num = parseInt($(this).val());

                if (typeof num != 'number') {
                    return alert('请输入数字');
                }
                $.ajax({
                    method: 'post',
                    url: '/admin/api/guochao/update',
                    contentType: 'application/json',
                    data: JSON.stringify({id: $(this).attr('data-id'), sort: $(this).val()})
                }).then(() => {
                    fetchRender();
                });
            });
            $('.btn-place-up').click(function() {
                $.ajax({
                    method: 'post',
                    contentType: 'application/json',
                    url: '/admin/api/guochao/update',
                    data: JSON.stringify({id: $(this).attr('data-id'), place: 0})
                }).then(() => {
                    fetchRender();
                });
            });
            $('.btn-place-down').click(function() {
                $.ajax({
                    method: 'post',
                    url: '/admin/api/guochao/update',
                    contentType: 'application/json',
                    data: JSON.stringify({id: $(this).attr('data-id'), place: 1})
                }).then(() => {
                    fetchRender();
                });
            });
        });
    };

    $('.btn_collect_desc').click(function() {
        obj.order = ['collect_count', 'desc'];
        fetchRender();
        obj = {where: {}, order: []};
    });
    $('.btn_up_query').click(function() {
        obj.where = {place: 0};
        fetchRender();
    });
    $('.btn_down_query').click(function() {
        obj.where = {place: 1};
        fetchRender();
    });
    $('.btn_name_query').click(function() {
        obj.where = {shop_name: $('.shop_name').val()};
        fetchRender();
    });
    $('.btn_all').click(function() {
        obj = {where: {}, order: []};
        fetchRender();
    });
    fetchRender();

}

(function() {
    bind_table_pagination();
}());