Server side, column sorting with render function
Server side, column sorting with render function
I suspect that the answer is right in front of me but I am having trouble trying to get my data tables to sort properly when rendering data through a function such as:
` columns: [
{data: 'work.id'},
{data: null,
render: function (data, type, row) {
if (data.customer.cname) {
return data.customer.cname;
} else {
return data.customer.fname + ' ' + data.customer.lname;
}
}`
I imagine that I should be able to append a custom sort delimiter within this function but I have not been able to succeed as of yet.
Thanks in advance.
This question has an accepted answers - jump to answer
Answers
Maybe I'm missing the point of the question but if you are using
serverSide
your server script is responsible for the sorting. The server script isn't going to know about the results of the render function. You would need to replicate the render function in your server side code and have it sort the data appropriately.Kevin
Kevin,
Thanks for the quick reply. I do understand where your coming from and I should have been more clear and included the below in the original post.
The issue that I am not understanding is when using the above function in the manner that I am, with the data:'null' the server is of course not getting anything to sort with. In short I guess a better way to phrase the question is: how can I achieve the rendering functionality alongside with a "data: value" to facilitate the server side sorting functionality?
I may still be missing something in your question. Two of the column parameters sent in the Server Side request come from the
columns.data
option and thecolumns.name
option. You can either set thecolumns.data
option to one of your returned fields or you can set thecolumns.name
or both. This example shows both:http://live.datatables.net/loqevoji/1/edit
The first column is set to
data: first_name
but it renders data from different objects in the row:return row.position + ' ' + row.last_name;
Still not sure I'm answering your question. If not what exactly are you wanting to send to the server to sort with?
Kevin
Kevin,
I am sorry to take so long in getting back to this, but taking another look at this issue, I am thinking the issue I am running into is related to the array formats as all of the columns that I am having issues with are formed from nested arrays. An example from the server data:
{"data":[{"DT_RowId":"row_1965","work":{"id":"1965","name":"284","date_ord":"2018-08-30 00:00:00","date_start":"2018-09-12 00:00:00","date_due":"2018-09-14 00:00:00","mu":"0.00","note":"<p>text<\/p><p>123456<\/p><p> <\/p>","company":"1"},"customer":{"cname":"","fname":"Dennis","lname":"Dennis"},"work_status_options":[{"id":"0","desc":"Not-Complete"}],"roles":[{"role_id":"2","role_name":"Admin"}]},.....
The columns in this case that are an issue are pulled from "customer", "work_status_options" and "roles".
This is used with the following data table construct:
It appears as though the issue in all of these instances is more related to the naming/interpreting of each columns name as the error that is received is along these lines when dealing with the roles column:
"DataTables warning: table id=editor_table - Unknown field: roles[].role_name (index 4)"
As Kevin says, if you are using server-side processing, then its up to the server-side script to do any sorting and filtering. Since the renderer is on the client-side, the server-side processing script can't take into account what you have on the client-side.
So the first key question is: do you need server-side processing? Have you got tens of thousands or more records? If not, disable server-side processing.
If it is needed, that you'd most likely need to create a VIEW in your database that the server-side processing script can query. That VIEW would do the conditional formatting needed, and that will allow correct sorting and filtering.
Allan
Allen and Kevin,
Thanks for all of the information you did get me not he right track. I imagine that there might be a better way to achieve this but the following did work well and scales for other similar instances. I am using codeigniter as a framework so there is a little bit of handing things around but the concept was to use the rest interface and use a direct create/edit of the table and a mysql_view for the datatable. this was achieved by simply renaming the json data before(server side) and after the create/edit model (client side via editor.on )so as to keep all validation and error catching.
Editor:
`})(jQuery, jQuery.fn.dataTable);
......
`
Table:
` table = $('#editor_table').DataTable({
Model:
` case 'work_editor_create':
'