简体   繁体   English

在 Laravel 中加载 ajax 分页

[英]Page loading on ajax pagination in Laravel

I am trying to implement ajax pagination.我正在尝试实现 ajax 分页。 but ajax pagination is loading perfectly when I am clicking on the second page button.但是当我单击第二页按钮时,ajax 分页正在完美加载。 but when I am clicking on the third-page button it's reloading the whole page.但是当我点击第三页按钮时,它正在重新加载整个页面。

Here is the search function这是搜索功能

public function search(Request $request){
$query = $request->get('query');
if($request->ajax()){   
            $data = Constant_model::get_icons('fontawesomeicons',"id",'DESC',10,$query);
                    }
    return view('icons_table_data', compact('data'))->render();


    }

}

and the javascript code is和 javascript 代码是

function load_data(query,page) {
  $.ajax({
    url: "/icons/search?page="+page,
    method: "GET",
    data: {
      search: query
    },
    error: function (error) {

        console.log(error);

    },
    success: function(data) {


        $('#tabledata').empty();
        $('#tabledata').append(data);
    }
  });
}


$(document).ready(function() {

$('#search').keyup(function() {
  var search = $(this).val();
  var page = $('#hidden_page').val();
  if (search != '') {

   load_data(search,page);

  } else {

  }
});
});

 $( ".pagination a" ).bind( "click", function(e) {
  event.preventDefault();
    var page = $(this).attr('href').split('page=')[1];
    $('#hidden_page').val(page);

    var query = $('#serach').val();

    $('li').removeClass('active');
    $(this).parent().addClass('active');
    load_data(query,page);

}); });

You are using e as the arguments in the function(e) , not function(event) .您使用e作为function(e)的参数,而不是function(event)

Make sure to use the same name.确保使用相同的名称。 So, you can either change it to:因此,您可以将其更改为:

$( ".pagination a" ).bind( "click", function(event) {
    event.preventDefault();
    // code...
});

or或者

$( ".pagination a" ).bind( "click", function(e) {
    e.preventDefault();
    // code...
});

I think you include the pagination link in the view 'icons_table_data' right?我认为您在视图“icons_table_data”中包含了分页链接,对吗? If so, you should write the event listen like this: $('body').on('click','.pagination a',function(e){...}) because it will not read the event on new created element if you write in your code.如果是这样,你应该像这样编写事件监听: $('body').on('click','.pagination a',function(e){...})因为它不会在 new 上读取事件如果您在代码中编写,则创建元素。 Also, change event.preventDefault();另外,更改event.preventDefault(); into e.preventDefault();进入e.preventDefault(); because you pass the variable name is e因为你传递的变量名是e

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM