sizeChart.js 3.62 KB
// 尺码管理
var $ = require('jquery');
var edit = require('../common/edit');
var dropDown = require('../common/dropDown');
var dropDown = require('../common/dropDown');
var Handlebars = require('yoho.handlebars')

exports.init = function() {
	$('.getsize').on('click', function() {
	console.log($(this).data('id'));
		$.ajax({
			url: '/erpproduct/size/getSize',
			type: 'POST',
			dataType: "json",
			data: {
				id: $(this).data('id')
			},
			success: function(data) {
				console.log(data.data.data);
				var siezNameBox = [];
				siezNameBox.push('<input type="hidden" id="id" value="'+data.data.data.id+'" />');
				siezNameBox.push('<input class="form-control"id="siezName" type="text" value="'+data.data.data.sizeName+'" />');
				$('#siezNameBox').html(siezNameBox);
			}
		})
	});
	$('#addSize').on('click',function(){
		$('#siezNameBox').html('<input id="siezName" class="form-control" type="text" value="" />');
        $('#modal-id').modal('show');
	})
	$('.add').on('click', function() {
		var id=  $('#id').val();
		var siezName=  $('#siezName').val();
		console.log(siezName);
		if(!id){
			__ajax({
					url:'/erpproduct/size/addSize',
					data:{
						sizeName: siezName
					}
				})
			}else{
			__ajax({
					url:'/erpproduct/size/updateSize',
					data:{
						id:id,
						sizeName: siezName
					}
				})				
			}
	})

function __ajax(options,callback){
	$.ajax({
	    type: 'POST',
	    url: options.url,
	    dataType: 'json',
	    data:options.data||{},
	    success: function(res) {
	        res=res.data;
	        if (res.code === 200) {
	            $.gritter.add({
	                title: options.msg||"提交成功",
	                class_name: 'growl-success',
	                sticky: false,
	                time: '1000',
	                after_open: function() {
	                    callback&&callback();
	                }
	             });
	            setInterval(function(){
	            	history.go(0);
	            },1000)
	        } else {
	            $.gritter.add({
	                title: '提交失败',
	                class_name: 'growl-danger',
	                sticky: false,
	                time: '1000'
	             });
	            setInterval(function(){
	            	history.go(0);
	            },1000)
	        }
	    }
	});
}
	function getArr(curPage, pageCount) {
		var arr = [];

		//改变标签数,最好是奇数.......
		var paginitionLength = 5;

		var middle = Math.ceil(paginitionLength / 2);


		if (curPage - 1 >= pageCount) {
			console.log('err data');
			return false;
		}

		for (var i = 0; i < pageCount && i < paginitionLength; i++) {
			arr[i] = i + 1;
		}

		if (curPage <= middle) {
			return arr;
		}

		if (curPage > middle && curPage < pageCount + 1 - middle) {
			for (var p = 0; p < arr.length; p++) {
				arr[p] = curPage - (paginitionLength - middle - p);
			}

		} else {
			for (var k = 0; k < arr.length; k++) {
				arr[k] = pageCount - paginitionLength + k + 1;
			}
		}

		return arr;
	}
	Handlebars.registerHelper("page", function(value) {
		var html = [];
		var all = getArr(this.number,this.totalPages);
		html.push('<li><a href="/erpproduct/size/index/1" aria-label="Previous">&laquo;</a></li>')
		$.each(all, function(index) {
			html.push('<li><a href="/erpproduct/size/index/' + all[index] + '" aria-label="Previous">' + all[index] + '</a></li>');
		})
		html.push('<li><a href="/erpproduct/size/index/'+this.totalPages+'" aria-label="Previous">&raquo;</a></li>')
		return html.join('');
	});
	var pagecm = Handlebars.compile($("#pagenation-template").html().replace(/\\/g, ''));

	$('#pagewrapper').html(pagecm({
		totalPages: $('#totalPages').text(),
		number:$('#number').text()
	}));
};