Ajax database search with bootstrap pagination - mysql

I am building a live database search using Ajax. When the user updates the search form it posts the data and writes the result of the sql query to the tablebody. The page is styled using Twitter Bootstrap v4.
Currently pagination works through dynamically generated radio buttons inside the search form representing the page number. This sets the offset of the sql query so the correct rows are selected. However, this approach isn't pretty and I'd like to switch to Bootstrap's pagination.
Could I somehow combine the code for radio boxes with pagination with some clever use of labels and hidden inputs? Would it be better to have the pagination outside of the form, updating a hidden input representing page number when pressed? It would be more complex to keep track of the active page this way.
Are there any other best practices to follow when creating a live search?
A static version of my code can be found here. Here's the post request I use to send the data:
$.post('/clients/get/json', $("form#search").serialize(), function(res) {...}

Related

Drupal 8 - Multiple fieldset in content type

I can create group (I use fieldset) in "manage form display". How to multiple the group just like field collection can do. I don't want use field collection because it can't generate JSON in API view. As I already post at Field Collection RESTful API rendered in HTML but not one reply.
So, how to multiple the fieldset ? Like have a "Add Item" button below the fieldset and add fieldset after click the button.
Millions of thanks if anyone can help.
You cannot do that with Field Groups. There is a Field Collection alpha for Drupal 8. However, I use Paragraphs in almost all my builds to get the same result. It is more challenging to configure but also very flexible.

Hold data for list to add later?

So no SQL tables or anything here. Basically, I have 3 pages that have 6 items each. Each item consists of an image and then some text and also a smaller image than changes from a check to an "x" depending on if user selects or not. So if the image is checked, the user is adding that to a list which would display on a fourth page. This data needs to persist through just a session and if the session times out, then it resets. If the list is complete and on that fourth page the user choices to email or share list via social sharing, then the data would be gone after that action. What I am trying to figure out is the best way to approach and implement this with minimal time and effort as it has to go out quickly. Can any of you explain and maybe point to some links with info on the best way to achieve?
This is being done in asp.net web forms with html, css, and javascript.
Much appreciated!
You can use ViewBag() (for view in razor) or ViewData[] (for page in .net) to hold data for one web page.when you want it to other page. You can pass it to other page.
check this out
ViewBag, ViewData and TempData

JQuery and Perl to display dynamic results on drop down selection

I'm currently using Perl CGI to display some internal billing pages at work. I'm using JQuery as well to add a little 'spunk' to it if you will.
I'm looking to generate/display HTML based on drop down selection by the user. Now, I can handle this part no problem. I'm generating table rows/table data on click based on the selection made by the user.
I want to retrieve and display values stored in the database for the corresponding data im displaying on button click, and without refreshing the page of course.
Can someone point me in the right direction? Thanks in advance.
You'll want to use an AJAX request to get the data. So, for example, if you have a select with class 'ajax' and you want to display data based on the option selected, you can do something like:
$('.ajax').change(function(){
$.get({'url', { value: this.val() }, function(data){
// display the data with a .html() or .append() or something
});
});
Replace 'url' with the url of a page that queries the database using the value that you send it.

Advanced techniques for reducing the page weight of hidden HTML forms

I'm working on a large application with lots of editable objects on a page. One page for example contains a list of categories and each category has a list of tasks. Both categories and tasks can be editted via a modal dialog window with a form inside.
The current solution is to embed a hidden form prepopulated with data next to each object in the html. When the edit button is clicked, the form is displayed inside the modal dialog and submitted via ajax. The advantage to this solution is that modal dialogs pop up quick. The disadvantage is the growing number of dom elements and page size as each object requires another hidden form.
I am looking for ways to reduce the page weight and reduce the number of dom elements. I am looking for a balance of performance vs maintainability, as there are lots of pages with lots of different objects to consider.
Use ajax to dynamically load the modal populated form. This reduces the page weight considerably, but causes a delay when the edit button is clicked and is the worst for user experience. It also requires each form to have an ajax handler for this specific function.
Store the form parameters as json data and populate the form values from a cloned template form when an element is edited. This reduces dom elements significantly, and the page weight a little as well. A generic piece of javascript needs to be written to insert the json data into input elements.
Store the entire form HTML as a json object inside a data attribute of a dom element. This does nothing for page weight but will reduce the number of dom elements. On edit, the html can be injected into the modal dialog. Its also the easiest to implement.
Scrape the content of the page for the values of the form, and inject them into a cloned template form. This reduces the duplication of content, and does the most to reduce page weight and dom elements. However it feels brittle and intrusive, there needs to be id coordination for content and special care needs to be taken so that hidden input fields are also injected into the page.
Those are the options I've come up with. The JSON solutions are the most appealing to me currently. I'm curious if anyone has other solutions or insight to any of these?
The most light-weight is probably store the entire form definition as JSON and write some sort of engine that would dynamically build the form HTML on the fly. You'd then use another JSON file to represent the data layer that populates/contains the form data.
The fun you run into is how to manage the layout of the form on top of all this. For a standard two-column layout it's simply. Beyond that it's an awful lot of work.
I started writing one using prototype.js about five years ago. I never finished it but it works. Feel free to view the source and steal the code.
If you look at advanced client libraries like ext.js, this is how they're doing it.
If you're going for speed, and don't have a framework handy, don't think "HTML", think "DOM element." Maintain as much data as possible in a "global" data object, and only go to the server for information you don't have. Initialize a modal <div> either on demand or asynchronously after form load, and populate its options via JavaScript in response to a handled event from each <input> element.
For performance, consider storing categorical information in some localStorage option.
Realistically, however, you want a framework. (If you don't use one someone else wrote, you should write your own.) I'm a fan of the Enyo framework leftover from WebOS personally, but there are others out there to help you do this.

How to Load the page First and read database

HTML form has some text boxes and a drop down box.
Drop down has huge values, and takes lot of time to fetch from database.
So I want to load the page first and while the user fills the form (text boxes) I want to load the drop down box (without his knowledge :-) ).
But without any event trigger, how do I make call to database again ?
I am using JSF with RichFaces, Servlet.
The following code is not working
<h:selectOneMenu value="#{obj.selectedValue}">
<f:selectItems value="#{obj.allValues}" />
<a4j:support selfRendered="true" action="#{bean.action}"/>
</h:selectOneMenu>
Thanks,
+1 for using Ajax - but if you have a very large number of values,t hen you might want to consider using an auto completion dropdown - where the the user starts typing what they need and after they have typed a few characters, you kick off your ajax reqeuest and just load those requests that match.
have a look at "google suggest" if you want to see this in action
-Ace
As already mentioned you can use AJAX to load the dropdown items asynchronously, but I would suggest redesigning the form so that the huge dropdown is not required. Perhaps let the user search for the correct value on a previous or subsequent screen? Long dropdowns are not easy to use as they require lots of scrolling and it can be hard to find the correct value on a large list.
At the bottom of your page put the following:
<a4j:jsFunction name="yourJsFunction" action="#{bean.fetchSelectItems}"
reRender="yourDropdown" />
window.onload = yourJsFunction();
You will have to use AJAX. When the page loads display a empty select box. Then write some JavaScript that will call some URL on your server that will return the options for the select box. And when you get that just populate the select box with those values.
Be advised that your form will be useless to those without JavaScript.