Hide and Show 'ng-repeat' Table Row Using Contextual Menu - css

I am creating a table adding row dynamically into the table. I want to hide selected row by providing a contextual menu.
I have created the html and model to show contextual menu but I don't how to call double click. I am able to create a contextual menu with this but now able to pass the selected row index into the function so that i can use that index to show or hide.
Data in table row is of 2 type, if i am getting data from backend then I am displaying that data in row but data not present then I am adding input element in cell of the table row. So I want to create 2 different contextual menu to work on 2 different rows one with data from database and other one with input textbox.
I want to add contextual menu on Row with background color grey which has hide option and the contextual menu on row with background will add that row that to other page.
HTML
<table id="phaseTable" class="table table-bordered">
<thead id="phaseHead">
<tr>
<th id="phase" scope="col" class="textSize grey t-space th-border">{{'eox.label.phases.phase'| localize}}</th>
<th id="description" scope="col" class="textSize grey th-border">{{'eox.label.phases.description'| localize}}</th>
<th id="knowHow" scope="col" class="textSize grey th-border">{{'eox.label.phases.how'| localize}}</th>
</tr>
</thead>
<tbody id="phaseBody">
<tr context-menu data-target="phase-row-hide" data-ng-repeat="pointCle in jes.listePointCle" id="{{$parent.$id+'-'+$index}}"
data-ng-click="setRowSelected($parent.$id,$index)">
<td id="phaseData" nowrap="nowrap" align="center" class="textSize grey t-space td-border"
data-ng-if="pointCle.type.length > 0">{{pointCle.type}}
</td>
<td id="phaseData" nowrap="nowrap" align="center" class="t-space td-border"
data-ng-if="pointCle.type == undefined || pointCle.type.length == 0 || pointCle.type.length == ''">
<input type="text" name="phaseData" maxlength="10" size="5" value="100" style="text-align: center;" class="input-how">
</td>
<td id="descriptionData" class="textSize grey t-space td-border"
data-ng-if="pointCle.designation.length > 0">{{pointCle.designation}}
</td>
<td id="descriptionData" class="t-space td-border"
data-ng-if="pointCle.designation == undefined || pointCle.designation.length == 0 || pointCle.designation.length == ''">
<input id="descriptionData{{$index}}"type="text" name="descriptionData" maxlength="50" size="50" value="OC BLA BLA" class="input-how"
data-ng-keypress="addRowPhaseOnPress($index)">
</td>
<td id="knowHowData" class="textSize grey t-space td-border"
data-ng-if="pointCle.risque.length > 0">{{pointCle.risque}}
</td>
<td id="knowHowData" class="t-space td-border"
data-ng-if="pointCle.risque == undefined || pointCle.risque.length == 0 || pointCle.risque.length == ''">
<input type="text" name="knowHowData" id="knowHowData" size="50" maxlength="50" class="input-how">
</td>
</tr>
</tbody>
</table>
Model
<div class="position-fixed" id="phase-row-hide">
<ul class="dropdown-menu" role="menu">
<li>
<a class="pointer" role="menuitem" tabindex="1" data-ng-click="setRowSelected()">
Hide Row
</a>
</li>
</ul>
JS - Code to select the row
$scope.setRowSelected = function(id,index){
alert('id = '+id);
alert('index = '+index);
alert('rowId = '+id+'-'+index);
$scope.selectedRow = index;
}
Screen Display

Context menu directive:
app.directive("contextMenu", function() {
return {
link: postLink
};
function postLink(scope, elem, attrs) {
elem.on("contextmenu", function (e) {
scope.$apply(function() {
var locals = {$event: e, $scope: scope, $element: elem};
scope.$eval(attr.contextMenu, locals);
});
});
}
})
Usage:
<tr context-menu="onContext($event, $index)" ng-repeat="...
$scope.onContext = function(ev, index) {
ev.preventDefault();
console.log(index);
//...
};
For more information, see
MDN Web API Reference - contextmenu Event

Related

Checkbox not being rendered with correct gentelella css when added via javascript

