最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Thymeleaf table update without page reload - Stack Overflow

programmeradmin4浏览0评论

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>

发布评论

评论列表(0)

  1. 暂无评论