SpringBoot + Thymeleaf dinamic table with checkbox in row communicate with Controller - ajax

Maybe the title is more confusing than the problem it self. Here's my situation, with Thymeleaf I show a table with a list of clients. It shows Name, LastName, a button to delete it and a checkbox to display it's state (if the user is active or inactive)
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<tr th:each="client : ${clientList}">
<td th:text="${client.name}"></td>
<td th:text="${client.lastName}"></td>
<td>
<a th:href="#{/delete_client/} + ${client.id}" class="btn btn-danger">Delete</a>
</td>
<td>
<input type="checkbox" name="my-checkbox" th:checked="${client.state} ? 'checked'">
</td>
</tr>
</tbody>
So far, no problem. But what I don't know how to do is communcate the controller when the user click the checkboxes, in order to disable or enable that particular client.
Something like what the button does to delete the client, but to change it's state.
I was told to use AJAX, but don't know how.
Thank you!

You can avoid ajax. For deleting-alike effect you can apply following steps to the td element containing a checkbox:
wrap up the checkbox in the form tag
provide the proper action attribute in the form. The action is an endpoint which handles updating of a client's state (similarily to/delete_client/)
provide onclick="submit();" attribute in the checkbox, which will trigger mentioned endpoint when the user clicks it
E.g.
<td>
<form th:action="#{/switch_client_state/} + ${client.id}">
<input type="checkbox" name="my-checkbox" onclick="submit();" th:checked="${client.state} ? 'checked'">
</form>
</td>

Related

How to use multiple selectList in a same Visual Force page with repeat and access the onchange event of each selectList later

I use the below code to repeat the selectList and like to access the individual onchange event of each slectlist. But it allows us to only create a common onChange event. Please refer the screenshot below.
screenshot of what i am trying to achieve
Here I have 2 selectList added to this visual force page.
The number of selectList is known only when the page is getting loaded.
Now when I change anyone of the selectList it automatically changes the selected value of other selectList, since event="onchange" action="{!getRoleSelectionChange}" is same for all selectList. - As it is added only in apex:repeat
The templateRoles collection here is updated in the constructor of SampleController with a RestAPI call. So the size and data for the templateRoles will be unknown initaly and we will get it only when the page is loaded.
Sample code I use
<apex:page controller="SampleController">
<apex:form enctype="multipart/form-data">
<table>
<tbody>
<tr>
<td>
<apex:panelGrid style="padding: 64; text-align: center; background-color: #F2F2F2">
<apex:repeat var="templateRole" value="{!templateRoles}">
<apex:outputPanel title="Role" style="padding: 32">
<tr>
<td>
<table>
<tbody>
<tr>
<td>
<apex:outputText value="{!templateRole.name}" id="roleValue" />
</td>
<td>
<apex:selectList value="{!roleSelectedObject}" size="1">
<apex:selectOptions value="{!objList}" />
<apex:actionSupport event="onchange" action="{!getRoleSelectionChange}">
<apex:param name="roleName" value="{!templateRole.Name}" assignTo="{!mappedRoleName}"/>
</apex:actionSupport>
</apex:selectList>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</apex:outputPanel>
</apex:repeat>
</apex:panelGrid>
</td>
</tr>
</tbody>
</table>
</apex:form>
</apex:page>
I am trying to add mutiple selectList in a visual force page dynamically and do some changes based on the selection changes in the selectList. I also tried changing the design with apex:pageBlock but id doesnt work well for my case.

PageObject gem's handling of nested tables vs iframe

I am using the PageObject gems to test Salesforce which is chock full of tables within tables. I wanted to know if anyone has used a specific technique to access cells within nested tables (see example below).
I want to access THE LINK inside the cell with the cell labeled id="desired_item"
Thanks in advance.
<table id="bodyTable" class="outer">
<tbody>
<tr>
<td id="blah">
<div>
<table class="detailList">
<tbody>
<td>
<tr>
<td id="desired_item">
<a>Click_Me_Link</a>
</td>
</tr>
</td>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
Since the parent cell of the link has an ID, you can easily find that cell. From there, you simply get the first link in the cell.
page.cell_element(id: 'desired_item').link_element

post a combination value instead of many drop-down values

