select2 的分页搜索加载的简单实例

select2 的分页搜索加载的简单实例


<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Select2 分页加载</title>

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/select2/4.0.9/js/select2.min.js"></script>
    <link href="https://cdn.bootcdn.net/ajax/libs/select2/4.0.9/css/select2.min.css" rel="stylesheet">

    
    
</head>
<style>
	/*.input-text{
		width:85%;
		height:200px ;
		line-height:200px ;
		margin: 0; 
		padding: 0;
		padding-left:5px;
		border-width:1px;
	}*/
    
    /*change dropdowm offset*/
	/*.select2-dropdown {
	    margin-left: 8px !important;
	    margin-top: 20px !important;
	}*/

    /*change box width height*/
    .select2-results__options {
       max-height: 600px;
    }
    /*change click selection height*/
    .select2-selection__rendered {
        line-height: 50px !important;
    }
    .select2-container .select2-selection--single {
        height: 50px !important;
    }
    .select2-selection__arrow {
        height: 50px !important;
    }

   /* .select2-choices {
      min-height: 300px;
      max-height: 300px;
      overflow-y: auto;
    }*/
    /*change search inpu height*/
    .select2-search input {
      height: 50px !important;
    }



</style>
<body>
	<div style="width: 85%;">
		<h1>Select2 分页加载</h1>
	    <div class="s1-example">
	       	<select id="selectDemo" style="width:100%;height: 50px;" class="select2-dropdown" name="selectDemo" ></select>
	    </div>
    
	</div>
   

    <script type="text/javascript">
  	$(document).ready(function() {
        var data_count = 10;
    	$("#selectDemo").select2({
    		ajax: {
     		url: "https://api.apiopen.top/getWangYiNews",
    	    dataType: 'json',
    	    delay: 250,
    	    data: function (params) {
    	        var query = {
    	    		search: params.term,
    	            page: params.page || 1,
    	            count: data_count
    	        }
    	   		return query;
    	    },
    	    processResults: function (data,params) {
    	    params.page = params.page || 1;
            console.log(data);
    		var array = data.result;
    		var i = 0;
    	    while(i < array.length){
    	        array[i]["id"] = array[i]['title'];
    	        array[i]["text"] = array[i]['title'];
    	        delete array[i]["title"];
    	        // delete array[i]["label"];
    	  		i++;
    	    }
            console.log(data_count,data.result.length);
   	         return {
   	             results: array,
	   	         pagination : {
                        //判断数据是否加载完成
	                  more : data_count <= data.result.length
	             }
   	         };


    	    },
    	    cache: true
    	  },
    	  placeholder: '请输入关键字',
    	  escapeMarkup: function (markup) {return markup; },
          //最小的搜索字数
    	  minimumInputLength: 0,
		  // templateResult : formatPromCode,
    	  formatSelection: formatSelect
    	})



    // 自适应
    remChange();
    window.addEventListener('resize', remChange);
    function remChange() {
        remove();
        let width = window.screen.width;
        let fixedw = 750;
        let scale = width / fixedw; //获取到的屏幕宽度比上自定义的750宽度 获得对应比例
        let meta = document.createElement("meta");
        meta.setAttribute('name', 'viewport');
        //将对应比例填入meta标签即可实现宽度自适应
        meta.setAttribute('content', `width=device-width, initial-scale=${scale}, maximum-scale=${scale}, minimum-scale=${scale}, user-scalable=no`);
        //meta将标签添加到文档中即可
        document.head.appendChild(meta);
    }
    function remove() {
        let meta = document.querySelector("meta[name='viewport']");
        if (meta != null) {
            document.head.removeChild(meta);
        }
    }





















    });

    function format(results){	
    	if (results.loading) {
    		return results.text;
    	}
    	if(results.id){
    		return '<option value="'+ results.id +'">' + results.text + '</option>';
    	}	
    }
    function formatSelect(results){
    	  return results.id || results.text;
    }

    












   
    </script>
</body>
 
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个基于ajax实现分页查询的例子: HTML文件: ``` <!DOCTYPE html> <html> <head> <title>Ajax Pagination Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> $(document).ready(function(){ load_data(); function load_data(page){ $.ajax({ url: "fetch.php", method: "POST", data: {page:page}, success: function(data){ $('#pagination_data').html(data); } }) } $(document).on('click', '.pagination_link', function(){ var page = $(this).attr("id"); load_data(page); }); }); </script> </head> <body> <div id="pagination_data"></div> </body> </html> ``` fetch.php文件: ``` <?php //数据库连接信息 $host = "localhost"; $user = "username"; $password = "password"; $dbname = "database_name"; //创建连接 $conn = mysqli_connect($host, $user, $password, $dbname); //如果连接失败则输出错误信息 if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } //设置每页显示的记录数 $record_per_page = 10; //获取当前页码值 if(isset($_POST["page"])){ $page = $_POST["page"]; } else { $page = 1; } //计算起始记录 $start_from = ($page-1) * $record_per_page; //执行查询语句 $query = "SELECT * FROM table_name ORDER BY id DESC LIMIT $start_from, $record_per_page"; $result = mysqli_query($conn, $query); //输出查询结果 if(mysqli_num_rows($result) > 0){ while($row = mysqli_fetch_assoc($result)){ echo "<div>".$row["name"]."</div>"; } } //计算总页数 $query = "SELECT COUNT(*) as total_records FROM table_name"; $result = mysqli_query($conn, $query); $row = mysqli_fetch_assoc($result); $total_records = $row["total_records"]; $total_pages = ceil($total_records / $record_per_page); //输出分页链接 if($total_pages > 1){ for($i=1; $i<=$total_pages; $i++){ echo "<span class='pagination_link' style='cursor:pointer; padding:6px; border:1px solid #ccc;' id='".$i."'>".$i."</span>"; } } //关闭连接 mysqli_close($conn); ?> ``` 在以上示例中,我们通过Ajax从服务器加载数据并将其分页显示。可以在fetch.php中更改查询语句和分页设置以适应不同的数据和页面布局需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WMSmile

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值