I have the related models Request 1: M Tasks and I would like to show a datatable with yajra in the show view of the requests, that is to say that if I enter the show view of the request with id = 1 it will show me a datatable filled only with the tasks related to the request id = 1, I tried to return the datatable from the show function of the controller but I didn't know how to only get the tasks related to the request.
In any case, I don't know if this is of any use, but, this is how I make my datatable for the index of the tasks:
public function taskData()
{
$tasks = Task::join('requests', 'requests.id', '=', 'tasks.request_id')
->select('tasks.id', 'requests.code_request', 'tasks.fake_id',
'tasks.date');
return Datatables::of($tasks)
->addColumn('btn', 'tasks.actions')
->rawColumns(['btn'])
->make(true);
}
and then in my view:
<script>
$(function() {
$(document).ready(function(){
// initializing Datatable
var table = $("#tareas-table").DataTable({
serverSide: true,
pageLength: 10,
ajax: '{!! route('datatables.tareas') !!}',
columns: [
{ data: 'fake_id', name: 'tareas.fake_id' },
{ data: 'codigo_solicitud', name: 'solicituds.codigo_solicitud' },
{ data: 'fecha_inicio', name: 'tareas.fecha_inicio' },
{ data: 'estado', name: 'tareas.estado' },
{ data: 'btn', name: 'btn',orderable:false,serachable:false,sClass:'text-center' }
]
});
});
</script>
thanks
You can use the DataTableScopeContract for that.
The docs have an example for that: https://datatables.yajrabox.com/services/scope
Related
i create datatable, i want create dynamically columns data.
my init datatable looks like this :
var columnsData = [
{data: 'name'},
{data: 'phone'}
];
var dtable = $('#dtable').DataTable({
serverSide : true,
ajax : '/data',
columns : columnsData,
});
in some condition, make columns data changed. i don't have solution for that.
example columns what i want to change :
columnsData = [
{data: 'name'},
{data: 'address'}
]
i was try
dtable.columns.data( columnsData ).load();
dtable.columns.data( columnsData ).draw();
but still doesn't work.
You can use the below function to recreate the data table with changes in structure.
function getDT() {
$.ajax({
url: '/data',
success: function (data) {
data = JSON.parse(data);
columnNames = Object.keys(data.data[0]);
for (var i in columnNames) {
columns.push({data: columnNames[i],
title: capitalizeFirstLetter(columnNames[i])});
}
$('#dtable').DataTable( {
processing: true,
serverSide: true,
ajax: '/data',
columns: columns
} );
}
});
}
please find the complete implementation on Example
Hello I have an editor table and would like to design one column as a variable dropdown. The values for the dropdown come from the ControllerDrpDwn method implemented in the controller. How can i get this output as dropdown options?
editor = new $.fn.dataTable.Editor({
ajax: { url: "/Controller/EditorTable" , type: "POST"},
table: "#tbl",
fields: [{
label: "abc:",
name: "abc"
},{
label: "xyz:",
name: "xyz",
type: "select"
// option: Output from ControllerDrpDwn method
}]
});
This is the method:
public async Task<IActionResult> ControllerDrpDwn()
{
return Ok(await _context.Dropdownoptions.Where(x => x.Selectbezeichnung == "xyz").Select(x => new
{
name = x.Optionbezeichnung
}).ToListAsync());
}
If you write javascript codes in a Razor page, you can follow this code to get the result:
#{var options = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Model);}
editor = new $.fn.dataTable.Editor({
ajax: { url: "/Controller/EditorTable" , type: "POST"},
table: "#tbl",
fields: [{
label: "abc:",
name: "abc"
},{
label: "xyz:",
name: "xyz",
type: "select"
option: #Html.Raw(options)
}]
});
But in controller, you need to change the code like this:
public async Task<IActionResult> ControllerDrpDwn()
{
return Ok(await _context.Dropdownoptions.Where(x => x.Selectbezeichnung == "xyz").Select(x => new
{
label= x.Optionbezeichnung,
value='value of item comes here'
}).ToListAsync());
}
Trying to implement a sortBy method on a Vue component. Here's what I have so far.
var studentTableComp = {
template: '#studentTable',
data: function () {
return {
students: data,
show: true,
columns: [
'id',
'candidateType',
'complete'
]
}
},
methods: {
sortBy: function (sortKey, e) {
e.preventDefault();
_.sortBy(this.students.students, sortKey);
}
},
}
And I can see that the sortBy function is being hit, and that the parameters sortKey are being sent correctly from the template, but the problem seems to be with the _.sortBy function doesn't have any effect on the this.students.students array.
Any ideas why it doesn't working.
I've tried the _.sortBy method using Underscores example array, and it works fine, but I think something happens to the array when it is added to the data on the Vue component that might be preventing this function from working correctly.
I see _.sortBy working, see fiddle here.
Here is the code:
var demo = new Vue({
el: '#demo',
data: function(){
return {
students: [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}]
};
},
methods: {
sorted: function() {
return _.sortBy(this.students, 'name');
}
}
})
Add more code from your repo, if you see still this is not resolved.
Hi is there any way to pass detailRow reference to a function using kendo grid template column?
Here is my trail.
function detailInit(e) {
detailRow = e.detailRow;
detailRow.find("#mygrid").kendoGrid({
dataSource: {
data: empModel,
},
columns: [
{
field: "empId",
title: "Emp ID",
template: '<a href="\\#" onclick="showEmpDetails(\'#= detailRow #\')"> }
]
});
});
Try to place every detailRow you retrieve at the detailInit into globally located array, then pass this index to your click method (or some sort of key - could be dictionary, rows has uid) and then make the method to read the row details from your array/collection based on the id you have passed in. (Ideally read the row details directly from your datasource by the uid, no need to duplicate the data.) Please refer to below code as pseudo code, I didn't have a chance to run it.
var rows = new Array();
$('.clickable').click(function () {
// get the id for your detail
var detailId = this.attr('detailId');
// pass it further
showEmpDetails(detailId);
});
function detailInit(e) {
detailRow = e.detailRow;
// add roe to your (cache) collection
rows[rows.legth] = detailRow ;
detailRow.find("#mygrid").kendoGrid({
dataSource: {data: empModel},
columns: [
{
field: "empId",
title: "Emp ID",
// add attribute onto your a tag
template: '<a href="\\#" detailId = "#= rows.legth #" class="clickable">'
}
]
});
});
function showEmpDetails(id) {
// read from your collection and do what you need
console.log(rows[id]);
}
I would like an example of this. Please give me a detailed example.
If you want to store the results in a Store, first create your model like this:
Ext.define('app.model.Example', {
extend: 'Ext.data.Model',
config: {
fields: ['data'],
}
});
Next, create your Store:
Ext.define('app.store.Examples', {
extend: 'Ext.data.Store',
config: {
model: 'app.model.Example',
autoLoad: true,
autoSync: true,
},
});
An example of JSONP request is easy to find in Sencha Touch 2 Kitchensink Demo. Here I added some code to add the results to your store:
Ext.data.JsonP.request({
url: 'http://free.worldweatheronline.com/feed/weather.ashx',
callbackKey: 'callback',
params: {
key: '23f6a0ab24185952101705',
q: '94301', // Palo Alto
format: 'json',
num_of_days: 5
},
callback: function(success, result) {
var store = Ext.getStore('Examples');
var weather = result.data.weather;
if (weather) {
store.add({data: weather});
}
else {
alert('There was an error retrieving the weather.');
}
panel.getParent().unmask();
}
});
Hope it helps ... Let me know if there are any errors.