I am creating an editable JQuery data-table from the model list . I want to edit some of the column [Rate, Qty, IsBranded, Description] of each record listed in a table. My code is given below.
ProductModel
Id int
Name string
Rate decimal
Qty int
Price decimal
Description string
Html and Javascript
<script type="text/javascript">
$("document").ready(function () {
$('#tbllist').DataTable();
});
</script>
#model List<Product>
<table id="tbllist" class="cell-border" style="width:100%">
<thead class="thead-light">
<tr>
<td>Name</td>
<td>Rate</td>
<td>Qty</td>
<td>total</td>
<td>IsBranded</td>
<td>Description</td>
</tr>
</thead>
<tbody>
#if (Model != null)
{
for (var i = 0; i < Model.Count; i++)
{
<tr>
<td>#Model[i].Name</td>
<td>#Model[i].Rate</td>
<td>#Model[i].Qty</td>
<td>#Model[i].total</td>
<td><input type="checkbox" #(Model[i].IsBranded ? "checked" : "") /></td>
<td>#Model[i].Description</td>
</tr>
}
}
</tbody>
</table>
I want to make edit Rate,Qty, Description, IsBranded column. It would be very appreciated , if someone can help me with appropriate code to make .
With Thanks
Alan
I made an example based on #StéphaneLaurent comment, hope it can work for you.
Copy the dataTables.cellEdit.js to your project, you can place it under wwwroot/js
Reference it in your page
<script src="~/js/dataTables.cellEdit.js"></script>
Then follow the tutorial.
#model List<ProductModel>
<table id="tbllist" class="cell-border" style="width:100%">
<thead class="thead-light">
<tr>
<td>Name</td>
<td>Rate</td>
<td>Qty</td>
<td>Total</td>
<td>IsBranded</td>
<td>Description</td>
</tr>
</thead>
<tbody>
#if (Model != null)
{
for (var i = 0; i < Model.Count; i++)
{
<tr>
<td>#Model[i].Name</td>
<td>#Model[i].Rate</td>
<td>#Model[i].Qty</td>
<td>#Model[i].Total</td>
<td><input type="checkbox" #(Model[i].IsBranded ? "checked" : "") /></td>
<td>#Model[i].Description</td>
</tr>
}
}
</tbody>
</table>
#section scripts{
<script src="https://cdn.datatables.net/1.10.24/js/jquery.dataTables.min.js"></script>
<script src="~/js/dataTables.cellEdit.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.24/css/jquery.dataTables.min.css" />
<script type="text/javascript">
var table = $('#tbllist').DataTable();
function myCallbackFunction(updatedCell, updatedRow, oldValue) {
console.log("The new value for the cell is: " + updatedCell.data());
console.log("The values for each cell in that row are: " + updatedRow.data());
}
table.MakeCellsEditable({
"onUpdate": myCallbackFunction
});
</script>
}
Result:
Related
I´m trying to create an HTML table which get´s data from an array that is being passed from request.gs to template.html.
My current setup is:
request.gs (partial snippet):
var products = tempSheet.getRange("B2:B").getValues();
var template = HtmlService.createTemplateFromFile('template');
template.products = products;
var emailToSend = template.evaluate().getContent();
MailApp.sendEmail({
to: Session.getActiveUser().getEmail(),
subject: 'test mail ' + Utilities.formatDate(new Date(), "GMT+2", "dd.MM.yyyy"),
htmlBody: emailToSend
});
template.html (partial snippet):
<table>
<tr>
<th>Product Name</th>
</tr>
<tr>
<td>placeholder</td>
</tr>
</table>
I do have access to the products variable within the template.html using <?= products ?>.
Where my current knowledge and tries ended so far is how to add table rows and the data of products in each of the rows.
Here is how to add rows dynamically to the template. However I suggest you filter out blank rows first.
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<table>
<tr>
<th>Product Name</th>
</tr>
<? for (var i = 0; i < products.length; i++) { ?>
<tr>
<td><?= products[i][0] ?></td>
</tr>
<? } ?>
</table>
</body>
</html>
Reference
Templated HTML
It will be something like:
<table>
<tr>
<th>Product Name</th>
</tr>
<? for (var i = 0; i < data.length; i++) { ?>
<tr>
<td><?= products[i] ?></td>
</tr>
<? } ?>
</table>
<table id="table" class="table table-bordered font" style="width: 100%; padding-top:10px;">
<thead>
<tr class="bg-primary textalign">
<th>Details</th>
<th>SonVin Id</th>
<th>Date</th>
<th>Venue Name</th>
<th>City</th>
<th>Area</th>
<th>Amount</th>
<th><input type="checkbox" ng-model="checkall" /><span style="margin-left:10px;">Add Invoice</span></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="data in getdataintable">
<td>Details</td>
<td>{{data.sonvinid}}</td>
<td>{{data.date}}</td>
<td>{{data.venuename}}</td>
<td>{{data.location}}</td>
<td>{{data.area}}</td>
<td>{{data.amount}}</td>
<td><input type="checkbox" ng-model="check" /></td>
</tr>
</tbody>
</table>
This is my table,
here I have this line( <th><input type="checkbox" ng-model="check" /><span style="margin-left:10px;">Add All Invoice</span></th>).
This indicates that if a user checked this checkbox, all the other checkboxes should also be checked.
Now what I want is if a particular user clicks on a checkbox on thead, all the checkboxes of td also be check and all the data of <td>{{data.sonvinid}}</td>
should be stored in the array in my controller of angularjs.
$scope.storeall=[];
I want to do this with angularjs,
anyone here who can help me?
make inside your controller some methods like this
method to select All
method for deselect All
method for toggle between above
var app = angular.module('app',[]) // the app module
app.controller('tableCtrl',function($scope){
$scope.storeall = [];
$scope.selectAllFlag = false;
$scope.getdataintable = [
{sonvinid:1, date : "string" , venuename: "String Name" },
{sonvinid:2, date : "string" , venuename: "String Name" }
];
$scope.checkAllToggle = funciton(){
if(!$scope.selectAllFlag){
$scope.selectAll();
$scope.selectAllFlag = true;
}else {
$scope.deselectAll();
$scope.selectAllFlag = false;
}
};
$scope.selectAll = function(){
for (var i = 0 ; i < $scope.data.length ; i++){
$scope.getdataintable[i].selected = true;
$scope.storeall.push($scope.data[i].sonvinid);
}
};
$scope.deselectAll = function(){
for (var i = 0 ; i < $scope.data.length ; i++){
$scope.getdataintable[i].selected = false;
$scope.storeall = [];
}
};
});
to Use it on HTML:
<table id="table" class="table table-bordered font" ng-controller="tableCtrl" style="width: 100%; padding-top:10px;">
<thead>
<tr class="bg-primary textalign">
<th>Details</th>
<th>SonVin Id</th>
<th>Date</th>
<th>Venue Name</th>
<th>City</th>
<th>Area</th>
<th>Amount</th>
<th><input type="checkbox" ng-model="checkAllToggle()" /><span style="margin-left:10px;">Add Invoice</span></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="data in getdataintable">
<td>Details</td>
<td>{{data.sonvinid}}</td>
<td>{{data.date}}</td>
<td>{{data.venuename}}</td>
<td>{{data.location}}</td>
<td>{{data.area}}</td>
<td>{{data.amount}}</td>
<td><input type="checkbox" ng-checked="data.selected" /></td>
</tr>
</tbody>
</table>
I need to vertically populate data cells with array.
This is a simple html table
<table>
<th>Name</th>
<th>Value</th>
</table>
Existing table:
Name Value
X
Y
Expected output:
Name Value
X 54
Y 48
I need to fill the data cells for value. I have calculated values for it and stored in array. How to populate the values using angularjs?
You want to use ngRepeat to add a new row for each element in your list. See the docs here for examples of how to use this directive.
<tr ng-repeat="row in data">
<td>{{row.name}}</td>
<td>{{row.value}}</td>
</tr>
Use directive for this. Look
var app = angular.module("App", []);
app.controller("AppController", function($scope) {
$scope.data = [1, 2, 3, 4, 5];
}).directive('indexItemTable', function($timeout) {
return {
link: function(scope, element, attrs) {
var data = scope.data;
angular.element(element).find("td").each(function(index, item){
console.log(item)
angular.element(this).parent().append('<td>' + data[index] + '</td>');
});
}
};
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app="App" ng-controller="AppController">
<table index-item-table>
<thead>
<th>Name</th>
<th>Value</th>
</thead>
<tbody>
<tr>
<td>A</td>
</tr>
<tr>
<td>B</td>
</tr>
<tr>
<td>C</td>
</tr>
<tr>
<td>D</td>
</tr>
<tr>
<td>E</td>
</tr>
</tbody>
</table>
</div>
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);
}
I have a HTML table with a checkbox in one of the columns. I want to know how I could get the row data when the user clicks on the checkbox with javascript (without jquery)? Can anyone please help me with this?
Thanks
HTML DOM solves your problem
<script type="text/javascript">
function getRow(n) {
var row = n.parentNode.parentNode;
var cols = row.getElementsByTagName("td");
var i=0;
while (i < cols.length) {
alert(cols[i].textContent);
i++;
}
}
</script>
<table>
<tr>
<td><input type="checkbox" onclick="getRow(this)" /></td>
<td>aaa</td>
<td>bbb</td>
</tr>
<tr>
<td><input type="checkbox" onclick="getRow(this)" /></td>
<td>ccc</td>
<td>ddd</td>
</tr>
<tr>
<td><input type="checkbox" onclick="getRow(this)" /></td>
<td>eee</td>
<td>fff</td>
</tr>
</table>
EDIT:
this script will help you more, I think:
function getRow(n) {
var row = n.parentNode.parentNode;
var cols = row.getElementsByTagName("td");
var i = 0;
while (i < cols.length) {
var val = cols[i].childNodes[0].nodeValue;
if (val != null) {
alert(val);
}
i++;
}
}
You could try something like this:
HTML:
<table>
<thead>
<tr><th></th><th>Row Text</th></tr>
</thead>
<tr>
<td><input type="checkbox" /></td>
<td>Test</td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>Test 2</td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>Test 3</td>
</tr>
</table>
JavaScript:
checkboxes = document.getElementsByTagName("input");
for (var i = 0; i < checkboxes.length; i++) {
var checkbox = checkboxes[i];
checkbox.onclick = function() {
var currentRow = this.parentNode.parentNode;
var secondColumn = currentRow.getElementsByTagName("td")[1];
alert("My text is: " + secondColumn.textContent );
};
}
JSFiddle: http://jsfiddle.net/markwylde/wzPHF/1/
if your select is in td directly, try the following:
sel.onclick = function(){
row = this.parentNode.parentNode
//then what you need
}
Note that first you have to find sel with either document.getElementById() or document.getElementsByTagName()
Also you may need to handle onchange event instead of onclick
This will give you content of row directly(all td in tr)
HTML:
<table>
<tr id="tr1">
<td>
<input type="checkbox" value="chkbox1" id="chk1">
</td>
<td>
Lorem ipsum text
</td>
</tr>
<tr id="tr2">
<td>
<input type="checkbox" value="chkbox2" id="chk2">
</td>
<td>
Lorem ipsum text
</td>
</tr>
</table>
Jquery:
$(document).ready(function(){
$('#chk1, #chk2').on('change',function(){
if($('#chk1').prop('checked') || $('#chk2').prop('checked'))
{
var ids=$(this).parent().parent().html();
alert(ids);
}
else
{
}
});
})
if you're new :
onChange EVENT of the checkbox will call the function that i named "checking"
it will send "This.checked", which means, will send True or false to the function "checking", Then i go in the function get that True or False that was sent, and put it in a variable i called "temp".
HTML: onchange="checking(this.checked)" <--- sending out : "This.checked" ( This = the object that creates the Event onchange, and .checked, is the propriety)
you could even send multiple info like this : onchange="checking(this.checked,this.id)" and so on.
JAVASCRIPT :function checking(temp) <--- this is how it gets the "this.checked" in HTML, and put it in the variable temp.
to receive multiple infos : function checking(temp,temp2) and so on.(name it like you want)
then i run a 'IF' with the variable "temp" and if the value of it = true, then do alert.
Hope it helps, think about it and work it so it fits what you need !
HTML :
<table>
<tr>
<td>
<input type="checkbox" value=""onchange="checking(this.checked)">
</td>
<td>
<label>Something Here</label>
</td>
</tr>
</table>
JAVASCRIPT :
function checking(temp)
{
if(temp == true)
{
alert("Checkbox is checked");
}
else
{
alert("Checkbox is NOT checked");
}
}