How to pass parameter between two jQuery datatables

I have two types of data, one of which contains addition lists, and the other a list of files in the parent folder. This is how my script looks for a folder table:

var oTable = $('#folderTable').dataTable({ "bServerSide": true, "sAjaxSource": "AJAXViewFolders", "bProcessing": true, "bFilter": false, "aoColumns": [ { "sName": "folder_id", "bSearchable": false, "bSortable": false, "fnRender": function (oObj) { return '<a href=\"ViewFiles?parentid=' + oObj.aData[0] + '\">View</a>'; } }, { "sName": "folder_name" }, { "sName": "create_date" } ] }); }); 

Now, when the user clicks on the link, I need to transfer this parent to the datatable file. I still have no luck. This is how the JSON result looks in my controller, for datatable files:

 public JsonResult AJAXViewFiles(DataTableParamModel dtParams, int parentid) { var repo = new TrusteeDocumentRepository(); var allDocuments = repo.FindAllFiles().Where(c=>c.folder_id == parentid); IEnumerable<Files> filteredFiles; filteredFiles = allDocuments; var displayedFiles = filteredFiles.Skip(dtParams.iDisplayStart).Take(dtParams.iDisplayLength); var result = from c in displayedFiles select new[] { Convert.ToString(c.folder_id),c.file_name, c.upload_date.ToString() }; return Json(new { sEcho = dtParams.sEcho, iTotalRecords = allDocuments.Count(), iTotalDisplayRecords = filteredFiles.Count(), aaData = result }, JsonRequestBehavior.AllowGet); } 

How can I get the link in the folder table to pass the parent to jsonresult for the datatable file successfully?

+4
source share
1 answer

I assume that dataTables are on the same page, so I would switch it to a button ...

 "fnRender": function (oObj) { return '<button type="button" class="folder-view" data-id="' + oObj.aData[0] + '">View</button>'; } 

Add a direct click handler so you can set the current parent and update the dataTable file. A handler might look like this:

 $('.folder-view').on('click', function() { var $filesTable = $('#filesTable'); $filesTable.attr('data-parentid', $(this).attr('data-id')); //refresh the files table $filesTable.dataTable().fnDraw(false); }); 

Finally, dataTable files will need to override the fnServerData function to combine the extra data of the parent ...

 "fnServerData": function (sSource, aoData, fnCallback) { var extraData = [ { parentid: $('#filesTable').attr('data-parentid') } ]; $.ajax({ "dataType": "json", "type": "POST", "url": sSource, "data": $.merge(extraData, aoData), "success": fnCallback }); } 
+1
source

Source: https://habr.com/ru/post/1435945/


All Articles