Refreshing a Thymeleaf table dynamically without having to reload the entire page

I am currently using the Thymeleaf attribute to render data, but I am now looking to add a "Search" button without reloading the page.

Within the attribute departments, I am rendering a List<Department> from the database. While I understand how to achieve this using ajax, it requires me to replace the attribute with a RestController that returns JSON data. Is there a way to fetch data from the attribute without having to reload the page? Perhaps through ajax, JavaScript, or another method?

Answer №1

Achieving this task is possible with the use of fragments and AJAX. In your controller:

@GetMapping("/url")
public ModelAndView getResultBySearchKey()
    {
        List<depatments> areaList= new ArrayList<>();//results from db
        ModelAndView mv= new ModelAndView("search::search_list"); 
        mv.addObject("searchList",areaList);

        return mv;
    }

Additionally, in your search.html file, include the following code snippet and remember to incorporate inline JavaScript.

function loadSearchResult()
    {
    
     $.ajax({
      type: 'get',
      url: /*[[ @{'/url'} ]]*/,
    
      success: function(data){
    
      /*<![CDATA[*/
      
      
      $('.search_list').html(data);
      
      
      /*]]>*/
    },
      
    })
    
    }
<button class="btn btn-primary btn-sm"
th:onclick="'loadSearchResult();'">Search</button>
    <div class="row">


      <div class="col-md-12 search_list">
       <div class="table-responsive" th:fragment="search_list">
         <table
         class="table  table-bordered ">
           <thead>
             <tr>
               <th>SL No.</th>
               <th>Actions</th>
               <th>Name</th>
             </tr>
           </thead>
        <tbody>
    <!-- your desired rows-->
        </tbody>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Jasmine: create a mock ajax success callback and provide it with an argument

I'm interested in writing a Jasmine test that verifies the display of a specific string in the browser when a button triggers an AJAX call to retrieve a JSON object from an API. This JSON object then undergoes a function for extracting essential data. ...

What is the best way to position a single element in React using the Grid Component without causing any overlap?

I am struggling with positioning 3 components on my react page: PageHeader, SideMenu & FeatureList (which consists of Display Cards). Here is the code for each component: App.js // App.js code here... PageHeader.js // PageHeader.js code here... SideMenu ...

Having difficulties parsing JSON data in JavaScript

So I've got this script embedded in my HTML code $(document).ready(function() { type :'GET', url :'MapleLeafs2011.json', dataType :'json', success :processTeam, error :function() { alert(&apo ...

Generating an array of objects using Jquery from XML data

I need assistance with extracting data from XML text nodes stored in a variable and then creating an array of objects using jQuery. The XML data I have is as follows: var header = ['name', 'data1', 'data2']; var data = &apos ...

Setting up the InMemoryCache in Vue ApolloConfiguring the InMemoryCache

I've set up vue-apollo following the instructions in the apollo.config.js file from this guide: (I'm using VSCode). Now, I want to configure the InMemoryCache to exclude the typeName (addTypename: false) like it's explained here: https://w ...

What are the steps for creating personalized sliders with WordPress?

Seeking guidance on how to code WP Template files to enable control from the WP dashboard for adding or removing slider images. A sample code snippet is provided below. <div id="myCarousel" class="carousel slide" data-ride="carousel"> <!-- Indi ...

What is the process for pausing a video while it is still buffering and loading?

Is it possible to suspend a video when it is in an opening or preparing state? For example, if I open a video and then switch to another application using the smart hub feature, how can I suspend the video while it is in the process of opening or preparin ...

Utilize JavaScript declarations on dynamically added strings during Ajax loading

Is there a way to append HTML strings to the page while ensuring that JavaScript functions and definitions are applied correctly? I have encountered a confusing issue where I have multiple HTML elements that need to be interpreted by JavaScript. Take, for ...

Mix up table data cells using Javascript/jQuery

Can anyone provide me with some helpful tips? I'm really struggling with this. My Objective I aim to create a table with two columns ("name" and "rating"), consisting of 5 rows. 2 of these rows should have a random "rating" between 6-10 2 other ro ...

Restrict User File Uploads in PHP

I have a system set up that enables users to upload files under 200 MB. Once the file is downloaded once, it will be automatically deleted. Additionally, all files are deleted from the server after 24 hours. I am looking for a way to limit the number of up ...

Using ReactJS and Ruby on Rails to implement an AJAX delete request

There is a component named Items residing inside a parent component called ItemsContainer. A click on a button within the Items component triggers an Ajax function to delete that specific Item. However, I am encountering a 500 error message at the moment ...

Comparing Fetch and Axios: Which is Better?

Currently delving into the realms of axios and the fetch API, I am experimenting with sending requests using both methods. Here is an example of a POST request using the fetch API: let response = await fetch('https://online.yoco.com/v1/charges/&ap ...

Swap out periods with commas in the content of Json Data

I have a JSON file containing percentage data that I am extracting and displaying on my website: <?php $resultData = file_get_contents('https://example.com/json/stats?_l=en'); $jsonData = json_decode($resultData, true); if( isset( ...

What options are available to enable the user to input information into the v-time-picker component?

I am looking for a solution where users can input digits into the vuetify v-time-picker, while still being able to select the time on the clock. <v-col align-self="center"> <v-menu ref="menuTimeStart" v-model="me ...

Tips for implementing an ajax request in SharePoint 2013

Currently, I am working on developing a custom web part within SharePoint 2013 that requires an ajax call. Here is how I am attempting to achieve this: Within my CafeteriaWebPart.ascx, I have included the following code snippet: <%@ Assembly Name="$S ...

Tips on incorporating CKEditor4 wiris MathML formulas into react JS

I am having trouble displaying MathML in the DOM. When I try to render it, the output is not showing correctly in the Editor. I am utilizing CKEditor4 Let me share the code below to provide more context on what I have attempted so far App.js file: impo ...

Adjusting the alignment of Bootstrap navbar items upon clicking the toggle button

When I click the toggle button on a small screen, my navbar items appear below the search bar instead of aligning with the home and about elements. Below is an image depicting this issue: https://i.stack.imgur.com/4rabW.png Below is the HTML code structu ...

Is it possible to manipulate content using jQuery?

I need help figuring out how to update the content within a span tag when a user clicks on a button. The specific span tag I am trying to target has a class called fa_button_id_counter and an id that corresponds to the post id. Despite my efforts, I haven& ...

Server response not being awaited by Ajax call

Currently running WAMP v.2.5 on a Windows10 system for my PHP project connected to a MySQL DB that involves multiple AJAX calls, all functioning correctly. However, there is one specific call that keeps throwing an 'Unexpected end of input' error ...

Article: Offering CoffeeScript and JavaScript Assets Simultaneously

Currently, my web app is up and running using Node and Express. I initially developed it in JavaScript but now want to transition over to CoffeeScript. My goal is to have both file1.js and file2.coffee coexisting in the application (with both being served ...