• el-select获取id和name


             一般选中节点时只会将:value绑定的数据给v-model中,要想获取id和name一并传给后端,需要如下几步:

            1、给选择框添加点击事件 @input="selectChangeParent" 

            2、v-for中多添加一个参数 index

    1. <el-select v-model="inputForm.projectId" filterable placeholder="请选择"
    2. @input="selectChangeParent">
    3. <el-option
    4. v-for="(item, index) in projectInfo"
    5. :key="item.id"
    6. :label="item.name"
    7. :value="item.id">
    8. el-option>
    9. el-select>

            3、添加选择框点击事件的方法 

    1. selectChangeParent(index){
    2. let listData=[];
    3. listData = this.projectInfo;
    4. for (let i = 0; i <listData.length ; i++) {
    5. if ( listData[i].id==this.inputForm.projectId){
    6. this.inputForm.projectName=listData[i].name
    7. }
    8. }
    9. },

            这是我在data中下拉框的数据结构 

    1. projectInfo: {
    2. id: '',
    3. name: ''
    4. },

             总体思路,点击事件触发时获取到点击的id后,再和v-for绑定的数组做对比,对比时projectInfo的id和点击选择的id相同时,给inputForm里的对象赋值

    完整代码:

    1. <%
    2. layout("/include/_vue_body.html", {title:"列表", parent:"隐患处理"}){
    3. %>
    4. <style>
    5. body {
    6. background: #ffffff !important;
    7. }
    8. style>
    9. <div id="app" class="bg-white">
    10. <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading"
    11. label-width="120px">
    12. <el-row :gutter="15">
    13. <el-col :span="12">
    14. <el-form-item label="隐患名称" prop="hiddenName"
    15. :rules="[
    16. {required: true, message:'隐患名称不能为空', trigger:'blur'}
    17. ]">
    18. <el-input v-model="inputForm.hiddenName" placeholder="请填写隐患名称" maxlength="25">el-input>
    19. el-form-item>
    20. el-col>
    21. <el-col :span="12">
    22. <el-form-item label="整改措施" prop="hiddenMeasurId"
    23. :rules="[
    24. ]">
    25. <el-select v-model="inputForm.hiddenMeasurId" filterable placeholder="请选择">
    26. <el-option
    27. v-for="item in hiddenMeasureList"
    28. :key="item.id"
    29. :label="item.measureName"
    30. :value="item.id">
    31. el-option>
    32. el-select>
    33. el-form-item>
    34. el-col>
    35. <el-col :span="12">
    36. <el-form-item label="整改负责人" prop="principal"
    37. :rules="[
    38. ]">
    39. <fs-user-select
    40. :is_layer="true"
    41. :limit="1"
    42. :value="inputForm.principal"
    43. @get-value="(value) => { inputForm.principal = value}">fs-user-select>
    44. el-form-item>
    45. el-col>
    46. <el-col :span="12">
    47. <el-form-item label="整改监督人" prop="supervision"
    48. :rules="[
    49. ]">
    50. <fs-user-select
    51. :is_layer="true"
    52. :limit="1"
    53. :value="inputForm.supervision"
    54. @get-value="(value) => { inputForm.supervision = value}">fs-user-select>
    55. el-form-item>
    56. el-col>
    57. <el-col :span="12">
    58. <el-form-item label="隐患类型" prop="hiddenType"
    59. :rules="[
    60. ]">
    61. <el-select v-model="hiddenType" @change="selectProjectType">
    62. <el-option v-for="item in hiddenTypeList" :key="item.value" :label="item.label"
    63. :value="item.value">el-option>
    64. el-select>
    65. el-form-item>
    66. el-col>
    67. <el-col :span="12">
    68. <el-form-item label="所属项目" prop="projectId"
    69. :rules="[
    70. ]">
    71. <el-select v-model="inputForm.projectId" filterable placeholder="请选择" @input="selectChangeParent">
    72. <el-option
    73. v-for="(item, index) in projectInfo"
    74. :key="item.id"
    75. :label="item.name"
    76. :value="item.id">
    77. el-option>
    78. el-select>
    79. el-form-item>
    80. el-col>
    81. <el-col :span="24">
    82. <el-form-item label="隐患地点" prop="address"
    83. :rules="[
    84. ]">
    85. <el-input v-model="inputForm.address" placeholder="请填写隐患地点" maxlength="64"
    86. show-word-limit>el-input>
    87. el-form-item>
    88. el-col>
    89. <el-col :span="24">
    90. <el-form-item label="隐患详情" prop="hiddenDetail"
    91. :rules="[
    92. ]">
    93. <el-input :rows="6" type="textarea" v-model="inputForm.hiddenDetail" placeholder="请填写隐患详情"
    94. maxlength="200" show-word-limit>el-input>
    95. el-form-item>
    96. el-col>
    97. <el-col :span="12">
    98. <el-form-item label="隐患图片" prop="hiddenUrl"
    99. :rules="[
    100. ]">
    101. <fs-upload-imgs v-model="inputForm.hiddenUrl">fs-upload-imgs>
    102. el-form-item>
    103. el-col>
    104. el-row>
    105. el-form>
    106. div>
    107. <script>
    108. var formContainer = new Vue({
    109. el: '#app',
    110. mixins: [sharpMixin],
    111. data() {
    112. return {
    113. loading: false,
    114. inputForm: {
    115. id: '${hiddenInfo.id}',
    116. hiddenName: '',
    117. principal: '',
    118. supervision: '',
    119. hiddenUrl: '',
    120. hiddenDetail: '',
    121. address: '',
    122. projectId: '',
    123. hiddenMeasurId: '',
    124. projectType: '',
    125. projectName:'',
    126. hiddenMeasurName: ''
    127. },
    128. hiddenType: '',
    129. hiddenTypeList: [],
    130. projectInfo: {
    131. id: '',
    132. name: ''
    133. },
    134. hiddenMeasure:{
    135. id: '',
    136. measureName: '',
    137. },
    138. hiddenMeasureList:[]
    139. }
    140. },
    141. created() {
    142. let that = this
    143. this.clearForm(function () {
    144. if (that.inputForm.id != '') {
    145. that.loadData()
    146. }
    147. });
    148. this.hiddenTypeList = this.getDictList("hidden_type")
    149. this.initHiddenMeasure()
    150. },
    151. methods: {
    152. loadData() {
    153. this.loading = true
    154. this.get('${ctx}/safety/hiddenInfo/info', {id: this.inputForm.id}).then((res) => {
    155. if (res.data) {
    156. this.inputForm = res.data
    157. this.hiddenType = this.inputForm.projectType
    158. }
    159. this.initBasicData()
    160. this.selectProjectType()
    161. this.loading = false
    162. })
    163. },
    164. selectChangeParent(index){
    165. let listData=[];
    166. listData = this.projectInfo;
    167. for (let i = 0; i length ; i++) {
    168. if ( listData[i].id==this.inputForm.projectId){
    169. this.inputForm.projectName=listData[i].name
    170. }
    171. }
    172. },
    173. initBasicData() {
    174. let initData = {}
    175. Object.assign(this.inputForm, initData);
    176. },
    177. initHiddenMeasure(){
    178. this.get('${ctx}/safety/hiddenMeasure/hiddenList').then((res) => {
    179. if (res.data) {
    180. this.hiddenMeasureList = res.data
    181. }
    182. })
    183. },
    184. initProjectData(type) {
    185. this.get('${ctx}/safety/hiddenInfo/findProjectInfo?type=' + type).then((res) => {
    186. if (res.data) {
    187. this.projectInfo = res.data
    188. }
    189. })
    190. },
    191. initBuildData(type) {
    192. this.get('${ctx}/safety/hiddenInfo/findProjectInfo?type=' + type).then((res) => {
    193. if (res.data) {
    194. this.projectInfo = res.data
    195. }
    196. })
    197. },
    198. initRoomData(type) {
    199. this.get('${ctx}/safety/hiddenInfo/findProjectInfo?type=' + type).then((res) => {
    200. if (res.data) {
    201. this.projectInfo = res.data
    202. }
    203. })
    204. },
    205. selectProjectType() {
    206. this.inputForm.projectType = this.hiddenType
    207. let type = this.inputForm.projectType
    208. if (this.hiddenType == 1) {
    209. //hiddenType为1时查询在建项目信息
    210. this.projectInfo = ''
    211. this.initProjectData(type)
    212. } else if (this.hiddenType == 2) {
    213. //hiddenType为2时查询建筑物信息
    214. this.projectInfo = ''
    215. this.initBuildData(type)
    216. } else {
    217. //hiddenType为3时查询房屋信息
    218. this.projectInfo = ''
    219. this.initRoomData(type)
    220. }
    221. },
    222. clearForm(call) {
    223. let that = this
    224. setTimeout(function () {
    225. that.$refs['inputForm'].resetFields();
    226. if (call) {
    227. call();
    228. }
    229. }, 300)
    230. },
    231. // 提交
    232. doSubmit(call) {
    233. this.$refs['inputForm'].validate((valid) => {
    234. if (valid) {
    235. this.post('${ctx}/safety/hiddenInfo/save', this.inputForm).then((res) => {
    236. if (res.success) {
    237. this.$message.success(res.msg)
    238. call()
    239. } else {
    240. this.$message.error(res.msg)
    241. }
    242. })
    243. } else {
    244. return false;
    245. }
    246. })
    247. }
    248. }
    249. })
    250. script>
    251. <% } %>

  • 相关阅读:
    软考软件设计师----软件工程(自用)
    iNFTnews | 元宇宙的欢乐世界:别开生面的游戏、音乐会、主题公园和电影
    OpenGL调用窗口,方向键和鼠标
    【漏洞通告】CVE-2022-31690 Spring Security Oauth2 Client权限提升漏洞
    gitlab安装
    C++PrimerPlus 第六章 分支语句和逻辑运算符(编程练习含答案)
    万字+28张图带你探秘小而美的规则引擎框架LiteFlow
    set和map的封装
    css案例:取消组件的阴影
    使用单调队列解决 “滑动窗口最大值” 问题
  • 原文地址:https://blog.csdn.net/weixin_51722520/article/details/134210303