效果图
功能说明
-
设置默认发货方式:
- 默认发货只能选择一个或者不选,要做一个切换功能
-
设置常用物流和默认物流
- 常用物流可多选或者不选,不选则不能为默认
- 默认只可以有一个
-
思路:记录上一个选择的索引
- 初始化时 记住索引
- 初始化时 没有索引 把当前的索引给初始化时的索引
demo
js
Page({
data: {
logiList: [{
CPNM: "物之桥测试",
HYID: "300",
RN: "1",
STUS: "2",
}, {
CPNM: "物之桥",
HYID: "444",
RN: "2",
STUS: "0"
}, {
CPNM: "三友物流",
HYID: "1647",
RN: "3",
STUS: "1"
}],
preIndex: "0",
deliveryModeList: [{
FTNH: "1",
FTNM: "自送至物流",
STUS: "Y"
}, {
FTNH: "2",
FTNM: "物流上门",
STUS: "2"
}, {
FTNH: "3",
FTNM: "指派代发",
STUS: "3"
}],
preModeIndex: "0",
page: 1
},
staticVal: {
logiCompany: "",
isNodata: false
},
onLoad: function(options) {
let t = this;
t.queryOrdGeyDic();
t.queryOrdGetLogCity(1, "", "已显示全部标记");
},
queryOrdGeyDic() {
let t = this;
// wx.request({
// url: '你的接口',
// method: "POST",
// header: getApp().globalData.header,
// success(res) {
// let rows = res.data.rows;
// if (rows && rows.length > 0) {
// t.setData({
// deliveryModeList: rows
// });
// let preModeIndex = t.searchNext(rows, 'Y', "STUS");
// t.setData({
// preModeIndex: preModeIndex
// });
// preModeIndex
// } else {
// wx.showToast({
// title: '已显示全部标记',
// icon: 'loading',
// duration: 1000
// })
// }
// }
// });
},
//查询常用物流
queryOrdGetLogCity(page, comp, title) {
let t = this;
// wx.request({
// url: '你的接口',
// method: "POST",
// header: getApp().globalData.header,
// data: {
// page: page,
// rows: 20,
// comp: comp
// },
// success(res) {
// let rows = res.data.rows;
// if (rows && rows.length > 0) {
// t.setLogiList(rows);
// let preIndex = t.searchNext(rows, 2, "STUS");
// t.setData({
// preIndex: preIndex
// });
// } else {
// t.staticVal.isNodata = true;
// wx.showToast({
// title: title,
// icon: 'loading',
// duration: 300
// })
// }
// }
// })
},
queryLogiCompany(e) {
let t = this;
let val = this.staticVal.logiCompany;
t.queryOrdGetLogCity(1, val, "正在查询..");
},
handlerLogiCompany(e) {
this.staticVal.logiCompany = e.detail.value;
},
setLogiSTUS(hyid, stus) {
let t = this;
// wx.request({
// url: '你的接口' + stus,
// method: "POST",
// header: getApp().globalData.header,
// data: {
// HYID: hyid
// },
// success(res) {
// if (res.data > 0) {
// } else {
// wx.showToast({
// title: '设置失败',
// icon: 'loading',
// duration: 1000
// })
// }
// }
// })
},
setDeliveryMode(ftnh) {
let t = this;
// wx.request({
// url: '你的接口',
// method: "POST",
// header: getApp().globalData.header,
// data: {
// code: ftnh
// },
// success(res) {
// if (res.data > 0) {
// } else {
// wx.showToast({
// title: '设置失败',
// icon: 'loading',
// duration: 1000
// })
// }
// }
// })
},
selectedLogi(e) {
let t = this;
let item = e.currentTarget.dataset.item;
if (item.STUS > 0) {
t.setLogiSTUSValue(item, 0);
} else {
t.setLogiSTUSValue(item, 1);
}
},
setLogiSTUSValue(item, stus) {
let t = this;
let p = new Promise((resolve, reject) => {
t.setLogiSTUS(item.HYID, stus);
resolve()
});
p.then(() => {
t.data.logiList[item.RN - 1].STUS = stus;
t.setData({
logiList: t.data.logiList
});
})
return item.RN - 1;
},
selectedDefaultLogi(e) {
let t = this;
let preIndex = t.data.preIndex;
let item = e.currentTarget.dataset.item;
if (item.STUS > 1) {
t.setLogiSTUSValue(item, 1);
} else {
let currentIndex = t.setLogiSTUSValue(item, 2);
if (t.data.logiList[preIndex]) {
if (t.data.logiList[preIndex].STUS * 1 > 0 && currentIndex * 1 != preIndex * 1) {
t.data.logiList[preIndex].STUS = 1;
t.setData({
logiList: t.data.logiList
});
}
}
t.setData({
preIndex: currentIndex
});
}
},
setDeliveryModeSTUS(item) {
let t = this;
let p = new Promise((resolve, reject) => {
t.setDeliveryMode(item.FTNH);
resolve()
});
p.then(() => {
t.data.deliveryModeList[item.FTNH - 1].STUS = 'Y';
t.setData({
deliveryModeList: t.data.deliveryModeList
});
})
return item.FTNH - 1;
},
selectedDeliveryMode(e) {
let t = this;
let preModeIndex = t.data.preModeIndex;
let item = e.currentTarget.dataset.item;
if (item.STUS == 'Y') {
t.data.deliveryModeList[item.FTNH - 1].STUS = 'N';
t.setData({
deliveryModeList: t.data.deliveryModeList
});
} else {
let currentIndex = t.setDeliveryModeSTUS(item)
if (t.data.deliveryModeList[preModeIndex]) {
if (t.data.deliveryModeList[preModeIndex].STUS == 'Y' && currentIndex * 1 != preModeIndex * 1) {
t.data.deliveryModeList[preModeIndex].STUS = 'N';
t.setData({
deliveryModeList: t.data.deliveryModeList
});
}
}
t.setData({
preModeIndex: currentIndex
});
}
},
setLogiList(rows) {
let t = this;
if (t.data.page == 1) {
t.setData({
logiList: rows
});
} else {
let d = t.data.logiList;
t.setData({
logiList: d.concat(rows)
});
}
},
tolowerReflesh() {
let t = this;
if (!t.staticVal.isNodata) {
t.setData({
page: t.data.page + 1
});
t.queryOrdGetLogCity(t.data.page, "", "");
}
},
searchNext(arr, key, field) {
for (let i = 0; i < arr.length; i++) {
if (key == arr[i][field]) {
return i;
}
}
},
stopBubble() {
return;
}
})
wxml
<view class="adl-wrap">
<view class="adl-item-list adl-item-wrap">
<view class="adl-item-title">设置默认发货方式</view>
<view class="adl-item-content">
<view class="adl-item-content-l" style="padding:0 15rpx 15rpx;justify-content: flex-start;">
<block wx:for="{{deliveryModeList}}" wx:key="{{index}}">
<view class="adl-item-content-wrap {{item.STUS=='Y'?'active':''}}" bindtap="selectedDeliveryMode" data-item="{{item}}">
<text class="adl-item-content-text" style=" padding: 0 15rpx;">{{item.FTNM}}</text>
</view>
</block>
</view>
</view>
</view>
<view class="adl-item-wrap">
<view class="adl-item-head">
<view class="adl-item-head-title">设置常用物流公司和默认物流公司</view>
</view>
<view class="adl-item-query">
<input class="adl-item-query-input" type="text" bindinput="handlerLogiCompany" value="" placeholder="输入物流公司名" />
<view class="adl-item-query-select" bindtap="queryLogiCompany">搜索</view>
</view>
<view class="adl-item-head">
<view class="adl-item-head-title adl-font">物流公司列表</view>
<view class="adl-item-head-title adl-font">设为默认</view>
</view>
<view class="adl-item-cotent">
<scroll-view id="page" scroll-y="true" style="height:740rpx;" bindscrolltolower="tolowerReflesh">
<view class="adl-item-content-l" wx:for="{{logiList}}" wx:key="{{index}}">
<view class="adl-item-content-wrap {{item.STUS>0?'active':''}}" bindtap="selectedLogi"
data-item="{{item}}">
<text class="adl-item-content-text">{{item.CPNM}}</text>
</view>
<view catchtap="stopBubble">
<switch checked="{{item.STUS>1?true:false}}" data-item="{{item}}"
bindtap="selectedDefaultLogi" />
</view>
</view>
</scroll-view>
</view>
</view>
</view>
wxss
.adl-wrap {
display: flex;
flex-direction: column;
padding: 15rpx 15rpx;
}
.adl-item-list {
display: flex;
flex-direction: column;
border-bottom: 10rpx solid #F3F1F1;
margin-top: 15rpx;
}
.adl-item-title {
height: 70rpx;
font-size: 30rpx;
font-weight: 540;
}
.adl-item-wrap .adl-item-wrap {
display: flex;
flex-direction: column;
}
.adl-item-wrap .adl-item-head {
display: flex;
justify-content: space-between;
padding-top: 30rpx;
}
.adl-item-wrap .adl-item-head-title {
font-weight: 540;
margin-right: 20rpx;
}
.adl-font {
margin-left: 20rpx;
font-size: 28rpx;
color: #302F31;
}
.adl-item-wrap .adl-item-content {
display: flex;
flex-direction: column;
}
.adl-item-wrap .adl-item-content-l {
display: flex;
justify-content: space-between;
padding: 30rpx 15rpx 15rpx;
line-height: 70rpx;
border-bottom: 1rpx solid #F3F1F1;
}
.adl-item-content-l .adl-item-content-wrap {
border-radius: 10rpx;
margin-right: 20rpx;
background: #F7F1F7;
color: #302F31;
}
.adl-item-wrap .adl-item-content-text {
display: inline-block;
padding: 0 30rpx;
font-size: 28rpx;
}
.wx-switch-input {
width: 82rpx !important;
height: 40rpx !important;
background: #23A8F2 !important;
border: #23A8F2 !important;
}
.wx-switch-input::before {
width: 84rpx !important;
height: 40rpx !important;
}
.wx-switch-input::after {
width: 42rpx !important;
height: 40rpx !important;
}
.adl-item-content-wrap.active {
background: #3164FE;
color: #FCFDFF;
}
.adl-item-query {
display: flex;
padding: 15rpx;
margin-top: 15rpx;
border-bottom: 5rpx solid #F3F1F1;
}
.adl-item-query-input {
flex: 10;
align-self: center;
}
.adl-item-query-select {
flex: 2;
text-align: center;
border-radius: 10rpx;
margin-right: 20rpx;
padding: 10rpx 0rpx;
background: #289CFF;
color: #FCFDFF;
}