onclick="create(this)"; function create(obj) { obj = $(obj); if (!obj.hasClass("selected")) { return true; } var orderItemMap = {}; var tr = $("input.cbx_select:checked").parents("tr.orderItem_info"); tr.each(function () { var key = $(this).find(".input_orderItem").attr("name"); orderItemMap[key] = $(this).find(".item_amount").children("input").val(); }); $.ajax({ url: "/tmall/orderItem", type: "PUT", data: { "orderItemMap": JSON.stringify(orderItemMap) }, traditional: true, success: function (data) { if (data.success) { location.href = "/tmall/order/create/byCart?order_item_list=" + data.orderItemIDArray; return true; } else { alert("购物车商品结算异常,请稍候再试!"); location.href = "/tmall/cart"; } }, beforeSend: function () { }, error: function () { alert("购物车商品结算异常,请稍候再试!"); location.href = "/tmall/cart"; } }); }
时间: 2024-02-14 11:32:36 浏览: 18
这段代码是一个 JavaScript 函数,其中 onclick 为 HTML 中的一个事件属性,表示当用户点击某个元素时会触发该函数。具体函数实现如下:
1. 首先,函数接收一个参数 obj,并将其转换为 jQuery 对象。
2. 然后,判断该元素是否具有 "selected" 类。如果没有,直接返回 true,不执行后续逻辑。
3. 如果具有 "selected" 类,则创建一个空对象 orderItemMap,用于存储订单项信息。
4. 遍历所有被选中的商品行(即选中了复选框的行),将每个商品的名称和数量存储到 orderItemMap 对象中。
5. 使用 Ajax 发送 PUT 请求到服务器的 "/tmall/orderItem" 地址,并将 orderItemMap 对象作为请求参数传递给服务器。
6. 如果请求成功,服务器会返回一个包含订单项 ID 的数组,该函数会将其作为参数,跳转到订单创建页面。如果请求失败,则弹出提示消息并跳转到购物车页面。
7. 在 Ajax 请求发送前和结束后,分别执行 beforeSend 和 success 回调函数。如果请求出错,则执行 error 回调函数。
总之,这段代码的作用是在用户点击结算按钮时,将购物车中选中的商品转换为订单项,并跳转到订单创建页面。
相关问题
handleOk () { const that = this this.$refs.modalForm.validate((valid) => { if (valid) { let obj if (!this.formData.module_id) { // 新增 obj = api.functionAdd(this.params) } else { // 单行编辑 obj = this.type !== '2' ? api.functionEdit(this.params) : api.functionCopy(this.params, this.params.module_id) } obj .then((res) => { if (res.success) { that.$message.success(res.message) that.$emit('ok', this.type) } else { that.$message.error(res.message) } }) .finally(() => { that.handleCancel() }) } }) },优化以上代码
你可以优化以上代码的部分。首先,你可以使用箭头函数的隐式返回来简化代码。在`then`和`finally`回调中,可以直接返回Promise对象,而不是将其赋值给`obj`变量。像这样:
```javascript
handleOk() {
this.$refs.modalForm.validate((valid) => {
if (valid) {
let obj;
if (!this.formData.module_id) {
// 新增
obj = api.functionAdd(this.params);
} else {
// 单行编辑
obj = this.type !== '2' ? api.functionEdit(this.params) : api.functionCopy(this.params, this.params.module_id);
}
return obj
.then((res) => {
if (res.success) {
this.$message.success(res.message);
this.$emit('ok', this.type);
} else {
this.$message.error(res.message);
}
})
.finally(() => {
this.handleCancel();
});
}
});
}
```
此外,你可以使用箭头函数来绑定`that`变量。这样就不需要在函数内部创建`that`变量并赋值给`this`。只需直接使用`this`来访问组件实例。像这样:
```javascript
handleOk() {
this.$refs.modalForm.validate((valid) => {
if (valid) {
let obj;
if (!this.formData.module_id) {
// 新增
obj = api.functionAdd(this.params);
} else {
// 单行编辑
obj = this.type !== '2' ? api.functionEdit(this.params) : api.functionCopy(this.params, this.params.module_id);
}
return obj
.then((res) => {
if (res.success) {
this.$message.success(res.message);
this.$emit('ok', this.type);
} else {
this.$message.error(res.message);
}
})
.finally(() => {
this.handleCancel();
});
}
});
}
```
最后,确保你在代码中使用合适的缩进和格式化,以提高可读性。
handerPersonList(list,result,type){ // 获取所有 收藏人员 数据Id let empIds = this.getPersonEmpId(this.latelyList); let arr = []; if(list && list.length > 0){ for(let item of list){ let obj = Object.assign({},item); obj.admin = util.combineName(item); obj.id = ''; obj.aId = ''; obj.bId = ''; obj.picSrc = item.picSrc || require('@/assets/common_images/person.jpg'); // 设置收藏 if(empIds.indexOf(obj.empId) > -1){ let latelyUser = this.latelyList.find(it=>it.empId == obj.empId); if(latelyUser){ obj.id = latelyUser.id; } obj.isCollect = true; } else{ obj.isCollect = false; } arr.push(obj); } } if(type && type == 'allUser'){ this.paginationuser.total = result.pageControllerInfo.totalRecordCount; this.$refs.userTable.bodyWrapper.scrollTop = 0; this.userdata = arr; }else{ this.orgPersonList = arr; this.pagination.total = result.pageControllerInfo.totalRecordCount; this.$refs.employeeTable.bodyWrapper.scrollTop = 0; } },
这段代码主要是用来处理收藏人员列表数据的。它接收三个参数,分别是 `list`,`result` 和 `type`。
首先,代码会使用 `getPersonEmpId` 函数获取所有收藏人员的 ID,然后对 `list` 数组进行遍历,对每个元素进行处理。处理后的每个元素会被添加一些属性,比如 `admin`,`id`,`aId`,`bId`,`picSrc` 和 `isCollect` 等。其中,`admin` 表示人员名称,`id` 表示收藏人员 ID,`aId` 和 `bId` 分别表示一级部门和二级部门的 ID,`picSrc` 表示人员头像,`isCollect` 表示人员是否被收藏。
如果 `empIds.indexOf(obj.empId) > -1`,则表示当前人员已被收藏。此时,代码会在 `latelyList` 中查找该人员并获取其 ID,然后将该 ID 赋值给 `obj.id`。最后,设置 `obj.isCollect` 为 `true`。
如果 `empIds.indexOf(obj.empId) < 0`,则表示当前人员未被收藏。此时,代码会将 `obj.isCollect` 设置为 `false`。
最后,如果 `type` 的值为 `allUser`,则将处理后的数据赋值给 `this.userdata`,否则赋值给 `this.orgPersonList`。