When an Ajax request via jQuery, you do not pass the parameter responsible for the page number. OK. But when you have Ajax returns the result in the form of HTML code that you insert into the body of the page, then when you click on the link or what you have there, you have to re-call the same AJAX method, but already transfer to it the number of the page you clicked on the paginator.
Below is one of the options. There are a lot of options for solving this problem, I cite one of the options:
IMPORTANT! I threw "on my knee", did not check the code, but the principle showed, I hope it is clear)
// Π’Π²ΠΎΠΉ ΠΏΠΎΠ΄Ρ
ΠΎΠ΄ ΠΏΡΠΈ ΠΏΠ΅ΡΠ²ΠΈΡΠ½ΠΎΠΉ ΠΎΡΠΏΡΠ°Π²ΠΊΠ΅ Π½Π΅ Π±ΡΠ΄Ρ ΡΡΠΎΠ³Π°ΡΡ $('#id_card').on('keyup', function() { // ΠΏΠΎΠΈΡΠΊ var value = $(this).val(); $.ajax({ type: 'POST', url: '/home', data: { search: value, code: 1, _token: '{{csrf_token()}}' }, success: function (data) { var tab= $('#table_list').find('tbody'); tab.empty(); tab.append(data.body); // ΠΡΡΠ°Π²Π»ΡΠ΅ΡΡ ΡΠΎΠ΄Π΅ΡΠΆΠΈΠΌΠΎΠ΅ ΠΏΠ°Π³ΠΈΠ½Π°ΡΠΈΠΈ Π² Π±Π»ΠΎΠΊ $('#paginator').html(data.paginator); }, error: function(data) { console.log(data); } }) }); // ΠΠ°Π³ΠΈΠ½Π°ΡΠΈΡ $(document).on('click', '[data-type="paginator"]', function() { // ΠΏΠΎΠΈΡΠΊ // ΠΠ΅ΠΎΠ±Ρ
ΠΎΠ΄ΠΈΠΌΠΎ ΡΠΊΠ°Π·Π°ΡΡ Ρ ΠΊΠ°ΠΊΠΎΠ³ΠΎ ΠΎΠ±ΡΠ΅ΠΊΡΠ° Π±ΡΠ΄Π΅Ρ Π±ΡΠ°ΡΡ ΡΡΡΠΎΠΊΡ Π΄Π»Ρ ΠΏΠΎΠΈΡΠΊΠ° var value = $(this).val(); $.ajax({ type: 'POST', url: '/home', data: { search: value, code: 1, _token: '{{csrf_token()}}', page: $(this).attr('data-page') }, success: function (data) { var tab= $('#table_list').find('tbody'); tab.empty(); tab.append(data.body); // ΠΡΡΠ°Π²Π»ΡΠ΅ΡΡ ΡΠΎΠ΄Π΅ΡΠΆΠΈΠΌΠΎΠ΅ ΠΏΠ°Π³ΠΈΠ½Π°ΡΠΈΠΈ Π² Π±Π»ΠΎΠΊ $('#paginator').html(data.paginator); }, error: function(data) { console.log(data); } }) });
Controller code
public function search(Request $request) { $models = Model::where('field', 'LIKE', '%' . $request->input('search') . '%')->paginate(4); $str = ''; foreach($models as $model) { $str .= ' <tr> <td>' . $model->id . '</td> <td> two</td> <td> three</td> <td> four</td> <td> five</td> <td> six</td> </tr>'; } $paginator = ''; for($i = 1; $i <= $models->lastPage(); $i++) { $paginator .= '<a href="javascript:;" data-type="paginator" data-page="' . $i . '">' . $i . '</a>'; } echo json_encode(['body' => $str, 'paginator' => $paginator]); // ΠΡΠ°Π²ΠΈΠ»ΡΠ½Π΅Π΅ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°ΡΡ ΡΠ³Π΅Π½Π΅ΡΠΈΡΠΎΠ²Π°Π½Π½ΡΠΉ ΡΠ΅ΠΊΡΡ ΠΈΠ· ΡΡΠ½ΠΊΡΠΈΠΈ, Π° Π½Π΅ Π² Π½Π΅ΠΉ Π΄Π΅Π»Π°ΡΡ Π²ΡΠ²ΠΎΠ΄ ΡΠ΅ΠΊΡΡΠ° return; }
Add block to HTML template
<div id="paginator"></div>