使用带有PHP的Ajax加快select2的速度 [英] speed up select2 using ajax with php

查看:85
本文介绍了使用带有PHP的Ajax加快select2的速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于数据库中有超过六千个客户,因此我试图加快select2结果的速度.我的输入框充满了来自mysql数据库的数据,并且不知道我可以在现阶段尝试做些什么.这是我的select2 javascript(Js不是我的东西)

I am trying to speed up my select2 results as i have over six-thousand customers in the database. I have the input box filled with data from a mysql database and dont know what more I can try at this stage to be honest. here is my select2 javascript (Js just isnt my thing)

   <script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.min.js"></script>
  <script type="text/javascript">
  $(function(){
  // turn the element to select2 select style
$(".js-data-example-ajax").select2(
  {
     ajax: {
        url: "/delete.php",
        dataType: 'json',
        data : function(term){
          return{
            term: term
          };
        },
      results: function(data){
        var results = <?php echo json_encode($jsData) ?>;
        $.each(data, function(index, item){
          results.push({
            id: item.id,
            text: item.fullName
          })
        })
      }
    }

  });//end select2
});//end function

在php中,我使用一个foreach循环读取数据.如果没有ajax,效果很好,只是选择结果的速度很慢,而且每天都会有更多的客户添加.我尝试发回ID和客户的全名.

in php I use a foreach loop to read the data. It works perfect without the ajax just can be very slow select a result and there will be more customers added every day. I try to send back the id and the customers full name.

<?php
    $sqlSearch="SELECT first_name, last_name, id, mobile, landline FROM customer order by first_name"; 
      echo "<select id='tbCustId' class='js-data-example-ajax' style='width: 150px' size='2' name='tbCustId' required></option>";
     // echo "<select>";
      $jsData = [];

      foreach ($db->query($sqlSearch) as $row){
          $id = $row["Id"];
          $fName = $row["first_name"];
          $sName = $row["last_name"];
          $fullName = $fName ." ". $sName;

      $jsData[] = [
      "id" => $id;
      "fullName" => $fullName;
      ];


      echo "<option value=$row[id]>
      $fullName
      </option>";     
      }
      echo "</select><br/>";// Closing of list box
      ?>

推荐答案

由于记录太多,需要花费太多时间,因此避免提示是

As there are too many records it taking too much time , to avoid suggestion is

  1. 使用分页或

  1. Make use of Pagination or

通过触发多个ajax请求来使用并行执行,即,一个请求获取1到3000个数据,并行请求获取3001到6000个数据.

Make use of parallel execution by firing multiple ajax request i.e. request one fetch 1 to 3000 data and parallel request fetch 3001 to 6000 data..

示例代码:可能存在语法错误

Example Code : might having syntax error

ajax: {
        url: "/delete.php",
        dataType: 'json',
        data : function(term){
          return{
            term: term
            //record no : 1 added parameter for paging
            //record no : 3000
          };
        },
      results: PushData
    }


ajax: {
        url: "/delete.php",
        dataType: 'json',
        data : function(term){
          return{
            term: term
            //record no : 3001 added parameter for paging
            //record no : 6000
          };
        },
      results: PushData
    }



function PushData(data){
        var results = <?php echo json_encode($jsData) ?>;
        $.each(data, function(index, item){
          results.push({
            id: item.id,
            text: item.fullName
          })
        })
      }

这篇关于使用带有PHP的Ajax加快select2的速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