why my datatable dosent work when I have huge data? - laravel

I am using a datatable in my laravel project for huge data. It's about 78000 rows and 30 columns.
I get this error :
DataTables warning: table id=dt-basic-example - Ajax error. For more information about this error, please see http://datatables.net/tn/7
When I insert smaller data everything works good and no error appears.
I went to this url : enter link description here
I do every step but it return this : This request has no response data available
What can I do?
My controller :
$ProducerCompanies = \App\ProducerCompanies::all()->where('Activation',$company);
return DataTables::of($ProducerCompanies)
->addColumn('id', function ($ProducerCompanies) {
if ($ProducerCompanies->Activation == '0') {
$data = "bg-danger" . '/' . $ProducerCompanies->id;
} elseif ($ProducerCompanies->Activation == '1') {
$data = "bg-success" . '/' . $ProducerCompanies->id;
}
return $data;
})
->addColumn('IdCategory', function ($ProducerCompanies) {
$id = $ProducerCompanies->category->name;
return $id;
})
->addColumn('action', function ($ProducerCompanies) {
$id = $ProducerCompanies->id;
return $id;
})
->addColumn('country', function ($ProducerCompanies) {
if ($ProducerCompanies->industrialname != '') {
$id = $ProducerCompanies->countryname->name . '/' . $ProducerCompanies->statename->name . '/'
. $ProducerCompanies->cityname->name . '/' . $ProducerCompanies->industrialname->name;
} else {
$id = $ProducerCompanies->countryname->name . '/' . $ProducerCompanies->statename->name . '/'
. $ProducerCompanies->cityname->name;
}
return $id;
})
->addColumn('Info', function ($ProducerCompanies) {
$data = $ProducerCompanies->Information;
return $data;
})
->addColumn('product', function ($ProducerCompanies) {
if ($ProducerCompanies->leaders->count() != 0) {
foreach ($ProducerCompanies->leaders as $row) {
if ($row->where('Activation', '1')->count() === $row->count()) {
$data2 = 'bg-success';
} else if ($row->where('Activation', '1')->count() >= 0) {
$data2 = 'bg-danger';
}
}
} else {
$data2 = 'bg-danger';
}
if ($ProducerCompanies->certificate->count() != 0) {
foreach ($ProducerCompanies->certificate as $row2) {
if ($row2->where('Activation', '1')->count() === $row2->count()) {
$data3 = 'bg-success';
} else if ($row2->where('Activation', '1')->count() >= 0) {
$data3 = 'bg-danger';
}
}
} else {
$data3 = 'bg-danger';
}
if ($ProducerCompanies->product->count() != 0) {
foreach ($ProducerCompanies->product as $row3) {
if ($row3->where('Activation', '1')->count() === $row3->count()) {
$data4 = 'bg-success';
} else if ($row3->where('Activation', '1')->count() >= 0) {
$data4 = 'bg-danger';
}
}
} else {
$data4 = 'bg-danger';
}
$data = $ProducerCompanies->id . '/' . $data2 . '/' . $data3 . '/' . $data4;
return $data;
})
->make(true);
My script in HTML page :
$(document).ready(function () {
$("#dt-basic-example").dataTable({
serverSide: true,
processing: true,
ajax: {
url: '/ProducerCompanies/ajax/{{ $company }}',
data: function (data) {
data.params = {
sac: "helo"
}
}
},
scrollY: 500,
srollX: true,
ScrollCollapse: true,
columns: [
{data: "id",className: 'id'},
{
"data": "Logo40",
name: "Logo40",
render: function (data) {
return "<div style='width: 50px;height: 50px'><img class='w-100 h-100' src='/" +
data + "'></div>"
},
orderable: false,
searchable: false
},
{"data": "Name"},
{"data": "CompanyId", searchable: false},
{
"data": "IdCategory",
name: "IdCategory",
render: function (data) {
return "<div>" + data + "</div>"
}
, searchable: false
},
{"data": "CreateTime", searchable: false},
{"data": "created_at", searchable: false},
{
"data": "action",
name: "action",
render: function (data) {
return "<div class='d-flex justify-content-center'>" +
"<div style='width: 50px;height: 35px'>" +
"<a class=\"btn btn-primary d-inline-block w-100 h-100\"\n" +
"href=\"/ProducerCompanies/index/" + data + "/edit\">" +
" <i class=\"fal fa-edit text-white\"></i></a></div>\n" +
"<div style='width: 50px;height: 35px'>" +
"<button class=\"btn btn-danger btn-sm text-white w-100 h-100 destroy ml-2\"" +
" value='" + data + "'>\n" +
"<i class=\"fal fa-trash-alt\"></i>\n" +
"</button></div></div>\n"
}, searchable: false
},
{"data": "CompanyNumber", searchable: false},
{"data": "FactoryNumber", searchable: false},
{"data": "FaxNumber", searchable: false},
{"data": "CompanyAddress", searchable: false},
{"data": "FactoryAddress", searchable: false},
{"data": "BackgroundImg", searchable: false},
{"data": "InformationImg", searchable: false},
{"data": "CompanyEmail", searchable: false},
{"data": "CompanyWebsite", searchable: false},
{
"data": "country",
name: "country",
render: function (data) {
return "<div><img class='mr-2' style='width: 25px;' src='/images/iran-flag.jpg'>" +
data + "</div>"
}
, searchable: false
},
{
"data": "Info",
name: "Info",
render: function (data) {
return "<div style='max-width: 850px''>" + data + "</div>"
}
},
{
"data": "product",
name: "product",
render: function (data) {
$data = data.split('/');
return "<a href='/product/" + $data[0] + "' " +
"class='btn btn-outline-primary float-right d-flex align-baseline'" +
" style='margin-top: 90px'>" +
"<i class='d-inline-block " + $data[3] + " rounded-circle mr-2 mt-1'" +
" style='width: 15px;height: 15px'></i>" +
"محصولات این شرکت</a>" +
"<a href='/company-leaders/" + $data[0] + "' " +
"class='btn btn-outline-primary float-right mr-2 d-flex align-baseline'" +
" style='margin-top: 90px'>" +
"<i class='d-inline-block " + $data[1] + " rounded-circle mr-2 mt-1'" +
" style='width: 15px;height: 15px'></i>" +
"مدیران این شرکت" +
"</a>" +
"<a href='/company-Certificate/" + $data[0] + "' " +
"class='btn btn-outline-primary float-right mt-2 d-flex align-baseline'>" +
"<i class='d-inline-block " + $data[2] + " rounded-circle mr-2 mt-1'" +
" style='width: 15px;height: 15px'></i>" +
"گواهی نامه های این شرکت</a>"
}
}
]
});
});