I'm trying to add some rows data in a datatable, first via Pug and then later on via javascript.
I add the first rows getting data via controller call, and then adding it via pug parse:
<div class="x_content">
<table id="datatable-buttons" class="table table-striped table-bordered bulk_action">
<thead>
<tr>
<th>
<th>Aprovado</th>
</th>
<th>Nome</th>
<th>Email</th>
<th>Data de nascimento</th>
<th>Cidade</th>
<th>Gênero</th>
<th>Escolaridade</th>
<th>Instituição de ensino</th>
<th>Semestre</th>
<th>Token</th>
<th>Selection Status</th>
</tr>
</thead>
<tbody>
for candidate in candidates
- var formatedCandidate = candidateService.formatCandidateForDataTable(candidate, newTrueCheckbox, newFalseCheckbox);
<tr>
<td>
<th>
if formatedCandidate.isCandidateApproved
<input type="checkbox" id="check-all" class="flat" checked>
else
<input type="checkbox" id="check-all" class="flat">
</th>
</td>
<td>
= formatedCandidate.name
</td>
<td>
= formatedCandidate.email
</td>
<td>
= formatedCandidate.bornDate
</td>
<td>
= formatedCandidate.city
</td>
<td>
= formatedCandidate.gender
</td>
<td>
= formatedCandidate.educationLevel
</td>
<td>
= formatedCandidate.educationInstitution
</td>
<td>
= formatedCandidate.educationSemester
</td>
<td>
= formatedCandidate.token
</td>
<td>
= formatedCandidate.selectionStatus
</td>
</tr>
</tbody>
</table>
This way, it manages to render the checkbox with the right style:
Later on, I try to update the rows via a "reload" button, getting data via a get request and then adding rows via javascript:
$.get("candidates/getCandidatesForTable", function(candidatesArray){
candidateTable.clear().draw();
candidatesArray.forEach((candidate, index) => {
debugger;
if (candidate[1]) {
candidate[1] = `<th>
<input type="checkbox" id="check-all" class="flat" checked/>
</th>`;
} else {
candidate[1] = `<th>
<input type="checkbox" id="check-all" class="flat"/>
</th>`;
}
candidateTable.row.add(candidate).draw();
});
});
Although I inserted the HTML code via javascript using the same properties as it was added via pug, the checkbox was rendered different: it is rendered, but the css style is now gone:
How should I add the a checkbox via javascript and still get the correct css styling?

How to group table cell together so we can hide them with one statement? (webforms)

I have a table with 2 set of cells in the first row each set containing 5 cells. Each cell contains controls inside them.
My issue is, if a certain condition is true, I need to hide one set and show the other and vice versa when the condition is false. Currently I have 10 .Visible = statements in my code for true part, and 10 for the false part. Is there any way to group one set of cells together so that hiding the group can hide all 5? I have to do it all in server-side code, no jQuery.
<table>
<tr>
<!-- first set -->
<td runat="server" id="set1_cell1"> content here</td>
<td runat="server" id="set1_cell2"> content here</td>
<td runat="server" id="set1_cell3"> content here</td>
<td runat="server" id="set1_cell4"> content here</td>
<td runat="server" id="set1_cell5"> content here</td>
<!-- end first set -->
<!-- second set -->
<td runat="server" id="set2_cell1"> content here</td>
<td runat="server" id="set2_cell2"> content here</td>
<td runat="server" id="set2_cell3"> content here</td>
<td runat="server" id="set2_cell4"> content here</td>
<td runat="server" id="set2_cell5"> content here</td>
<!-- end second set -->
</tr>
...
</table>
This is how my current code looks like
if (condition is true)
{
set1_cell1.Visible = true;
set1_cell2.Visible = true;
set1_cell3.Visible = true;
set1_cell4.Visible = true;
set1_cell5.Visible = true;
set2_cell1.Visible = false;
set2_cell2.Visible = false;
set2_cell3.Visible = false;
set2_cell4.Visible = false;
set2_cell5.Visible = false;
}
else
{
// opposite of the above
}
I would love to replace those 10 statements with just one.
You can give a different class name to the cells of each group:
<table>
<tr id="row1" runat="server">
<td class="set1">Content 1a</td>
<td class="set1">Content 1b</td>
<td class="set1">Content 1c</td>
<td class="set1">Content 1d</td>
<td class="set1">Content 1e</td>
<td class="set2">Content 2a</td>
<td class="set2">Content 2b</td>
<td class="set2">Content 2c</td>
<td class="set2">Content 2d</td>
<td class="set2">Content 2e</td>
...
</tr>
...
</table>
In code-behind, you show/hide the cells according to the class name and the value of the condition:
foreach (HtmlTableCell cell in row1.Cells)
{
string className = cell.Attributes["class"];
if (className == "set1")
{
cell.Visible = condition;
}
if (className == "set2")
{
cell.Visible = !condition;
}
}
Note 1: The class name could also be used to do the same on the client side if you want to (especially with jQuery).
Note 2: I use the class name in the code above but you could get the same result with a custom attribute (e.g. data-group="set1" instead of class="set1", with the corresponding change in code-behind).

