你好,我在Largo 5.7.26中使用mongo db。
我想从mongo db获取产品详细信息并加载到数据表中,我的mongo db包含2081个产品。
有时会加载,有时会显示错误DataTables warning: table id=ProductTables - Ajax error. For more information about this error, please see http://datatables.net/tn/7
`
/**
* @author Prakash Vaghela<prakashvaghela700@gmail.com>
* @description this function will fatch the data from mongo db using Ajax and dynamically
* append row to datatables
* @returns {Boolean}
*/
function initializeTable() {
if (tableDataTable) {
return false;
}
tableDataTable = $table.on('preXhr.dt', function (e, settings, data) {
// showLoading("loading data...");
}).on('draw.dt', function () {
// hideLoading();
}).DataTable({
processing: true,
"language": {
"processing": "Hang on. Waiting for response..." //add a loading image,simply putting <img src="loader.gif" /> tag.
},
serverSide: true,
pageLength: 5,
ajax: {
url: window.loadProducts,
type: 'post',
data: function (d) {
d._token = token;
d.vendorKeyword = vendorKeyword;
d.productTypeKeyword = productTypeKeyword;
return d;
},
},
columnDefs: [
{
name: "pid",
data: "pid",
className: "text-center",
targets: [0],
visible: true,
render: function (data, type, row, meta) {
var return_div = $("<div class='table-data-design '>");
var productBox = $(".product-dropDown").clone();
productBox.removeClass("product-dropDown");
productBox.addClass("parentDiv");
productBox.attr("data-pid", row.pid);
productBox.find("#parentImage").attr("src", row.img);
productBox.find(".parent-titleName").html(row.title);
var variants = row.variants;
$.each(variants, function (key, value) {
var childBox = $(".child-box-container").clone();
childBox.show();
childBox.removeClass("child-box-container");
childBox.attr("data-variantPid", value.id)
childBox.addClass("variant");
childBox.find("#childImage").attr("src", value.img);
childBox.find(".child-titleName").html(value.title);
childBox.find(".variant-price").html(value.price);
var indexInArray = isSelectedList.indexOf(value.id);
if (indexInArray > -1) {
childBox.css({"background-color": "#4998f2"});
childBox.attr("isSelected", "1");
}
productBox.append(childBox);
});
productBox.show();
return_div.append(productBox);
return return_div.html();
},
},
{
name: "title",
data: "title",
className: "text-center",
targets: [1],
visible: false,
render: function (data, type, row, meta) {
var return_div = $("<div class='table-data-design '>");
return_div.append(data);
return return_div.html();
}
},
{
name: "vendor",
data: "vendor",
className: "text-center",
targets: [2],
visible: false,
render: function (data, type, row, meta) {
var return_div = $("<div class='table-data-design '>");
return_div.append(data);
return return_div.html();
}
},
{
name: "type",
data: "type",
className: "text-center",
targets: [3],
visible: false,
render: function (data, type, row, meta) {
var return_div = $("<div class='table-data-design '>");
return_div.append(data);
return return_div.html();
}
},
]
});
}
/**
* @author Prakash Vaghela<prakashvaghela700@gmail.com>
* @description this method will triggers when user change the dropdown list
* based on selected value data tables fatch vendors products only from db
*/
$(document).on("change", "#selectVendor", function () {
vendorKeyword = $("#selectVendor option:selected").val();
tableDataTable.ajax.reload();
});
/**
* @author Prakash Vaghela<prakashvaghela700@gmail.com>
* @description this method will triggers when user change the dropdown list
* based on selected value data tables fatch products type only from db
*/
$(document).on("change", "#selectProductType", function () {
productTypeKeyword = $("#selectProductType option:selected").val();
tableDataTable.ajax.reload();
});
@extends('layout.app')
@section('content')
<div class="Polaris-Stack__Item">
<div>
<table id="ProductTables" class="table table-striped table-bordered table-hover" style="width:100%">
<thead>
<th>Products</th>
<th>title</th>
<th>vendor</th>
<th>type</th>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
@csrf
@stop
@section('js')
<script src="{{asset('js/app/manageProduct.js')}}?t=<?php echo time(); ?>"></script>
<script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"></script>
<script>
window.updateSubscribeSettings = "{{url("updateSubscribeSettings")}}";
window.syncProduct = "{{url("syncProduct")}}";
window.loadProducts = "{{url("loadProducts")}}";
window.loadVendorAndProductList = "{{url("loadVendorAndProductList")}}";
window.filterProduct = "{{url("filterProduct")}}";
</script>
@stop
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Log;
use DB;
use Auth;
use Auth\User;
use Pimlie\DataTables\MongodbDataTable;
class ProductMongoController extends Controller {
/**
* @author Prakash Vaghela<prakashvaghela700@gmail.com>
* @description This method will fatch the products from mongo db
* @param Request $request
* @return \Exception
*/
public function loadProducts(Request $request) {
$retData = new \stdClass();
$vendorKeyword = $request->vendorKeyword;
$productTypeKeyword = $request->productTypeKeyword;
$shopid = Auth::user()->id;
try {
if ($vendorKeyword && $productTypeKeyword) {
$datatables = datatables(DB::connection('mongodb')
->collection('product_' . $shopid)
->where("vendor", "=", $vendorKeyword)
->where("type", "=", $productTypeKeyword)
->get());
} else if ($vendorKeyword) {
$datatables = datatables(DB::connection('mongodb')
->collection('product_' . $shopid)
->where("vendor", "=", $vendorKeyword)
->get());
} else if ($productTypeKeyword) {
$datatables = datatables(DB::connection('mongodb')
->collection('product_' . $shopid)
->where("type", "=", $productTypeKeyword)
->get());
} else {
$datatables = datatables(DB::connection('mongodb')
->collection('product_' . $shopid)
->get());
}
return $datatables->toJson();
} catch (\Exception $ex) {
return $ex;
}
}
}
Route::post('/loadProducts', 'ProductMongoController@loadProducts');