add.js
2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
*@time: 2016/1/29
*@author: chenglong
*/
var $ = require('jquery');
var edit = require('../common/edit');
var Handlebars = require('yoho.handlebars');
var dropDown = require('../common/dropDown');
var selectOption =
'{{# data}}' +
'<option value="{{id}}">{{sortName}}</option>' +
'{{/ data}}';
var optionStr = Handlebars.compile(selectOption);
function getAllSort(callback) {
$.ajax({
url: '/product/class/queryAllProductSortList',
type: 'POST',
dataType: 'json'
}).then(function (d) {
var firstSort = [],
data = d.data.data,
sortLen = data.length,
i;
for (i = 0; i < sortLen; i++) {
if (!data[i].child) {
firstSort.push(data[i]);
}
}
callback(firstSort);
});
}
function getChildSort(id, callback) {
$.ajax({
url: '/product/class/queryProductSortList',
type: 'POST',
dataType: 'json',
data: {
param: id
}
}).then(function (d) {
callback(d);
});
}
exports.init = function () {
new dropDown({
el:'.level-select'
});
// 页面初始化渲染一级菜单
getAllSort(function (data) {
$('#parentSortId').after(optionStr({
data: data
}));
});
// 选择一\二级菜单时渲染二\三级菜单
$('.level-select').change(function () {
var id = $(this).val();
var thisChild = $(this).attr('data-child');
if (!thisChild) {
return;
}
getChildSort(id, function (data) {
$('#' + thisChild).find('option:first').after(optionStr(data.data));
});
});
// 添加品类表单验证
var newClassVerification = new edit("#new-class-form");
$(".new-class-btn").click(function(){
var id = $(this).attr('data-id');
var postUrl;
if (!!id) {
postUrl = '/product/updateProductSort'
} else {
postUrl = $("#new-class-form").attr("action");
}
newClassVerification.submit(postUrl, function(option,that) {
option.success=function(res){
console.log(res);
window.location.href = '/product/class/index';
};
option.error=function(res){
console.log('error');
}
});
return false;
});
};