Can not data bind to table using knockout

//AJAX Webservice Call
ManualRegDiseaseData = $.parseJSON(rows.d);
var VM = new testView(ManualRegDiseaseData);
ko.applyBindings(VM);
var testView = function (DiseaseData) {
var self = this;
self.disease = ko.observableArray();
self.benefitData = ko.observableArray();
var sampleBenefit = [{ "Benefit": "Room", "Detail": "Ordinary", "Desc": "not herbal", "TotalDays": "2", "Remaining": "1000", "Claimed": "400", "Approved": "350", "Excess": "50" },
{ "Benefit": "Medicine", "Detail": "All", "Desc": "herbal", "TotalDays": "1", "Remaining": "2000", "Claimed": "800", "Approved": "600", "Excess": "100" }];
$.each(sampleBenefit, function (x, rowType) {
var obj = new BenefitObject();
obj.Benefit(rowType.Benefit);
obj.Detail(rowType.Detail);
obj.Desc(rowType.Desc);
obj.TotalDays(rowType.TotalDays);
obj.Remaining(rowType.Remaining);
obj.Claimed(rowType.Claimed);
obj.Approved(rowType.Approved);
obj.Excess(rowType.Excess);
self.benefitData.push(obj);
})
$.each(DiseaseData, function (x, rowType) {
var obj = new DiseaseObject();
obj.DiseaseCode(rowType.DiseaseCode);
obj.DiseaseName(rowType.DiseaseName);
self.disease.push(obj);
})
}
<table id="tblDisplayBenefit" class="">
<thead>
<tr>
<th>No.</th>
<th>Benefit </th>
<th>Detail </th>
<th>Description </th>
<th>Total Days </th>
<th>Remaining Inner Limit </th>
<th>Claimed </th>
<th>Approved </th>
<th>Excess </th>
<th>Reduce Max Limit </th>
</tr>
</thead>
<tbody data-bind="foreach: benefitData">
<tr>
<td data-bind="value: Benefit"></td>
<td data-bind="value: Detail"></td>
<td data-bind="value: Desc"></td>
<td data-bind="value: TotalDays"></td>
<td data-bind="value: Remaining"></td>
<td data-bind="value: Claimed"></td>
<td data-bind="value: Approved"></td>
<td data-bind="value: Excess"></td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="6" style="text-align: right">Total</td>
<td>Total Claimed</td>
<td>Total Approved</td>
<td>Total Excess</td>
</tr>
</tfoot>
</table>
Those are my JS snippet code and my html, the problem is, I can not data bind the sample JSON variable into my table. It seems I can not push my data into benefitData object.
I have been doing console.log the rowType, the Json data is readable through the console.log.
I dont know, this must be a simple data binding, yet I dont know where is the fault part.
I cant figure this out.
Thanks in advance for the suggestion!
Use text binding instead of value for text int the table cells:
<td data-bind="text: Benefit"></td>
<td data-bind="text: Detail"></td>
<td data-bind="text: Desc"></td>
<td data-bind="text: TotalDays"></td>
<td data-bind="text: Remaining"></td>
<td data-bind="text: Claimed"></td>
<td data-bind="text: Approved"></td>
<td data-bind="text: Excess"></td>
Js Fiddle
According to knockout documentation
The value binding links the associated DOM element’s value with a
property on your view model. This is typically useful with form
elements such as input, select and textarea.
In other words it works with form elements and makes two-way binding, so when you change the value knockout automatically updates your ViewModel.
On the other hand, text binding
Typically this is useful with elements like span or em that
traditionally display text, but technically you can use it with any
element.
So you should use text binding here, because <td> element has static content.

