Search code examples
phpjqueryajaxlaravelyajra-datatable

Yajra How to parsing ID from html to ajax without using onclick


I have a datatable with more than 1 tab. I want to display my data from post's table into the datatable, but in My post table I have LanguageID column. So, I want to separate the data by LanguageID and that's why I am using tab. But, I got problem when I want to get the LanguageID.

This is my view.. Sorry I can not post the code and using Image because I have @foreach in my code.

and this is my ajax

$(function() {
  var id = 1;
  var oTable = $("#data-post" + id).DataTable({
    processing: true,
    serverSide: true,
    ajax: {
      url: "{{ url("
      post ") }}",
      data: function(d) {
        d.LanguageID = id;
      }
    },
    columns: [{
        data: 'PostDate',
        name: 'PostDate'
      },
      {
        data: 'PostTitle',
        name: 'PostTitle'
      },
      {
        data: 'PostSlug',
        name: 'PostTitle'
      },
      {
        data: 'action',
        name: 'action'
      }
    ]
  });

  $('#tabPost' + id).on('click', function(e) {
    oTable.draw();
    e.preventDefault();
  });

});

I want to parse {{$language->id}} from html into my ajax.. but I don't know how to do that without using function onclick


Solution

  • Problem why I can't get the id, that is because I only get the first id.. So, I decide to use each to get all the id

    $('.tabPost').each(function(){
    var lang_id = $(this).attr('id');
    var oTable = $("#data-post" + lang_id).DataTable({
      processing: true,
      serverSide: true,
      ajax:{
        url: "{{ url("post") }}",
        data: function(d) {
          d.LanguageID = lang_id
        }
      },
      columns: [
          { data: 'PostDate', name: 'PostDate'},
          { data: 'PostTitle', name: 'PostTitle' },
          { data: 'PostSlug', name: 'PostTitle' },
          { data: 'action', name: 'action'}
      ]
    });
    
    $('#'+lang_id).on('click', function(e){
      oTable.draw();
      e.preventDefault();
    });
    

    });