Related

Select2 Add New Tag if no result returned in search

I have dropdown list where , after typing 2 or more symbols it starts to search in base nad returns values . if none is searched i want user to be able to add that tag as new one. here is my code so far
$("#tags,#TagId").select2(
{
allowClear: true,
ajax: {
url: "/Entry/ReadSelect2DataMulti",
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term,
page: params.page || 1
};
},
processResults: function (data, params) {
params.page = params.page || 1;
return {
results: data
};
},
cache: true,
error: function (xhr, textStarus, error) {
}
},
minimumInputLength: 2,
placeholder: "აირჩიეთ პოზიცია",
templateResult: function (state) {
var result = '<div class="pos-template">' + '<span title=""><b>' + state.text + '</b></span>';
if (state.content != null && state.content != '') {
var tags = state.content.split(",");
for (i = 0; i < tags.length; i++) {
result = result + '<span class="pos-tag">' + tags[i] + '</span>';
}
} else {
}
result = result + '</div></div>';
return $(result);
},
createSearchChoice: function (term, data) {
if ($(data).filter(function () {
return this.text.localeCompare(term) === 0;
}).length === 0) {
return { id: term, text: term };
}
},
language: {
noResults: function () {
return "პოზიცია ვერ მოიძებნა";
},
inputTooShort: function () {
return "შეავსეთ მინიმიმ 2 სიმბოლო";
},
loadingMore: function () {
return 'იტვირთება...';
},
searching: function () {
return 'იფილტრება...';
}
}
}
);
But this createSearch doesnt do anything.
<div class="form-group">
<select multiple="multiple" class="form-control" id="tags" style="width: 400px;"></select>
</div>
this is my div part . any suggestions ?