Multiple foreach in Durandal

I am all new to Durandal and have been playing around with it for a few hours now. It seems very promising - but now I have run into a problem, I cannot figure out - and cannot find a solution with Google.
I have a view with three tables of data - creditCardLines, cashLines and drivingReimbursementLines. They fetch data from three different data sources - and the user can add new lines to cashLines and drivingReimbursementLines (left out the forms).
Problem: In the viewmodel, I can easily bind a list for data to the first foreach - but I cannot figure out how to bind data to the second and third.
In the activate function I make an ajax call to my server API to get the data for the first foreach - and then returns the promise, when this finishes. How do I get data for the second and third foreach here?
ViewModel:
define(function () {
var submit = function () {
this.displayName = 'Expenses';
this.creditCardLines = ko.observableArray();
var me = this;
this.activate = function () {
return $.get('/submit/GetCreditCardLines').then(function (creditCardLines) {
me.creditCardLines(creditCardLines.Data);
});
};
};
return submit;
});
View:
<section>
<h2 data-bind="html:displayName"></h2>
<h3>CreditCard lines</h3>
<table class="table">
<tbody data-bind="foreach: creditCardLines">
<tr>
<td class="date" data-bind="text: Date"></td>
<td data-bind="text: Description"></td>
<td data-bind="text: Amount"></td>
<td><input type="checkbox" data-bind="checked: ApprovedEmployee" /></td>
</tr>
</tbody>
</table>
<h3>Cash lines</h3>
<table class="table">
<tbody data-bind="foreach: cashLines">
<tr>
<td class="date" data-bind="text: Date"></td>
<td data-bind="text: Description"></td>
<td data-bind="text: Amount"></td>
</tr>
</tbody>
</table>
<!-- TODO: Generate form to add new lines -->
<h3>Driving reimbursement lines</h3>
<table class="table">
<tbody data-bind="foreach: drivingReimbursementLines">
<tr>
<td class="date" data-bind="text: Date"></td>
<td data-bind="text: Description"></td>
<td data-bind="text: Distance"></td>
<td data-bind="text: Rate"></td>
<td data-bind="text: Amount"></td>
</tr>
</tbody>
</table>
<!-- TODO: Generate form to add new lines -->
<!-- Approve and save all lines as a quote with lines -->
<input type="submit" value="Submit quote" />
</section>
This is how I've been dealing with multiple jQuery deferred calls in my view models:
return $.when(
$.get('/submit/GetCreditCardLines'),
$.get('/submit/GetCashLines'),
$.get('/submit/GetReimbursementLines'))
.then(function (creditCardLines, cashLines, reimbursementLines)
{
processCreditCardLines(creditCardLines);
processCashLines(cashLines);
processReimbursementLines(reimbursementLines);
})
.fail(function (status)
{
// Do whatever you need to if it fails
});
You don't need the process methods if you don't want them, but if you're doing anything complicated, I think it's neater to have them.
You should check out BreezeJS. It has an option to make a single Ajax call to pull multiple lookups.
Take a look at the BreezeJS docs for a detailed explanation: http://www.breezejs.com/documentation/lookup-lists

jquery select span by id