i am working on a project where a clerk like professional will make/edit classes time table for educational institute.
so in a day-period cell there are many drop-downs for selecting teacher, classroom/lab, batch of students, subject etc.
and this whole combination of values can be repeated many places in the grid.
so i want to post a single value combining all these 4-5 values through POST data in FORM for that cell location.
what approach should i use?
EDIT:
Oh! Yes, i am actually using PHP and angularjs and this is sample code in html
<table border="1" width="90%" height="90%" style="margin:10px">
<thead>
<tr>
<td>
Day
</td>
<td ng-repeat="hour in chours">
{{hour.name}}
<br>
{{hour.stime}}--{{hour.etime}}
<br>
<input type="checkbox" ng-model="hour.noclass" /> No class {{hour.noclass}}
</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="day in weekdays">
<td>
{{day.name}}
</td>
<td ng-repeat="hour in day.hours">
<div ng-show="hour.noclass=='false'" name="cell[{{day.name}}][{{hour.name}}][]">
<select name="cars">
<option ng-model="dd" ng-repeat="sub in subjects">{{sub.name}}</option>
</select>
{{dd}}
</div>
</td>
</tr>
</tbody>
<tfoot></tfoot>
</table>
and this is in js file
var App = angular.module('tteditApp',[]);
App.controller('editCtrl', function($scope){
$scope.chours=[
{'name':'1','stime':'800','etime':'855','noclass':'true'},
{'name':'2','stime':'855','etime':'950','noclass':'true'},
{'name':'3','stime':'800','etime':'855','noclass':'false'},
{'name':'4','stime':'800','etime':'855','noclass':'false'},
{'name':'5','stime':'800','etime':'855','noclass':'false'},
{'name':'6','stime':'800','etime':'855','noclass':'false'},
{'name':'7','stime':'800','etime':'855','noclass':'false'},
{'name':'8','stime':'800','etime':'855','noclass':'false'},
{'name':'9','stime':'800','etime':'855','noclass':'false'},
{'name':'10','stime':'800','etime':'855','noclass':'false'}];
$scope.subjects=[{'name':'CLOUD'},{'name':'CC'},{'name':'ISS'},{'name':'DCT'},{'name':'DMW'},{'name':'VLSI'},{'name':'SEMINAR'},{'name':'PROJECT'},{'name':'WEB DEV'}];
$scope.weekdays=[{'name':'Monday','hours':$scope.chours},{'name':'Tuesday','hours':$scope.chours},
{'name':'Wednesday','hours':$scope.chours},{'name':'Thursday','hours':$scope.chours},{'name':'Friday','hours':$scope.chours}
,{'name':'Saturday','hours':$scope.chours}];
});

How do you implement a generic CRUD grid using knockout.js?

I have a very nice grid with Create, Retrieve, Edit and Delete functionality. I am using knockout.js on the client and WebAPI on the back end.
I would like to extend this to a number of different objects. Essentially I have a List<T> where T is an MVC view model and the knockout view and view model should be able to work out what they should look like based on T.
Does anyone know of a simple way to display any viewmodel (with specific controls for editing based on the viewmodel itself - eg datepicker for dates, input for string, drop downs etc) in a grid format and have generic CRUD functions.
Below is an example of the exiting HTML I am using (for a specific object):
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Value Date</th>
<th>Position Date</th>
<th>Book</th>
<th>Currency</th>
<th>Currency Base</th>
<th>Amount1</th>
<th>Amount2</th>
<th>Position Type</th>
<th style="width: 100px; text-align:right;" />
</tr>
</thead>
<tbody data-bind=" template:{name:templateToUse, foreach: pagedList }"></tbody>
</table>
and the templates look like this:
<script id="itemsTmpl" type="text/html">
<tr>
<td data-bind="text: valueDate.formattedDate"></td>
<td data-bind="text: positionDate.formattedDate"></td>
<td data-bind="text: book"></td>
<td data-bind="text: currency"></td>
<td data-bind="text: currencyBase"></td>
<td data-bind="text: amount1"></td>
<td data-bind="text: amount2"></td>
<td data-bind="text: positionType"></td>
<td class="buttons">
<a class="btn" data-bind="click: $root.edit" href="#" title="edit"><i class="icon-pencil"></i></a>
<a class="btn" data-bind="click: $root.remove" href="#" title="remove"><i class="icon-trash"></i></a>
</td>
</tr>
</script>
<script id="editTmpl" type="text/html">
<tr>
<td><input data-bind="datepicker: valueDate.formattedDate, datepickerOptions: { dateFormat: 'yy/mm/dd' }"/></td>
<td><input data-bind="datepicker: positionDate.formattedDate, datepickerOptions: { dateFormat: 'yy/mm/dd' }"/></td>
<td><input data-bind="value: book"/></td>
<td><input data-bind="value: currency"/></td>
<td><input data-bind="value: currencyBase"/></td>
<td><input data-bind="value: amount1"/></td>
<td><input data-bind="value: amount2"/></td>
<td><input data-bind="value: positionType"/></td>
<td class="buttons">
<a class="btn btn-success" data-bind="click: $root.save" href="#" title="save"><i class="icon-ok"></i></a>
<a class="btn" data-bind="click: $root.cancel" href="#" title="cancel"><i class="icon-remove"></i></a>
</td>
</tr>
</script>
I would like the View as well as the View Model to be generic and not "hard-coded" as above. I am sure someone else must have done something like this.
One solution would be that:
AJAX call gets a list of JSON viewmodels
If list is empty then do not display anything
If list has items, pick first item and go through properties
Start constructing the view on the client by looping through proprties
Append the string as a DOM element to the main DIV
Wireup knockout
There are problems with this approach. What if a property is null for the first object but exists in another? If so we do not setup the element for it.
A better option is to use content negotiation to get a dedicated template:
So GET /api/customers will return customers but if you request text/knockout-template+html then you get back a knockout template as string and then you append to DIV and wireup knockout. So server can generate template using reflection or customise for some models.

Spring MVC Table selected row

in a Spring MVC 2.5 application i'm using a spring <form:form> component and in this form a table with c:forEach is rendered.
In each row a submit button is placed in. If i start a submit by clicking a button i would like to knwo, which button has processed the submit.
<form:form commandName="formObject">
<table class="data-table" cellpadding="1" cellspacing="1" border="0">
<thead>
<tr>
</tr>
</thead>
<c:forEach items="${list}" var="document" varStatus="row">
<tr>
<td>
<input type="submit" value="${document.title}"/>
<td>
</tr>
</c:forEach>
</table>
</form:form>
THX.
Give the submit button a name. You can handle the request parameter from that button like any other parameter. Some browser might handle the value they give differently though.
<input name="submittype" type="submit" value="${document.title}" />

Resources