从fnrowcallback数据表调用ajax函数

时间:2019-05-20 10:14:28

标签: javascript jquery ajax datatables

我正在尝试从fnRowCallback调用一个从AJAX请求返回数据的函数。在console.log上可以访问数据,但是在return上返回undefined。这就是我正在尝试的功能,我正在尝试从getUsersCount调用的函数fnRowCallback

$(document).ready(function() {
  $('#match-request-list').DataTable({
    "lengthMenu": [25, 50, 100],
    "pageLength": 50,
    "searching": false,
    "bProcessing": true,
    "ordering": false,
    "serverSide": true,
    "responsive": true,
    "stateSave": true,
    "stateDuration": -1,
    "dom": '<"top"f>rt<"bottom"ilp><"clear">',
    "ajax": {
      url: "{{ url('automatch/resultset') }}",
      type: "post",
      data: {
        "id": $('#req_id').val()
      }
    },
    error: function() {},
    "initComplete": function(settings, json) {},
    "fnDrawCallback": function(settings) {
      $('[data-toggle="tooltip1"]').tooltip({
        content: function() {
          return $('#tooltipContent').html();
        }
      });
    },
    fnRowCallback: function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
      $(nRow).attr({
        size: aData[7],
        category: aData[8],
        category_group: aData[9]
      }); //add id to tr rel attribute
      var count = getUsersCount(aData[10], aData[9], aData[8]);
      // console.log(count);
      $('td:eq(5)', nRow).html('<p style="text-align:center;"' + count + '</p');

    },
    columns: [null, null, null, null, null, null, null, {
      visible: false
    }, {
      visible: false
    }, {
      visible: false
    }, {
      visible: false
    }]
  });
});

function getUsersCount(reqId, catGrId, catId) {
  getUsersCountAjax(reqId, catGrId, catId).complete(function(data) {
    return data.count;
    // console.log(data.count);                
  });
}

function getUsersCountAjax(reqId, catGrId, catId) {
  return $.ajax({
    data: {
      reqId: reqId,
      catGrId: catGrId,
      catId: catId
    },
    url: "{{ url('/assigned-users/count') }}",
    type: 'POST',
    async: false,
    dataType: 'json'
  });
}

0 个答案:

没有答案