hello guys
some times you just loose it and you cant event remember how to search that that you lost
<div>
<table cellspacing="0" rules="all" border="1" id="ctl00_DefaultContent_migrationGridView" style="height:90%;width:100%;border-collapse:collapse;">
<tr>
<th scope="col"> </th><th scope="col">Lenda</th><th scope="col">CSV Dosje</th><th scope="col">Gjendje</th><th scope="col">Datë</th><th scope="col">Njoftim</th><th scope="col"> </th>
</tr><tr>
<td>
<input type="submit" name="ctl00$DefaultContent$migrationGridView$ctl02$Button1" value="Fshije" id="ctl00_DefaultContent_migrationGridView_ctl02_Button1" />
</td><td>
<a id="ctl00_DefaultContent_migrationGridView_ctl02_CaseLinkButton" href="javascript:__doPostBack('ctl00$DefaultContent$migrationGridView$ctl02$CaseLinkButton','')" style="font-weight:bold;">mig1</a>
</td><td>
<span id="ctl00_DefaultContent_migrationGridView_ctl02_lblCSVFileName">19_71914066_2010-11-11_0849_ENG_SOFALI.csv</span>
</td><td>
<span id="ctl00_DefaultContent_migrationGridView_ctl02_Label2" style="color:Maroon;font-weight:bold;">Read</span>
</td><td>2010-12-28</td><td>
<span id="ctl00_DefaultContent_migrationGridView_ctl02_Label3"></span>
</td><td>
<a id="ctl00_DefaultContent_migrationGridView_ctl02_startStopLinkButton" href="javascript:__doPostBack('ctl00$DefaultContent$migrationGridView$ctl02$startStopLinkButton','')">Start migration</a>
<a id="ctl00_DefaultContent_migrationGridView_ctl02_checkedAllLinkButton" title="Të Kontrolluara" href="javascript:__doPostBack('ctl00$DefaultContent$migrationGridView$ctl02$checkedAllLinkButton','')">Të Kontrolluara</a>
</td>
</tr><tr>
<td>
<input type="submit" name="ctl00$DefaultContent$migrationGridView$ctl03$Button1" value="Fshije" id="ctl00_DefaultContent_migrationGridView_ctl03_Button1" />
</td><td>
<a id="ctl00_DefaultContent_migrationGridView_ctl03_CaseLinkButton" href="javascript:__doPostBack('ctl00$DefaultContent$migrationGridView$ctl03$CaseLinkButton','')" style="font-weight:bold;">mig1</a>
</td><td>
<span id="ctl00_DefaultContent_migrationGridView_ctl03_lblCSVFileName">19_71914070_2010-11-11_0850_ENG_TRUDE.csv</span>
</td><td>
<span id="ctl00_DefaultContent_migrationGridView_ctl03_Label2" style="color:Maroon;font-weight:bold;">Read</span>
</td><td>2010-12-28</td><td>
<span id="ctl00_DefaultContent_migrationGridView_ctl03_Label3"></span>
</td><td>
<a id="ctl00_DefaultContent_migrationGridView_ctl03_startStopLinkButton" href="javascript:__doPostBack('ctl00$DefaultContent$migrationGridView$ctl03$startStopLinkButton','')">Start migration</a>
<a id="ctl00_DefaultContent_migrationGridView_ctl03_checkedAllLinkButton" title="Të Kontrolluara" href="javascript:__doPostBack('ctl00$DefaultContent$migrationGridView$ctl03$checkedAllLinkButton','')">Të Kontrolluara</a>
</td>
</tr>
</table>
</div>
can somebody tell me how to iterate through spans that have this string on the id 'lblCSVFileName'
and get their values
for the first row i should get 19_71914066_2010-11-11_0849_ENG_SOFALI.csv and for the second
19_71914070_2010-11-11_0850_ENG_TRUDE.csv
:( another bad day
You can use an attribute ends-with selector for the ID and .map() to get an array of strings (the text inside each), like this:
var arr = $("span[id$='lblCSVFileName']").map(function() {
return $(this).text();
}).get();
Or, a slightly more optimized unpublished version:
var arr = $("span[id$='lblCSVFileName']").map(function() {
return $.text([this]);
}).get();
This would get you an array of values to work with, for example:
["19_71914066_2010-11-11_0849_ENG_SOFALI.csv", "19_71914070_2010-11-11_0850_ENG_TRUDE.csv"...]
The following code will give you what you need.
It gets all spans that have an ID that starts with "ctl00_DefaultContent_migrationGridView_ctl02_".
$('span[id^="ctl00_DefaultContent_migrationGridView_ctl02_"]').each(function() {
//the following will give you the text of each span
$(this).text();
});
NOTE: I would add a class to each span and do a jquery select using that.

Resources