Ant Design Vue AutoComplete 通过Ajax返回服务器端数据例子

Ant Design Vue AutoComplete 通过Ajax返回服务器端数据例子,通过我们需要根据业务对输入框输入的关键词返回对应的数据,本文将显示如何实现这个功能。

node.js webpack方式

<template>
  <a-auto-complete
    :dataSource="ds"
    style="width: 260px"
    @select="onSelect"
    @search="handleSearch"
    placeholder="input here"
  />
</template>
<script>
  export default {
    data() {      return {
        ds: [], //数据源
      };
    },
    methods: {      
     //搜索事件
      handleSearch(value) {    
        var that=this;
        var url = '/package/ant-design-vue/data.json?keyword='+value
         $.ajax({
            url:url,  
            dataType:"JSON", //返回格式为json
            async:false , 
            data:{"id":"value"}, 
            type:"GET", 
            success:function(res){
             //请求成功时处理
             //var result=$.parseJSON(res);
              res.ds.push("这是服务器端返回的数据");
             that.ds=res.ds;
            }, 
            error:function(){
                //请求出错处理
               console.log('请求出错处理');
            }
        });
      },      
       //选择下拉框事件
      onSelect(value) {
        console.log('onSelect', value);
      },
    },
  };
</script>

浏览器方式

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Ant Design Vue AutoComplete ajax搜索例子</title>
    <script src="https://www.itxst.com/package/vue/vue.min.js"></script>
   <script src="https://www.itxst.com/package/bootstrap-table-1.14.1/jquery-3.3.1/jquery.js"></script>
    <script src="https://www.itxst.com/package/ant-design-vue/antd.min.js"></script>
    <link href="https://www.itxst.com/package/ant-design-vue/antd.min.css" rel="stylesheet" />
    <style>
        body {
            padding-top: 10px
        } 
        .ant-pagination-item-link.red {
            color: red;
            padding-left: 6px;
            padding-right: 6px;
        }
    </style>
</head>
<body>
<div id="app">
   <a-auto-complete
    :data-source="ds"
    style="width: 260px"
    @select="onSelect"
    @search="search"
    placeholder="input here"
  ></a-auto-complete>
</div>
<script>
var app = new Vue({
    el: '#app',
     data() {
      return {
        ds: ['这是前端数据','22','33'],
      };
    },
    methods: {
      search(value) {
        var that=this;
        var url = '/package/ant-design-vue/data.json?keyword='+value
         $.ajax({
            url:url,  
            dataType:"JSON", //返回格式为json
            async:false , 
            data:{"id":"value"}, 
            type:"GET", 
            success:function(res){
             //请求成功时处理
             //var result=$.parseJSON(res);
              res.ds.push("这是服务器端返回的数据");
             that.ds=res.ds;
            }, 
            error:function(){
                //请求出错处理
               console.log('请求出错处理');
            }
        });
      },
      onSelect(value) {
       //选择的数据
        console.log('onSelect', value);
      } 
    },
});
 </script>
</body>
</html>

例子