Sweetalert2 : Unable to trigger the validation message

I have created this sequence of popups with Sweetalert2
The user select an year, wait for generation of the report, and at the end can download the report.
This is the code (simplified)
var startYear = 2017;
$("#test").click(function(){
var _id = ....;
var listYears = {};
for (var i = parseInt(moment().format("YYYY")); i >= startYear; i--) listYears[" " + i] = i;
swal({
title: "Data export",
html : "Select a year and press the <strong>export</strong> button.",
reverseButtons : true,
showCancelButton: true,
cancelButtonText : "Cancel",
confirmButtonText: "Export",
validationMessage : "Select a year",
inputClass : "form-control", /* bootstrap 4 class */
input: "select",
inputOptions: listYears,
inputPlaceholder: "Select..",
}).then((result) => {
if (result.value) {
swal({
title: 'Wait',
text: 'Report generation in progress..',
allowOutsideClick : false,
showConfirmButton : false,
onOpen: () => {
swal.showLoading();
var dataGET = ".....&id=" + _id + "&year=" + parseInt(result.value);
var xhr = $.ajax({
type: "GET",
url: ".....php",
data : dataGET,
cache: false,
success : function(val){
var _this = this;
if(val == "OK_DOWNLOAD"){
var pathDownload = xhr.getResponseHeader(".....");
var nameDownload = xhr.getResponseHeader(".....");
swal({
type : "success",
title: 'Perfect',
html : 'Now you can download the report<br/><a class="btn btn-custom-secondary mt-3" href="......" target="_blank" id="tempBtnDownloadReport"><span class="icon-download1"></span></a>',
showConfirmButton : false,
});
$("#tempBtnDownloadReport").click(function(){
swal.close();
});
}else{
_this.error();
}
},
error : function(){
swal("Attention","Error creating report, please try again.","error");
},
complete : function(jqXHR,textStatus){
swal.hideLoading();
xhr = null;
}
});
}
});
}
});
My problem is when the user press the export button and the select it hasn't been "selected". I would like to trigger the error message ("Select a year"), something like these examples.
SOLVED
I used the preConfirm event.
swal({
title: "Data export",
html : "Select a year and press the <strong>export</strong> button.",
reverseButtons : true,
showCancelButton: true,
cancelButtonText : "Cancel",
confirmButtonText: "Export",
validationMessage : "Select a year",
inputClass : "form-control",
input: "select",
inputOptions: listYears,
inputPlaceholder: "Select..",
allowOutsideClick: () => !Swal.isLoading(),
preConfirm: (test) => {
if(test == "") Swal.showValidationMessage("Select a year");
}
}).then((result) => {
if (result.value) {
swal({
title: 'Wait',
text: 'Report generation in progress..',
allowOutsideClick : false,
showConfirmButton : false,
onOpen: () => {
swal.showLoading();
var dataGET = "category=download&cmd=do_excel_report&id=" + _id + "&year=" + parseInt(result.value);
var xhr = $.ajax({
type: "GET",
url: "/" + $("html").data("project") + "/home/command.php",
data : dataGET,
cache: false,
success : function(val){
var _this = this;
if(val == "OK_DOWNLOAD"){
var pathDownload = xhr.getResponseHeader("Custom-Success-Download-Path");
var nameDownload = xhr.getResponseHeader("Custom-Success-Download-Name");
swal({
type : "success",
title: 'Perfect',
html : 'Now you can download the report<br/><a class="btn btn-custom-secondary mt-3" href="/' + $("html").data("project") + "/home/command.php?category=download&cmd=download_excel_report&path=" + pathDownload + "&name=" + nameDownload + '" target="_blank" id="tempBtnDownloadReport"><span class="icon-download1"></span></a>',
showConfirmButton : false,
});
$("#tempBtnDownloadReport").click(function(){
swal.close();
});
}else{
_this.error();
}
},
error : function(){
swal("Attention","Error creating report, please try again.","error");
},
complete : function(jqXHR,textStatus){
swal.hideLoading();
xhr = null;
}
});
}
});
}
});

Datatable button not showing up using jquery

i have a problem with my datatable.
Using require.js buttons are not showing up as I would expect.
I have tried without require.js and it is working fine, using require I have this problem.
Here the code snippet:
UPDATE 15.12.2016:
var jobDataTable = function(){
requirejs.config({
baseUrl: "./assets",
paths: {
jquery: "js/vendor/jquery-1.11.1.min.js",
bootstrap: 'js/bootstrap/js/bootstrap.min',
datatables: 'plugins/dataTables/js/jquery.dataTables.min',
'datatables.bootstrap': 'plugins/dataTables/js/dataTables.bootstrap.min',
buttons: "plugins/dataTables/extensions/buttons/dataTables.buttons.min",
colVis: "plugins/dataTables/extensions/buttons/buttons.colVis.min",
html5: " plugins/dataTables/extensions/buttons/buttons.html5.min",
flash: " plugins/dataTables/extensions/buttons/buttons.flash.min",
print: " plugins/dataTables/extensions/buttons/buttons.print.min",
jsZip: "js/jszip"
},
shim: {
'bootstrap':{ deps:['jquery']},
'datatables':{ deps:['jquery', 'bootstrap']},
'datatables.bootstrap':{ deps:['datatables']},
'buttons': { deps:['datatables']},
'html5': { deps:['datatables', 'buttons']},
'flash': { deps:['datatables', 'buttons']},
'colVis':{ deps:['datatables', 'buttons']},
'print': { deps:['datatables', 'buttons']}
}
});
require(['jquery', 'bootstrap', 'datatables'], function($){
var dataset = app.getJobsByUser();
var jobTable = $('#axCsJobsTable').DataTable({
aaData: dataset,
dom: 'Bfrtip',
buttons: [
'copy', 'csv', 'excel', 'pdf', 'print'
],
aoColumns: [
{ "mDataProp": "JOB_ID" },
{ "mDataProp": "JOB_NAME" },
{ "mDataProp": "JOB_EXPORT_DATE" },
{ "mDataProp": "JOB_EXPORT_FORMAT" },
{ "mDataProp": "JOB_CDATE" },
{ "mDataProp": "JOB_STATUS",
"sClass": "status",
"mRender": function (data, type, row) {
if (parseFloat(data) > 0) {
return "<span class='glyphicon glyphicon-ok-circle'></span>";
} else {
return "<span class='glyphicon glyphicon-time'></span>";
}
}
},
{
"mDataProp": null,
"className": "center",
"defaultContent": "<button id='axCsJobRemove' type='button' class='btn btn-default btn-sm'> <span class='glyphicon glyphicon-trash'></span> Delete </button>"
},
{
"mDataProp": null,
"className": "center",
"defaultContent": "<button id='axCsJobConvert' type='button' class='btn btn-default btn-sm'> <span class='glyphicon glyphicon-save'></span> Convert </button>"
}
]
});
$('#axCsJobsTable tbody').on( 'click', '#axCsJobRemove', function () {
var data = jobTable.row( $(this).parents('tr') ).data();
if (removeJob(data)==1){
jobTable
.row( $(this).parents('tr') )
.remove()
.draw();
}
});
$('#axCsJobsTable tbody').on( 'click', '#axCsJobConvert', function () {
var data = jobTable.row( $(this).parents('tr') ).data();
if(startJob(data)==1){
console.log('Job run successfully');
$('#axCsJobsTable').dataTable().fnDestroy();
setTimeout(function() { jobDataTable(); }, 3000);
}
});
});
}
Some ideas where It hangs?
Missing some dependencies?
Regards,
Patric

Datatables display columns by Index rather than name

The following code works fine for me:
$(document).ready(function () {
var t = $("#users").DataTable({
columnDefs: [{
"searchable": false,
"orderable": false,
"targets": 0
}],
order: [[1, 'asc']],
ajax: {
url: "/api/users",
dataSrc: ""
},
columns: [
{
data: "id"
},
{
data: "firstName"
},
{
data: "lastName"
},
{
data: "userName"
},
{
data: "id",
render: function (data) {
return "<a href='#'><i class='fa fa-eye js-view' data-id='" + data + "'></i></a> | <a href='#'><i class='fa fa-pencil js-edit' data-id='" + data + "'></i></a> | <a href='#'><i class='fa fa-trash js-delete' data-id='" + data + "'></i></a>";
}
}
],
Is there a way to use column indexes instead of names like this?:
$(document).ready(function () {
var t = $("#users").DataTable({
columnDefs: [{
"searchable": false,
"orderable": false,
"targets": 0
}],
order: [[1, 'asc']],
ajax: {
url: "/api/users",
dataSrc: ""
},
columns: [
{
data: 0
},
{
data: 1
},
{
data: 2
},
{
data: 3
},
{
data: 0,
render: function (data) {
return "<a href='#'><i class='fa fa-eye js-view' data-id='" + data + "'></i></a> | <a href='#'><i class='fa fa-pencil js-edit' data-id='" + data + "'></i></a> | <a href='#'><i class='fa fa-trash js-delete' data-id='" + data + "'></i></a>";
}
}
],
All I basically want to do is to be able to display the first 4 columns of a web API source or even choose which four columns to display by INDEX not by name. Is this possible?
Instead of converting the result of the AJAX call as was suggested, I implemented an "ugly hack" that proved effective. I was really surprised that my problem could not be solved conventionally by "index" so instead I created a pseudo-index that grabs the column names from data attributes in the table tag and fed it to numbered variables like this:
//Display first 4 columns of data
var f1 = $(".grid").attr("data-f1");
var f2 = $(".grid").attr("data-f2");
var f3 = $(".grid").attr("data-f3");
var f4 = $(".grid").attr("data-f4");
var f5 = $(".grid").attr("data-f5"); //this value is the same as f1
var t = $(".grid").DataTable({
columnDefs: [{
"searchable": false,
"orderable": false,
"targets": 0
}],
order: [[1, 'asc']],
ajax: {
url: "/api/users",
dataSrc: ""
},
columns: [
{
data: f1
},
{
data: f2
},
{
data: f3
},
{
data: f4
},
{
data: f5,
render: function (data) {
return "<a href='#'><i class='fa fa-eye js-view' data-id='" + data + "'></i></a> | <a href='#'><i class='fa fa-pencil js-edit' data-id='" + data + "'></i></a> | <a href='#'><i class='fa fa-trash js-delete' data-id='" + data + "'></i></a>";
}
}
],
This approach proved to be surprisingly flexible, as I could swap the display order of the column on the fly without changing the code. Still, I would have preferred the indexed approach, though.

Data does not show in dataTables

No error but no data. Is there an option I need to add to make this work? my data uses the result from an ajax call.
Any suggestions?
Code:
$.ajax({
cache: false,
url: "http://a.co.nz/GetTestDocs",
data: '{'
+ ' "oldCompanyIdString": "' + selectOldCompanyId + '", '
+ ' "effectiveDateString": "' + selectEffectiveDate + '", '
+ ' "endDateString": "' + selectEndDate + '",'
+ ' "userToken": "' + userToken + '"'
+ ' }',
type: "POST",
//jsonpCallback: "Value",
contentType: "application/json",
dataType: "json",
error: function (jqXHR, textStatus, errprThrown) {
//console.log(errprThrown);
//window.location = location;
},
success: function (value) {
if (value.GetTestDocsResult != null) {
result = $.parseJSON(value.GetTestDocsResult);
if (result.length == 0) {
alert("Your query did not return any data.");
}
else {
console.log(result);
bindDataTable(result.DocsList);
}
}
}
});
function bindDataTable(data) {
$('#docuList').dataTable({
"processing": true,
"ajax": data,
"columns": [
{ "data": "Description" },
{ "data": "FileLocation" }]
});
}
Help pls..my first time to use dataTables
Replace "ajax": data, with "data": data,
function bindDataTable(data) {
$('#docuList').dataTable({
"processing": true,
"data": data,
"columns": [
{ "data": "Description" },
{ "data": "FileLocation" }
]
});
}

Resources