Blogger Information
Blog 28
fans 0
comment 0
visits 65495
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
element ui级联选择器 通过axios获取数据源
蒸蒸
Original
816 people have browsed it

前端框架vue,后端框架tp5,使用element ui的级联选择器,实现始发地和目的地检索功能,效果如下:

一、vue模板语法复习:
v-bind用来定义vue模板元素的属性,一般使用它的缩写方式:

  1. <!-- 完整语法 -->
  2. <a v-bind:href="url"></a>
  3. <!-- 缩写 -->
  4. <a :href="url"></a>

二、对options数据源与后端数据的关系的理解,以官方实例hover触发子菜单为例:

  1. <el-cascader
  2. v-model="value"
  3. :options="options"
  4. :props="{ expandTrigger: 'hover' }"
  5. @change="handleChange">
  6. </el-cascader>

1.代码中的:options和:props分别定义了cascader的options和props属性,element-ui官方文档中有对这些属性的说明:options定义可选项的数据源,props定义一些配置项。

2.options数据源中,默认value参数,对应后端数据的’value’字段;label参数,对应’label’字段’;同理,children和leaf参数分别对应’children’和’leaf’字段。

3.这种对应关系,可以通过修改props配置项进行修改;
修改方法①,直接在模板中修改:

  1. //修改label参数对应后端数据的zone字段,value参数对应address字段
  2. <el-cascader
  3. v-model="value"
  4. :options="options"
  5. :props="{ label: 'zone',value: 'address' }"
  6. @change="handleChange">
  7. </el-cascader>

修改方法②,在vue模型中修改:

  1. <el-cascader :props="destiOptionsr"></el-cascader>
  1. export default {
  2. name: "Truckprice",
  3. data() {
  4. const _self = this;
  5. return {
  6. destiOptionsr:{
  7. label:'zone',
  8. value: 'address',
  9. }
  10. }}}

三、后端数据准备
1.级联选择器的第一级对应数据表FivLcltruckPrice的zone字段,第二级对数据表应FivLcltruckPrice的address字段

  1. public function gettrucknetworkt()
  2. {
  3. $data = [];
  4. //获取所有不同的zone字段的值
  5. $zone = FivLcltruckPrice::distinct(true)->field('zone')->select();
  6. //对查询结果进行遍历
  7. foreach ($zone as $key => $value) {
  8. //整理数据格式,zone字段的值作为data数据的value和lebel的值
  9. $data[$key]['value'] = $value->zone;
  10. $data[$key]['label'] = $value->zone;
  11. //一共两级,所以第一级所有数据都不是叶子节点,leaf设置为false
  12. $data[$key]['leaf'] = false;
  13. //获取每一个zone对应的address字段的值,作为第一级的children
  14. $address = FivLcltruckPrice::where('zone', $data[$key]['value'])->field('address')->select();
  15. foreach ($address as $keyt => $valuet) {
  16. //整理数据格式,address作为children的value和label
  17. $data[$key]['children'][$keyt]['value'] = $valuet->address;
  18. $data[$key]['children'][$keyt]['label'] = $valuet->address;
  19. //一共两级,所以第二级所有数据都是叶子节点,leaf设置为true
  20. $data[$key]['children'][$keyt]['leaf'] = true;
  21. }
  22. }
  23. //将数组转化为json,并返回到前端
  24. return json($data);
  25. }

四、前端获取数据,并显示出来

  1. <el-cascader :options="originOptions" clearable placeholder="请选择始发地" v-model="value"
  2. :props="{ expandTrigger: 'hover' }" filterable></el-cascader>
  3. <i class="el-icon-right"></i>
  4. <el-cascader :options="destiOptions" clearable placeholder="请选择目的地" v-model="value"
  5. :props="{ expandTrigger: 'hover' }" filterable></el-cascader>
  1. export default {
  2. name: "Truckprice",
  3. data() {
  4. return {
  5. originOptions:[],
  6. destiOptions:[]
  7. }
  8. },
  9. created() {
  10. this.$axios.post(this.$host + 'api/gettrucknetworkt').then(res => {
  11. this.destiOptions=res.data;
  12. this.originOptions=res.data;
  13. })
  14. },
  15. }
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post