Row not updating after editing data
Row not updating after editing data
kumamari
Posts: 24Questions: 9Answers: 1
I have an issue with my rows data not updating after I edit the data in my editor. I can see the data has successfully been edited in my database, but it's not reflecting on the datatable.
Here is my code.
table = $('#myTable').DataTable({
dom: 'ZBfrtip',
scrollX: true,
scrollY: 615,
processing: true,
serverSide: true,
ajax: {
url: "/itemCreation/getRecords.php"
},
columns: [
{ data: "button" },
{ data: "item_id" },
{ data: "item_desc" },
{ data: "item_name" },
{ data: "item_detail" },
{ data: "bid_start" },
{ data: "min_bid_increment" },
{ data: "bid_increment" },
{ data: "fair_market" },
{ data: "group_id" },
{ data: "categories" },
{ data: "confirm_bids" },
{ data: "consign" },
{ data: "buy_out" },
{ data: "auc_start" },
{ data: "start_time" },
{ data: "tzoffset" },
{ data: "taxable" },
{ data: "display_item" }
],
columnDefs: [
{ width: '20%', targets: 0 }
],
colResize: {
tableWidthFixed: false,
handleWidth: 10
},
fixedColumns: true,
searching: true
});
setInterval(function() {
table.ajax.reload(null,false);
}, 3000);
What am I missing?
This discussion has been closed.
Answers
Are you using the Datatables Editor? Its not shown in your code.
If you are... Are you returning the edited data to the client as described in this doc?
https://editor.datatables.net/manual/server#Example-data-exchanges
If you reload that page does it show the correct data?
Kevin
Yes, I am using the Datatables Editor.
I am now returning this data via my php script through an ajax call.
I then do
table.ajax.reload
still nothing changes. Weird thing is when I create my items, the row gets added on the table.Here is all my code for reference. I can't seem to find a solution.
Are you using the PHP libraries for Editor? If so, can you also show that code please?
I would suggest removing line 321 (
table.ajax.reload();
) above. It isn't needed - the table should automatically be updated from the edit result. I realise that is probably an attempted workaround, but if you could remove it for now, as it might be confusing things.Allan
No, I'm not using the libraries atm. I did my own script to echo out my array with json_encode.
I'm getting this back through my network tab on saveItem.php when I edit my entry. I've modified my rows on my html markup to all have an id through the DT_RowId property.
{"data":[{"DT_RowId":"row-257","item_id":"001","auc_id":"1","item_detail":"hey yous","item_name":"","item_desc":"lorem ipsumsss","categories":"","fair_market":"0.00","confirmBids":"0.00","consign":"0.00","bid_start":"0.00","min_bid_increment":"0.00","tzoffset":"","auc_start":"1899-11-30 ::00","auc_end":"2099-12-23 00:00:00","start_time":"","display_item":"","buy_out":"0.00","taxable":"0","group_id":""}]}
I've looked at your samples and it seems this is the exactly the same format. I'm confused as to why this won't edit when I submit my edited entry through the lightbox on the editor.
Ah - but your Editor
ajax
option is:It isn't pointing at the save script.
I think I see the issue now. You aren't calling
submit()
which trigger's Editor's own save call - thus, you appear to be using your own Ajax call as a workaround for that. The upshot is that Editor can't execute its post submit actions - including updating the row!Because that is bypassing Editor - if you want to continue using that method, you'd need to also update the row yourself.
Allan
Alright, I modified the script to now use the getRecords.php option when it edits. I am doing a call with the submit method on the api. It is now calling a POST action with an upload parameter of edit which I recognize and then go into my array and capture the data. I have it coming back with this data which is inline with what my db has.
{"data":[{"DT_RowId":"row-257","item_id":"001","auc_id":"1","item_detail":"","item_name":"","item_desc":"lorem ipsum","categories":"","fair_market":"0.00","confirmBids":"","consign":"0.00","bid_start":"0.00","min_bid_increment":"0.00","tzoffset":"","auc_start":"","auc_end":"2099-12-23 00:00:00","start_time":"","display_item":"","buy_out":"0.00","taxable":"0","group_id":""}]}
After that is done, it does another call to the getRecords.php with the following object coming back in the network tab.
{"draw":"1","recordsTotal":"1","recordsFiltered":"1","data":[{"button":"<button>Test<\/button>","item_id":"001","item_desc":"lorem ipsum","item_name":"","item_detail":"","bid_start":"0.00","min_bid_increment":"0.00","bid_increment":"0.00","fair_market":"0.00","group_id":"","categories":"","confirm_bids":"0.00","consign":"0.00","buy_out":"0.00","auc_start":"","tzoffset":"Eastern","start_time":"","display_item":"OFF","DT_RowId":"257","primaryID":"109001","secondaryID":"109001","taxable":"0"}]}
I assume at this point, it paints the table, but I am not seeing the table change even though the record gets updated.
Anything jump out?
Sounds like you are emulating the server writing to the DB and responding with pre-canned data.
I see a couple things:
buttons
columnconfirm_bids
but the first response hasconfirmBids
This example shows the Editor sending the edited information and receiving the first response. Then when the table is draw it sends a SSP request for the page data and receives the second response. Same as the behavior you are seeing.
The problem is your second response does not have the changes. I assume this is also pre-canned data to initially populate the data. On the surface it looks like everything is behaving as expected but your server side test code needs a bit of changing to reflect your changes.
Assuming I'm correct about your test code then a slight change might help. Not sure what you are using but maybe you can create a global variable containing the original data object and that will be returned when Datatables initializes. Update the row with the editor then in your code update the global variable object with the new data, return the first response with updated fields and the second response should return with the updated variable. And you should see the table updated. Hope this makes sense.
Kevin
I figured the issue out. Here was the primary issue in all this that I didn't realize.
When I would draw the records initially for the datatable I had this format.
{"draw":"1","recordsTotal":"1","recordsFiltered":"1","data":[{"button":"<button>Test<\/button>","item_id":"001","item_desc":"lorem ipsum","item_name":"","item_detail":"","bid_start":"0.00","min_bid_increment":"0.00","bid_increment":"0.00","fair_market":"0.00","group_id":"","categories":"","confirm_bids":"0.00","consign":"0.00","buy_out":"0.00","auc_start":"","tzoffset":"Eastern","start_time":"","display_item":"OFF","DT_RowId":"257","primaryID":"109001","secondaryID":"109001","taxable":"0"}]}
The problem is when drawing the records initially and drawing them on edit, they need the same format. I didn't need the draw, recordsTotal and recordsFiltered keys. What I did need was this format on the initial loading and on the edit. Once I did this, the editing on the records worked. Here is what works. It's the same on the documentation on the site. Also I have the DT_RowId to identify each row. So basically on initial drawing and edit I have the same format of the json encode from the SSP but the values are different because of editing. Hope this helps someone.
{"data":[{"button":"<button>Test<\/button>","DT_RowId":"row_257","item_id":"001","item_desc":"lorem ipsumsxxxxxx","item_name":"boom","item_detail":"boom","group_id":"","auc_id":1,"categories":"","fair_market":"0.00","confirm_bids":"","consign":"0.00","bid_start":"0.00","min_bid_increment":"0.00","tzoffset":"","auc_start":"","auc_end":"2099-12-23 00:00:00","start_time":"","display_item":"","buy_out":"0.00","taxable":"0"}]}