uniapp开辟小程序,制作选择下拉框(履历少,做的不完美) ...

农民  论坛元老 | 2024-11-4 06:19:50 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1018|帖子 1018|积分 3054

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
一、最终结果
1、默认样式(默认选择第一条数据)

2、点击后默认展示10条数据

3、支持模糊查询

4、选择项目确认后

二、代码实现过程
  1. <template>
  2.         <view class="layout">
  3.                 <!-- #ifndef MP-TOUTIAO -->
  4.                 <custom-nav-bar title="物品出库" withBack @backPage="backSpace"></custom-nav-bar>
  5.                 <!-- #endif -->
  6.                 <view class="inner">
  7.                         <view class="content">
  8.                                 <view class="goodsInfo">
  9.                                         <view class="top">
  10.                                                 <view class="img">
  11.                                                         <u--image :src="baseUrl + info.goodsImages" width="80px" height="80px" radius="4"></u--image>
  12.                                                 </view>
  13.                                                 <view class="text">
  14.                                                         <u--text :text="info.goodsName" bold lines="1" size="20px"></u--text>
  15.                                                         <view style="margin-top: 10rpx;">
  16.                                                                 <u--text :text="info.goodsCode"  lines="1" size="14px" color="#999"></u--text>
  17.                                                         </view>
  18.                                                 </view>
  19.                                         </view>
  20.                                         <view class="goodsDesc">
  21.                                                 <view class="row">
  22.                                                         <view class="left">
  23.                                                                 仓库名
  24.                                                         </view>
  25.                                                         <view class="right">
  26.                                                                 {{info.wareHouseName}}
  27.                                                         </view>
  28.                                                 </view>
  29.                                                 <view class="row">
  30.                                                         <view class="left">
  31.                                                                 库位号
  32.                                                         </view>
  33.                                                         <view class="right">
  34.                                                                 {{info.wareStorageCode}}
  35.                                                         </view>
  36.                                                 </view>
  37.                                                 <view class="row">
  38.                                                         <view class="left">
  39.                                                                 规格
  40.                                                         </view>
  41.                                                         <view class="right">
  42.                                                                 {{info.goodsSize==null ? '暂无' :info.goodsSize}}
  43.                                                         </view>
  44.                                                 </view>
  45.                                                 <view class="row">
  46.                                                         <view class="left">
  47.                                                                 单位
  48.                                                         </view>
  49.                                                         <view class="right">
  50.                                                                 {{info.goodsUnit}}
  51.                                                         </view>
  52.                                                 </view>
  53.                                                 <view class="row">
  54.                                                         <view class="left">
  55.                                                                 类别
  56.                                                         </view>
  57.                                                         <view class="right">
  58.                                                                 {{info.goodsCategory }}
  59.                                                         </view>
  60.                                                 </view>
  61.                                                 <view class="row">
  62.                                                         <view class="left">
  63.                                                                 库存数量
  64.                                                         </view>
  65.                                                         <view class="right">
  66.                                                                 {{info.goodsInventoryNum}}
  67.                                                         </view>
  68.                                                 </view>
  69.                                         </view>
  70.                                 </view>
  71.                         </view>
  72.                         <view class="intoNum">
  73.                                 <u-form
  74.                                 labelWidth="180rpx"
  75.                                       :model="formData"
  76.                                       :rules="rules"
  77.                                       ref="form"
  78.                                     >
  79.                                       <u-form-item
  80.                                         label="出库数量"
  81.                                         prop="outNum"
  82.                                       >
  83.                                         <u--input
  84.                                           v-model="formData.outNum"
  85.                                           type="number"
  86.                                                   border="none"
  87.                                           placeholder="请输入数量"
  88.                                         />
  89.                                       </u-form-item>
  90.                                           
  91.                                           <!-- 下拉选择框-->
  92.                                          <view class="project" v-if="projectFlag">
  93.                                                  <u-form-item
  94.                                                    label="关联项目"
  95.                                                    prop="projectName"
  96.                                                  >
  97.                                                    <view class="put" @click="isNation=!isNation">
  98.                                                            <u--input
  99.                                                                 v-model="formData.projectName"
  100.                                                                 type="text"
  101.                                                                 border="none"
  102.                                                                  disabled
  103.                                                                 placeholder="请选择关联项目"
  104.                                                            />
  105.                                                    </view>
  106.                                                  </u-form-item>
  107.                                          </view>
  108.                                 </u-form>
  109.                         </view>
  110.                         <view class="remark">
  111.                                 <view class="text">
  112.                                         备注信息
  113.                                 </view>
  114.                                 <u--textarea v-model="remarkText" placeholder="请输入备注信息" border="null"></u--textarea>
  115.                         </view>
  116.                         <view class="upLoadImg" >
  117.                                 <view class="img">
  118.                                         <text>出库照片</text>
  119.                                 </view>
  120.                                 <u-upload
  121.                                         class="upload"
  122.                                         :fileList="fileList1"
  123.                                         @afterRead="afterRead"
  124.                                         @delete="deletePic"
  125.                                         name="1"
  126.                                         multiple
  127.                                         :maxCount="9"
  128.                                 ></u-upload>
  129.                         </view>
  130.                         <view class="footer">
  131.                                 <view class="certain" @click="certain">
  132.                                         <btn title="添加出库单"></btn>
  133.                                 </view>
  134.                         </view>
  135.                 </view>
  136.                 <!-- 下拉选择框底部弹出框 -->
  137.                 <view class="date-background" v-show="isNation">
  138.                         <view class='date-gray-background' @click="hiddeDatePicker"></view>
  139.                         <view class='date-container'>
  140.                                 <view class="transparent">
  141.                                         <view class='date-confirm'>
  142.                                                 <view @click="hiddeDatePicker" class="pickerCancel">取消</view>
  143.                                                 <!-- 输入框 -->
  144.                                                 <u-search style="width: 70%;" placeholder="请输入项目名称" @clear="clearSearch" @change='searchChange'
  145.                                                         :show-action='false' v-model="searchValue" shape="round" clearabled></u-search>
  146.                                                 <view @click="confirm1" class="pickerConfirm">确认</view>
  147.                                         </view>
  148.                                                 <picker-view :immediate-change='true' indicator-class="indicator" :value="setValues"
  149.                                                         @change="bindChange" mask-style="height:100rpx;"
  150.                                                         style="width: 100%; height: 80%;position:absolute;bottom:0rpx;text-align:center;background:white">
  151.                                                         <picker-view-column class="pickViewColumn">
  152.                                                                 <view v-for="item in array" :key="item.id" class="u-column-item"
  153.                                                                         style="height: 68rpx;overflow: hidden;">{{item.projectName}}
  154.                                                                 </view>
  155.                                                         </picker-view-column>
  156.                                                 </picker-view>
  157.                                 </view>
  158.                         </view>
  159.                 </view>
  160.         </view>
  161. </template>
  162. <script>
  163.         import config from '@/config'
  164.         import { getToken } from '@/utils/auth'
  165.         import {onLoad,onReady} from "@dcloudio/uni-app"
  166.         import {apiGetGoodsInfo,apiFdProject} from "@/api/home.js"
  167.         import {mounted,watch} from "vue"
  168.         export default {
  169.                 data(){
  170.                         return{
  171.                                 projectFlag:false,
  172.                                 baseUrl:config.baseUrl,
  173.                                 pageNum:1,
  174.                                 pageSize:10,
  175.                                 projectCode:'',
  176.                                 //搜索下拉框
  177.                                 proIndex:0,
  178.                                 setValues:[0],
  179.                                 array: [],
  180.                                 searchValue:'',
  181.                                 isNation:false,
  182.                                 baseUrl:config.baseUrl,
  183.                                 wareHouseName:'',
  184.                                 info:{},
  185.                                 remarkText:'',
  186.                                 operationType:2,
  187.                                 files:'',
  188.                                 fileList1:[],
  189.                                 lastList:[],
  190.                                 //表单验证
  191.                                 goodsNum:0,
  192.                                 formData: {
  193.                                                 outNum: '', // 初始化为空字符串
  194.                                                 projectName:''
  195.                                         },
  196.                                 rules: {
  197.                                         outNum: [
  198.                                                         {
  199.                                                                 required: true,
  200.                                                                 message: "请输入出库数量",
  201.                                                                 trigger: ['blur', 'change']
  202.                                                         },
  203.                                                         {
  204.                                                                 type: 'number',
  205.                                                                 message: '请输入数字',
  206.                                                                 trigger: ['blur', 'change']
  207.                                                         },
  208.                                                         {
  209.                                                                 validator: (rule, value, callback) => {
  210.                                                                         console.log(this.goodsNum)
  211.                                                                         console.log(value)
  212.                                                                    if (value > this.goodsNum) {
  213.                                                                                         callback(new Error(`出库数量不能大于库存数量 ${this.goodsNum}`));
  214.                                                                                 } else if (value <= 0) {
  215.                                                                                         // 修正:判断小于等于 0
  216.                                                                                         callback(new Error('出库数量必须大于 0'));
  217.                                                                                 } else {
  218.                                                                                         callback();
  219.                                                                         }
  220.                                                                 },
  221.                                                                 trigger: ['blur','change']
  222.                                                         }
  223.                                                 ],
  224.                                         },
  225.                         }
  226.                 },
  227.                 methods:{
  228.                         setValueInit(){
  229.                                 this.$nextTick(()=>{
  230.                                         this.setValues=[0]
  231.                                         this.formData.projectName=this.array[0].projectName
  232.                                         this.projectCode = this.array[0].projectCode;
  233.                                 })
  234.                         },
  235.                         backSpace(){
  236.                                 // 返回上个页面,此处使用navigateBack避免页面栈爆掉
  237.                                 uni.navigateBack({
  238.                                         url:"/pages/home/outBill/outBill"
  239.                                 })
  240.                         },
  241.                         //隐藏底部弹出框
  242.                         clearSearch() {
  243.                                 //清空搜索内容
  244.                                 this.searchValue = ''
  245.                         },
  246.                         hiddeDatePicker(){
  247.                                 this.isNation=false
  248.                         },
  249.                         bindChange(e) {
  250.                                  const proIndex = e.detail.value[0]; // 获取选中项目的索引
  251.                                   this.formData.projectName = this.array[proIndex].projectName;
  252.                                   this.projectCode = this.array[proIndex].projectCode;
  253.                                 console.log(e)
  254.                                 // 获取选择得项目的索引
  255.                                 // let proIndex = e.detail.value.toString()
  256.                                 // this.array.forEach((item,index)=>{
  257.                                 //         if(proIndex==index){
  258.                                 //                 this.$nextTick(()=>{
  259.                                 //                         this.formData.projectName=item.projectName
  260.                                 //                         this.projectCode = item.projectCode
  261.                                 //                 })
  262.                                                
  263.                                 //         }
  264.                                 // })
  265.                         },
  266.                         // 确认关闭
  267.                         confirm1(e) {
  268.                                 console.log(this.array)
  269.                                 this.setValues=[0]
  270.                                 this.isNation=false
  271.                         },
  272.                         // 查询项目
  273.                         searchChange(e){
  274.                                 this.searchValue=e
  275.                                 this.getPro()
  276.                                 this.setValueInit()
  277.                         },
  278.                         // 获取物品信息
  279.                         getGoodsInfo(id){
  280.                                 apiGetGoodsInfo(id).then(res=>{
  281.                                 this.info=res.data
  282.                         // 判断物品是否需要关联项目
  283.                                 if(this.info.goodsCategoryCode==='3'||this.info.goodsCategoryCode==='5'){
  284.                                         this.projectFlag=true
  285.                                 }
  286.                                 this.goodsNum=res.data.goodsInventoryNum
  287.                                 })
  288.                         },
  289.                         // 获取项目数据
  290.                         getPro(){
  291.                                 apiFdProject({pageNum:this.pageNum,pageSize:this.pageSize,projectName:this.searchValue}).then(res=>{
  292.                                         this.array=[...res.rows]
  293.                                 })
  294.                         },
  295.                         //删除图片
  296.                         deletePic(event) {
  297.                                 this[`fileList${event.name}`].splice(event.index, 1)
  298.                                 this.lastList.splice(event.index,1)
  299.                                 this.files=this.lastList.join(',')
  300.                         },
  301.                         // 新增图片
  302.                         async afterRead(event) {
  303.                                 // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  304.                                 let lists = [].concat(event.file)
  305.                                 let fileListLen = this[`fileList${event.name}`].length
  306.                                 lists.map((item) => {
  307.                                         this[`fileList${event.name}`].push({
  308.                                                 ...item,
  309.                                                 status: 'uploading',
  310.                                                 message: '上传中'
  311.                                         })
  312.                                 })
  313.                                 for (let i = 0; i < lists.length; i++) {
  314.                                         const result = await this.uploadFilePromise(lists[i].url)
  315.                                         let item = this[`fileList${event.name}`][fileListLen]
  316.                                         this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
  317.                                                 status: 'success',
  318.                                                 message: '',
  319.                                                 url: result
  320.                                         }))
  321.                                         fileListLen++
  322.                                 }
  323.                         },
  324.                         uploadFilePromise(url) {
  325.                                 return new Promise((resolve, reject) => {
  326.                                         let a = uni.uploadFile({
  327.                                                 url: this.baseUrl + '/common/upload',
  328.                                                 filePath: url,  
  329.                                                 name: 'file',  
  330.                                                 formData: {},
  331.                                                 header: {  
  332.                                                   Authorization: 'Bearer ' + getToken(),
  333.                                                  'Content-Type': 'multipart/form-data'
  334.                                                 },  
  335.                                                 success: (res) => {
  336.                                                         this.lastList.push(JSON.parse(res.data).fileName)
  337.                                                         this.files=this.lastList.join(',')
  338.                                                         setTimeout(() => {
  339.                                                                 resolve(res.data.data)
  340.                                                         }, 500)
  341.                                                 }
  342.                                         });
  343.                                 })
  344.                         },
  345.                         certain(){
  346.                                 if(this.formData.outNum<=0){
  347.                                         uni.showModal({
  348.                                                 title:'提示',
  349.                                                 content:'请正确填写出库数量',
  350.                                                 showCancel:false,
  351.                                                 confirmText:"去填写",
  352.                                                 success: (res) => {
  353.                                                         if(res.confirm){
  354.                                                                 // 用户点击确定,但因为没有填写数量,所以不需要跳转到其他页面或提交数据  
  355.                                                         }
  356.                                                 }
  357.                                         })
  358.                                         return
  359.                                 }
  360.                                
  361.                                 if(this.projectFlag){
  362.                                         if(!this.formData.projectName){
  363.                                                 uni.showModal({
  364.                                                         title:'提示',
  365.                                                         content:'没有关联项目',
  366.                                                         showCancel:false,
  367.                                                         confirmText:"去关联",
  368.                                                         success: (res) => {
  369.                                                                 if(res.confirm){
  370.                                                                         // 用户点击确定,但因为没有填写数量,所以不需要跳转到其他页面或提交数据  
  371.                                                                 }
  372.                                                         }
  373.                                                 })
  374.                                                 return
  375.                                         }
  376.                                 }
  377.                                 //设置info对象的数据
  378.                                 this.info.operationNum=Number(this.formData.outNum)
  379.                                 this.info.remark=this.remarkText
  380.                                 this.info.operationType = this.operationType
  381.                                 this.info.projectCode = this.projectCode
  382.                                 this.info.projectName = this.formData.projectName
  383.                                 if(this.files!==''&&this.files){
  384.                                         this.info.operationImage = this.files
  385.                                 }else{
  386.                                         this.info.operationImage = ''
  387.                                 }
  388.                                 this.$store.dispatch('addToOutCart',this.info).then(()=>{
  389.                                 })
  390.                                 .catch((err)=>{
  391.                                         console.log('添加购物车失败',err)
  392.                                 })
  393.                         }
  394.                 },
  395.                 onReady() {
  396.                         //如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则。
  397.                     this.$refs.form.setRules(this.rules)
  398.                     },
  399.                 // 与vue中的mounted作用类似,但是执行时间更早
  400.                 onLoad(options) {
  401.                         let id = options.id
  402.                         this.wareHouseName = options.wareHouseName
  403.                         this.getGoodsInfo(id)
  404.                         this.getPro()
  405.                 },
  406.                 watch:{
  407.                         array:function (newVal,oldVal) {
  408.                                 console.log(newVal)
  409.                                 this.formData.projectName = newVal[0].projectName;
  410.                                 this.projectCode = newVal[0].projectCode;
  411.                         }
  412.                 },
  413.                 mounted(){
  414.                         this.setValueInit()
  415.                 }
  416.         }
  417. </script>
  418. <style lang="scss" scoped>
  419. .layout{
  420.         width: 100%;
  421.         height: 100%;
  422.         box-sizing: border-box;
  423.         padding: 30rpx;
  424.         padding-bottom: 140rpx;
  425.         background-color: #f2f4f8;
  426.         .inner{
  427.                 height: 1240rpx;
  428.                 overflow: hidden;
  429.                 overflow-y: auto;
  430.                 .content{
  431.                         box-sizing: border-box;
  432.                         padding:20rpx 30rpx;
  433.                         background-color: #fff;
  434.                         border-radius: 20rpx;
  435.                         .top{
  436.                                 height: 240rpx;
  437.                                 box-sizing: border-box;
  438.                                 background-color: #f2f4f8;
  439.                                 padding: 20rpx;
  440.                                 border-radius: 20rpx;
  441.                                 position: relative;
  442.                                 .img{
  443.                                         position: absolute;
  444.                                         top:20%;
  445.                                         left: 60rpx;
  446.                                 }
  447.                                 .text{
  448.                                         position: absolute;
  449.                                         top:30%;
  450.                                         left: 40%;
  451.                                 }
  452.                         }
  453.                         .goodsDesc{
  454.                                 margin-top: 40rpx;
  455.                                 .row{
  456.                                         margin-bottom: 15rpx;
  457.                                         display: flex;
  458.                                         justify-content: space-between;
  459.                                         .left{
  460.                                                 color:#999;
  461.                                                 font-size: 14px;
  462.                                         }
  463.                                         .right{
  464.                                                 color:#333;
  465.                                                 font-size: 14px;
  466.                                         }
  467.                                 }
  468.                         }
  469.                 }
  470.                 .intoNum,.remark,.upLoadImg{
  471.                         margin-top: 20rpx;
  472.                         box-sizing: border-box;
  473.                         padding:20rpx 30rpx;
  474.                         background-color: #fff;
  475.                         border-radius: 15rpx;
  476.                         display: flex;
  477.                 }
  478.                
  479.                 .intoNum{
  480.                         align-items: center;
  481.                         justify-content: start;
  482.                         .put{
  483.                                 display: flex;
  484.                                 align-items: center;
  485.                         }
  486.                 }
  487.                 .remark{
  488.                         align-items: start;
  489.                         .text{
  490.                                 margin-top: 15rpx;
  491.                         }
  492.                 }
  493.                 .upLoadImg{
  494.                         align-items: center;
  495.                         justify-content: space-between;
  496.                         .upload{
  497.                                 margin-left: 40rpx;
  498.                         }
  499.                 }
  500.                 .footer{
  501.                         padding: 0 20rpx;
  502.                         position: fixed;
  503.                         bottom: 0;
  504.                         left: 0;
  505.                         height: 160rpx;
  506.                         width: 100%;
  507.                         display: flex;
  508.                         align-items: center;
  509.                         justify-content: center;
  510.                         background-color: #fff;
  511.                         border-radius: 25rpx;
  512.                 }
  513.         }
  514.         //底部弹出框样式
  515.         .date-background {
  516.                 position: fixed;
  517.                 left: 0;
  518.                 top: 0;
  519.                 bottom: 0;
  520.                 width: 100%;
  521.                 height: 100%;
  522.                 z-index: 1111;
  523.         }
  524.          
  525.         .date-gray-background {
  526.                 position: absolute;
  527.                 width: 100%;
  528.                 top: 0rpx;
  529.                 background: rgba(0, 0, 0, .5);
  530.                 height: calc(100% - 500rpx);
  531.         }
  532.          
  533.         .date-container {
  534.                 position: absolute;
  535.                 width: 100%;
  536.                 height: 60%;
  537.                 overflow: hidden;
  538.                 background: #fff;
  539.                 bottom: 0;
  540.                 z-index: 1000;
  541.         }
  542.          
  543.         .date-confirm {
  544.                 display: flex;
  545.                 justify-content: space-between;
  546.                 align-items: center;
  547.                 padding: 20rpx;
  548.                 font-size: 34rpx;
  549.                 line-height: 100rpx;
  550.                 z-index: 2;
  551.         }
  552.          
  553.         .pickViewColumn {
  554.                 height: 60%;
  555.         }
  556.          
  557.         .indicator {
  558.                 height: 40rpx;
  559.         }
  560.          
  561.         .pickerCancel {
  562.                 font-size: 30rpx;
  563.                 color: #606266;
  564.                 box-sizing: border-box;
  565.                 text-align: center;
  566.                 text-decoration: none;
  567.          
  568.                 padding: 0rpx 8rpx;
  569.         }
  570.          
  571.         .pickerConfirm {
  572.                 font-size: 30rpx;
  573.                 color: #2979ff;
  574.                 box-sizing: border-box;
  575.                 text-align: center;
  576.                 text-decoration: none;
  577.                 padding: 0rpx 8rpx;
  578.         }
  579.          
  580.         .u-column-item {
  581.                 display: flex;
  582.                 flex-direction: row;
  583.                 align-items: center;
  584.                 justify-content: center;
  585.                 font-size: 30rpx;
  586.                 color: #303133;
  587.                 padding: 0 8rpx;
  588.         }
  589. }
  590. </style>
复制代码
上述代码为实现过程

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

农民

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表