How would add a function to a form that posts to an api to pull json data from search button?
How would add a function to a form that posts to an api to pull json data from search button?
I have created my serverside Datatables and they work great. Now I need a function that allows the user to type in a search form and press a search button to read specific data delivered via API to draw (or, in this case, redraw) my Datatables. The form can not simply filter already populated entries because I have to get them via API. I know its possible I just need to be pointed in the right direction. What are the best practices for creating these functions and forms?
This question has an accepted answers - jump to answer
Answers
You would use either
search()
orcolumn().search()
withdraw()
to send the search request via server side processing. These APIs won't use the client data since you have server side processing enabled.Kevin
Can you show an example of this type of search?
See if this page helps:
https://datatables.net/examples/api/regex.html
Kevin
the search API is just a filter. I have created an ajax form in ASP.NET MVC that calls to EntityFramework to pull data. That data is required to draw the 'new' database table. I have created a similar form to that one displayed at https://datatables.net/examples/api/regex.html but it is unsuccessful and is out of scope because of the amount of data I am pulling ... I don't get a response from the ajax form. It has to be processed from submitting the form which calls the ajax. Essentially, that form you suggested is client-side ajax I'm asking for a viable serverside approach. I don't believe there is one listed that combines the two functionalities.
Maybe I misunderstood. Are you using
serverSide
processing? or your own ajax?If you are using the
serverSide
then here is a simple example showing it works.http://live.datatables.net/cezofode/1/edit
If you are using your own ajax to fetch the table data then you can either use
clear()
followed byrows.add()
in your ajax success function. Or you candestroy()
the Datataable and recreate it.Maybe you can provide more specifics of what you are doing.
Kevin
I see how that solution works and I have a prior implementation similar to that. Things are a little different in the ASP.NET MVC world with Entity Framework....
Here's an example of how the file structure operates.
https://datatables.net/forums/discussion/40690/sample-implementation-of-serverside-processing-in-c-mvc-ef-with-paging-sorting-searching#latest
Right - the server-side aspect is going to be significantly different for EF, but I'm not clear what is happening on the client-side here. I'd be tempted to use
$.ajax()
to make an Ajax request to your server to get the data (with a regular jQuery click event) and then use the API methods Kevin suggested (clear()
androws.add()
) to clear out the table and then add the new data to it.Allan
Also - do you need server-side processing? Are you using 50'000 or more records?
Allan
There are records in the 100,000's if not more... I have a working serverside solution. What I can't get working is a dedicated column search option. I will post my code a little later on today. Hopefully, you can help.
Here is the code on my index.cshtml file. The name search works but the other two column searches do not. I also only need one form with several fields and one search button. Can you help? if you want me to post the controller I can.
I received the solution from here
Its your
GetTableData
code that we'd need to be able to see - that is implementing the search and ordering.Allan
Here's the entire project including database.. Make sure you change the .config connection strings to match your database environment and initialize migrations and update database in Package console.
https://github.com/SunRhythms/DatatablewCustomSearch...
The DB can also be found on Mockaroo..
It would much simpler for you to provide your GetTableData code, as requested.
https://github.com/SunRhythms/DatatablewCustomSearch
https://github.com/SunRhythms/DatatablewCustomSearch/blob/master/DatatablePlugin/Controllers/HomeController.cs
...
I know its these lines but I don't know how to write the object to create the input that pulls the reflected column. So I am asking how can I rewrite this for a form group with 4 inputs and one submit button. The inputs are queries for the columns in the datatable.
1:
and 2:
I see in your code above where you are doing the name search:
But I don't see a search on the other two columns, which would explain why they aren't working. Similar code would need to be added for them.
Allan
Once again, I know its these lines but I don't know how to write the object to create the input that pulls the reflected column. So I am asking how can I rewrite this for a form group with 4 inputs and one submit button. The inputs are queries for the columns in the datatable.
You mean on the server-side? You've already got the employee name column working, so can you not just employ the same approach for the other columns?
for example. Sorry if I'm just not getting the issue - it looks like you are 99% of the way through and it just needs those other conditions added to your code. I believe DataTables, from your code above, is already sending the data you need, you just need to access it and then use it as part of your query construction.
Allan