admin管理员组

文章数量:1400174

I am rendering datawith thymeleaf attribute. But i am implementing "Search" button now, and want to do it without reload.

I have attribute depatments which render List<Department> from db I know, how to do it via ajax, but then i need to replace attribute with RestController, who will give me JSON. Is it posible to fetch data from attribute without reloading page? Ajax, or js, or something else? Thanks

I am rendering datawith thymeleaf attribute. But i am implementing "Search" button now, and want to do it without reload.

I have attribute depatments which render List<Department> from db I know, how to do it via ajax, but then i need to replace attribute with RestController, who will give me JSON. Is it posible to fetch data from attribute without reloading page? Ajax, or js, or something else? Thanks

Share Improve this question asked Feb 16, 2019 at 13:44 NeewbieNeewbie 691 gold badge3 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Yes, you can achieve this by using fragment 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;
    }

and in your search.html add bellow code. And don't forget to use 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>

本文标签: javascriptThymeleaf table update without page reloadStack Overflow