Send row info to angularjs controller when checkbox in row is clicked - html

I have some html code which uses angularjs to show a table. On this table, there is a checkbox on each row.
The table looks like this;
Here is the html code.
<div ng-app ng-controller="CheckCtrl">
<table class="table table-hover data-table sort display" style="width:100%">
<thead>
<tr>
<th class="Serial_">
Serial
</th>
<th class="Name_">
Name
</th>
<th class="ID_">
ID
</th>
<th class="On_off_">
On/off
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in check_items">
<td>{{item.SERIAL}}</td>
<td>{{item.NAME}}</td>
<td>{{item.ID}}</td>
<td> <input type="checkbox" ng-checked="item.ON_OFF == '1'"></td>
</tbody>
</table>
</div>
Here is my angularjs controller code.
controller('CheckCtrl', ['$scope', '$http', 'configuration',
function ($scope, $http, $configuration) {
var url_api = $configuration.host + "cloe/webroot/cloe-cloud/app/API.json";
$http.get(url_api).success(function(data)
{
$scope.check_items = data;
});
This is what I want. When a user clicks on a checkbox, information regarding all the items on that particular row belonging to the clicked checkbox is sent to a angualrjs controller function for processing. This information should include the Serial, Name, ID and new state of On/Off after clicking on the checkbox.
I am using angularjs v1 and twitter bootstrap.
EDIT: Edited the original question details to indicate that the row information should be sent whenever the checkbox is clicked and not only when the checkbox is enabled.

We can use ng-change to send row data to controller as object.
After that, you can retrieve row data as following:
var serial = row.SERIAL;
var name = row.NAME;
var id = row.ID;
var onOff = row.ON_OFF;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="CheckCtrl">
<table class="table table-hover data-table sort display" style="width:100%">
<thead>
<tr>
<th class="Serial_">
Serial
</th>
<th class="Name_">
Name
</th>
<th class="ID_">
ID
</th>
<th class="On_off_">
On/off
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in check_items">
<td>{{item.SERIAL}}</td>
<td>{{item.NAME}}</td>
<td>{{item.ID}}</td>
<td> <input type="checkbox" ng-checked="item.ON_OFF == '1'" ng-click="rowSelected(item)"></td>
</tbody>
</table>
</div>
<script>
var app = angular.module('app',[]);
app.controller('CheckCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.check_items =
[
{
SERIAL:'Test Serial',
NAME:'Test Name',
ID : 10,
ON_OFF : '1'
}
];
$scope.rowSelected = function(row)
{
console.log(row);
};
}]);
</script>

You can maybe use something like this,
HTML:
<input type="checkbox" ng-checked="item.ON_OFF == '1'" ng-change="checkSubmit(item.serial,item.name,item.id,item.on_off)">
JS:
function checkSubmit(serial,name,id,on_off){
...
/*Processing can happen now with the parameters obtained*/
...}
NOTE: The case of the variables may be wrong

Some modification to the answer provided by Natiq.
Modify this line
<td> <input type="checkbox" ng-checked="item.ON_OFF == '1'" ng-model="item.selected" ng-change="rowSelected(item)"></td>
to
<td> <input type="checkbox" ng-checked="item.ON_OFF == '1'" ng-model="item.selected" ng-click="rowSelected(item)"></td>
The modification is from ng-change to ng-click. This will solve your problem where the first click does not work but only subsequent clicks work based on the comments posted. Using ng-click ensures that every click will be detected.

Related

Footable and Validation error message in Mobile view MVC

In my MVC 5 Web App I use footable. In a table I have fields radio buttons for example that are built the form.
here is the code:
<thead>
<tr>
<th></th>
#foreach (var labelfreqdep in ViewBag.rbFreqDep)
{
<th style="text-align:center;" data-breakpoints="xs sm">
<label for="FrequencyID">#labelfreqdep.FrequencyDescription</label>
</th>
}
<th data-breakpoints="xs sm"></th>
</tr>
</thead>
<tbody>
<tr>
<td>First Chooses</td>
#foreach (var freqdep in ViewBag.rbFreqDep)
{
<td class="rd">
<input type="radio" name="A3_1" id="A3_1" value="#freqdep.FrequencyDescription" data-val="true" data-val-required="Please select">
</td>
}
<td class="rd">#Html.ValidationMessageFor(model => model.A3_1, "", new { #class = "text-danger" })</td>
</tr>
</tbody>
</table>
I also use validation for empty fields with data-val="true". The problem is that doesn't display the "Please select" message in mobile view. It works ok for desktop view. When I have expanded the tr with the plus icon the message appears. Can we just expand the rows on submit so the message to be displayed?
Any idea?
Edited
I used
$("#survey_form").submit(function (event) {
$('.surveytable').footable({
"expandAll": true
});
});`
It expands the rows on submit but it doesn't show the validation errors
For anyone who has the same issue, I solved it by giving an id to the button and then handle it through jquery
<script type="text/javascript">
jQuery(function($){
$('.surveytable').footable()
$("#survey_btn").click(function (event) {
$('.surveytable').footable({
"expandAll": true
});
});
});
</script>

Show text beside checkbox depending on whether it is checked or unchecked

Currently, I have a angularjs table that looks like this.
Here is the code;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="CheckCtrl">
<table class="table table-hover data-table sort display" style="width:100%">
<thead>
<tr>
<th class="Serial_">
Serial
</th>
<th class="Name_">
Name
</th>
<th class="ID_">
ID
</th>
<th class="On_off_">
On/off
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in check_items">
<td>{{item.SERIAL}}</td>
<td>{{item.NAME}}</td>
<td>{{item.ID}}</td>
<td> <input type="checkbox" ng-checked="item.ON_OFF == '1'" ng-click="rowSelected(item)"></td>
</tbody>
</table>
</div>
<script>
var app = angular.module('app',[]);
app.controller('CheckCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.check_items =
[
{
SERIAL:'Test Serial',
NAME:'Test Name',
ID : 10,
ON_OFF : '1'
}
];
$scope.rowSelected = function(row)
{
console.log(row);
};
}]);
</script>
I would like the text "On" to appear beside each checkbox in each row whenever it is checked. The text "Off" will appear beside each checkbox in each row whenever it is unchecked.
I am using angularjs v1.
<td> <input type="checkbox" ng-checked="item.ON_OFF == '1'" ng-click="rowSelected(item)">{{item.ON_OFF == 1 ? 'On' : 'Off'}}</td>
You can do this if you have AngularJS 1.1.5 or more

Show hyperlink or text beside angularjs checkbox in each table row depending on checkbox status

I have an angularjs table that looks like this:-
Here is the code below:-
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="CheckCtrl">
<table class="table table-hover data-table sort display" style="width:100%">
<thead>
<tr>
<th class="Serial_">
Serial
</th>
<th class="Name_">
Name
</th>
<th class="ID_">
ID
</th>
<th class="On_off_">
On/off
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in check_items">
<td>{{item.SERIAL}}</td>
<td>{{item.NAME}}</td>
<td>{{item.ID}}</td>
<td> <input type="checkbox" ng-checked="item.ON_OFF == '1'" ng-click="rowSelected(item)"></td>
</tbody>
</table>
</div>
<script>
var app = angular.module('app',[]);
app.controller('CheckCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.check_items =
[
{
SERIAL:'Test Serial',
NAME:'Test Name',
ID : 10,
ON_OFF : '1'
}
];
$scope.rowSelected = function(row)
{
console.log(row);
};
}]);
</script>
When the checkbox is unchecked. I want a hyperlink text "Link" containing the URL 127.0.0.1/{{item.SERIAL}} to appear beside the checkbox. When the checkbox is checked an ordinary text "Checked" will appear beside the checkbox.
I am using angularjs v1.
Try this,
Should change the ON_OFF value while click on check box.
$scope.rowSelected = function(row)
{
row.ON_OFF = (row.ON_OFF=='1')?'0':'1';
};
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="CheckCtrl">
<table class="table table-hover data-table sort display" style="width:100%">
<thead>
<tr>
<th class="Serial_">
Serial
</th>
<th class="Name_">
Name
</th>
<th class="ID_">
ID
</th>
<th class="On_off_">
On/off
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in check_items">
<td>{{item.SERIAL}}</td>
<td>{{item.NAME}}</td>
<td>{{item.ID}}</td>
<td> <input type="checkbox" ng-checked="item.ON_OFF == '1'" ng-click="rowSelected(item)">
<a ng-show="item.ON_OFF=='0'" ng-href="127.0.0.1/{{item.SERIAL}}">LINK</a>
<span ng-show="item.ON_OFF=='1'">CHECKED</span>
</td>
</tbody>
</table>
</div>
<script>
var app = angular.module('app',[]);
app.controller('CheckCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.check_items =
[
{
SERIAL:'Test Serial',
NAME:'Test Name',
ID : 10,
ON_OFF : '1'
}
];
$scope.rowSelected = function(row)
{
row.ON_OFF = (row.ON_OFF=='1')?'0':'1';
};
}]);
</script>
You can add conditional span checking value of the ON_OFF model like below:
<td>
<input type="checkbox" ng-checked="item.ON_OFF == '1'" ng-click="rowSelected(item)">
<span ng-if="item.ON_OFF == '1'">Checked</span>
<span ng-if="item.ON_OFF != '1'"><a ng-href="127.0.0.1/{{item.SERIAL}}">Link</a></span>
</td>

Change this number character into a checkbox inside a table on angularjs

I have this html code which uses angularjs to display contents in a table.
<div ng-controller="CheckCtrl">
<table class="table table-hover data-table sort display">
<thead>
<tr>
<th class="Serial_">
Serial
</th>
<th class="Name_">
Name
</th>
<th class="ID_">
ID
</th>
<th class="On_off_">
On/off
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in check_items">
<td>{{item.SERIAL}}</td>
<td>{{item.NAME}}</td>
<td>{{item.ID}}</td>
<td>{{item.ON_OFF}}</td>
</tbody>
</table>
</div>
The webpage looks like this;
Here is the controller code.
.controller('CheckCtrl', ['$scope', '$http', 'configuration',
function ($scope, $http, $configuration) {
var url_api = $configuration.host + "cloe/webroot/cloe-cloud/app/API.json";
$http.get(url_api).success(function(data)
{
$scope.check_items = data;
});
I would like to change the On/Off columns number character into a checkbox. If the number character is '0', the checkbox is unchecked. If the number character is '1', the checkbox is checked.
I am using angularjs v1 and twitter bootstrap.
EDIT: Sorry, I realized that my checkbox value is a character, not a number. They are '0', '1' and not 0, 1.
function myCtrl($scope) {
$scope.check_items = [ {
'SERIAL':12345,
'NAME': 'ANil Kumar Ram',
'ID' : 1,
'ON_OFF': '1'
},{
'SERIAL':6453,
'NAME': 'Rohan Ram',
'ID' : 2,
'ON_OFF': '0'
},{
'SERIAL':732,
'NAME': 'Sunil Ram',
'ID' : 3,
'ON_OFF': '0'
},{
'SERIAL':1261,
'NAME': 'Ram Jitesh',
'ID' : 4,
'ON_OFF': '1'
}]
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="myCtrl">
<table class="table table-hover data-table sort display" style="width:100%">
<thead>
<tr>
<th class="Serial_">
Serial
</th>
<th class="Name_">
Name
</th>
<th class="ID_">
ID
</th>
<th class="On_off_">
On/off
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in check_items">
<td>{{item.SERIAL}}</td>
<td>{{item.NAME}}</td>
<td>{{item.ID}}</td>
<td> <input type="checkbox" ng-checked="item.ON_OFF == '1'"></td>
</tbody>
</table>
</div>
Hope this will help.
<input type='checkbox' ng-checked='{{item.ON_OFF == 1}}'>
Think that should do it.
<input type="checkbox" ng-model="{{item.ON_OFF}}" ng-true-value="1" ng-false-value="0">
ng-true-value the value that will set the input checked
ng-false-value="0" the value that will set the input unchecked
Angular Documentation
https://docs.angularjs.org/api/ng/input/input%5Bcheckbox%5D

How to bind Knockout model data to Multiple tables

I am using knockout binding to bind some data into html tables. My knockout view Model had multiple products and each product will have multiple chars. I want to display the products in one table and when i select the link "show chars" it should display the corresponding chars in below table.
This is my View Model
var ProductViewModel = function(items) {
this.items = ko.observableArray(items);
this.itemToAdd = ko.observable("");
this.addItem = function() {
if (this.itemToAdd() != "") {
this.items.push(this.itemToAdd());
this.itemToAdd("");
}
}.bind(this);
};
And this is my html tables
<div id="productTable">
<table class="ui-responsive table">
<thead>
<tr>
<th >Product Name</th>
<th >Description</th>
<th >Parent?</th>
</tr>
</thead>
<tbody id="pBody" data-bind="foreach: items">
<tr class="success" >
<td><span data-bind="text: name"></span>
</td>
<td><span data-bind="text: desc"></span>
</td>
<td>show chars</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="productChars">
<div id="productCharTable">
<table class="ui-responsive table">
<thead>
<tr>
<th >Char Name</th>
<th >Description</th>
<th >Length</th>
<th >Type</th>
</tr>
</thead>
<tbody id="pBody" data-bind="foreach: $data['chars']">
<tr class="success">
<td><span data-bind="text: name"></span>
</td>
<td>asdf asdfasdf</td>
<td>10</td>
<td>String</td>
</tr>
</tbody>
</table>
</div>
I am able to bind the products into first table. But for characteristics i am not sure how to achieve the same.
Could someone please help me in figuring out how to achieve the same.
Here is the jsfiddle
https://jsfiddle.net/sirisha_k/0Ln7h2bo/7/
As #supercool pointed out you can use "data-bind='with selectedItem'" to populate the second table with chars data. For that you need to add one more item into your model called selectedItem and every time you select or add a row, you point the selectedItem to that elementdata. And use "data-bind='with selecteItem'" for second table.
var ProductViewModel = function(items) {
this.items = ko.observableArray(items);
this.selectedItem = ko.observableArray();
this.itemToAdd = ko.observable("");
this.addItem = function() {
if (this.itemToAdd() != "") {
this.items.push(this.itemToAdd());
this.itemToAdd("");
}
}.bind(this);
};
and on row select call some function selectedItem($data) where $data refers to the current item.
then set that data to selectedItem in model.
function selectedItem(prod){
ProductViewModel.selectedItem(prod);
}