当前位置:   article > 正文

vue+elementui+springboot 传参以及从后台请求数据(table控件为例子)_vue springboot 后端数据显示到tabledata

vue springboot 后端数据显示到tabledata

1.js发出请求

  mounted() {
      this.$store.dispatch('showHeader');
      this.loadData();
    },
 methods: {
 loadData() {
    var params = {
          page: this.currentPage, 
          pageSize: this.pageSize, 
          name: this.formInline.name, 
          address: this.formInline.address
        };
        getUserList(params).then(function(result){
          this.tableData = result.data.list;
          this.total = result.data.total;
          this.loading2 = false;
        }.bind(this)).catch(function (error) {
            this.loading2 = false;
            console.log(error);
        }.bind(this));
      }
        }
跳转
   export const getUserList = params => { return Axios.get(`${BASE_PATH}/user/list`, { params: params }).then(res => res.data); };
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

2.后台服务

@RestController
@RequestMapping("/user")
public class UserController {

@GetMapping("/list")
	public ResponseData queryUsers(String name, String address, Integer page, Integer pageSize){
		if (page == null) {
			page = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		List<UserVo> users = new ArrayList<UserVo>();
		if (page == 1) {
			for (int i = 1; i < 11; i++) {
				users.add(new UserVo(i, "王小虎" + i, "上海市普陀区金沙江路 1518 弄", "2016-05-02"));
			}	
		}
		if (page == 2) {
			for (int i = 11; i < 20; i++) {
				users.add(new UserVo(i, "王小虎" + i, "上海市普陀区金沙江路 1518 弄", "2016-05-02"));
			}	
		}
		if (!address.equals("")) {
			users = new ArrayList<UserVo>();
			users.add(new UserVo(111, "王小虎111", "上海市普陀区金沙江路 1518 弄", "2016-05-02"));
		}
		Map<String, Object> result = new HashMap ();
		result.put("list", users);
		result.put("currentPage", page);
		result.put("total", 19);
		result.put("pageSize", pageSize);
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ResponseData.ok(result);
	}
	
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

3.返回数据显示js

export default {
    data() {
      return {
        tableData: [],
        formInline: {
          name: '',
          address: '',
          id:''
        },
        currentPage:1,
        total:0,
        pageSize:10,
        addressList:["上海市普陀区金沙江路 1518 弄", "上海市普陀区金沙江路 1519 弄", "上海市普陀区金沙江路 1517 弄"],
        pickerOptions0: {
            disabledDate(time) {
              return time.getTime() < Date.now() - 8.64e7;
            }
        },
        selectDate:'',
        dialogFormVisible: false,
        formLabelWidth: '120px',
        form: {
            name: '',
            address: '',
            id:''
        },
        loading2: false,
        options: [{
          value: '湖南',
          label: '湖南',
          children: [{
            value: '长沙',
            label: '长沙'
          },
          {
            value: '株洲',
            label: '株洲'
          },
          {
            value: '湘潭',
            label: '湘潭'
          }]
        }],
        selectedOptions: [],
        multipleSelection: []
      }
    },
```methods: {
    
        getUserList(params).then(function(result){
        //赋值
          this.tableData = result.data.list;
          this.total = result.data.total;
          this.loading2 = false;
        }.bind(this)).catch(function (error) {
            this.loading2 = false;
            console.log(error);
        }.bind(this));
      }

 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

4控件绑定数据

  <el-form-item label="城市">
    <el-cascader :options="options" v-model="selectedOptions"></el-cascader>
  </el-form-item>

  <el-form-item label="姓名">
    <el-input v-model="formInline.name" placeholder="姓名"></el-input>
  </el-form-item>
        
  <el-form-item label="地址">
    <el-select v-model="formInline.address" placeholder="地址">
      <el-option v-for="item in addressList" :key="item" :label="item" :value="item"></el-option>
    </el-select>
  </el-form-item>
        
  <el-form-item>
    <el-date-picker v-model="selectDate" type="date" placeholder="选择日期" :picker-options="pickerOptions0">
    </el-date-picker>
  </el-form-item>
        
  <el-form-item>
    <el-button type="primary" @click="onSubmit">查询</el-button>
  </el-form-item>
    
</el-form>
    //绑定tableData
<el-table :data="tableData" style="width: 100%" v-loading="loading2" element-loading-text="拼命加载中" @selection-change="handleSelectionChange">
  <el-table-column type="selection" width="55"></el-table-column>
  <el-table-column prop="name" label="姓名" width="180"></el-table-column> 
  <el-table-column prop="address" label="地址"></el-table-column> 
  <el-table-column prop="date" label="日期" width="180"></el-table-column>
  <el-table-column fixed="right" label="操作" width="140">
    <template scope="scope">
      <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
      <el-button type="text" size="small"  @click="showEditDialog(scope.$index)">编辑</el-button>
      <el-button type="text" size="small"  @click="removeData(scope.$index)">删除</el-button>
    </template>
  </el-table-column>
</el-table> 
  
<div class="block">
    <div class="r_btn">
      <el-button type="primary" @click="batchRemove">批量删除</el-button>
    </div>
    <div class="r_page">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
</div>
  
<!-- Form -->
<el-dialog title="编辑信息" :visible.sync="dialogFormVisible">
  <el-form :model="form">
    <el-form-item label="姓名" :label-width="formLabelWidth">
      <el-input v-model="form.name" auto-complete="off"></el-input>
    </el-form-item>
     <el-form-item label="地址" :label-width="formLabelWidth">
      <el-input v-model="form.address" auto-complete="off"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="update">确 定</el-button>
  </div>
</el-dialog>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
```
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/227649
推荐阅读
相关标签
  

闽ICP备14008679号