I have the following code in Razor, why doesn't it work?
All I want to do is check to see if data has a value, if it does then print it with to 2 decimal places and append a % sign..
<td>
#if (data.HasValue == true)
{
#string.Format("{0:F2}", data)%
}
</td>
it says I don't need am # at the start of the string. I take that off, then it complain about an ;
but I got it to work by doing the following
#if (data.HasValue == true)
{
<td>
#string.Format("{0:F2}", data)%
</td>
}
else
{
<td>
</td>
}
How can I fix the first set of code to work?
Thanks
Razor has no clue what you want to do with the string.Format call.
To fix that, use:
<td>
#if (data.HasValue == true)
{
#:#string.Format("{0:F2}", data)%
}
</td>
This is guaranteed to work, give it a try:
<td>
#{
if (data.HasValue)// you don't need to compare to true because it's already a boolean :)
{
string.Format("{0:F2}", data)%
}
}
</td>
Try this its working ..
<td>
#if (data.HasValue == true)
{
string.Format("{0:F2} {1}", data, "%");
}
</td>
Or you can try this
<td>
#if (data.HasValue == true)
{
#:#String.Format("{0:F2}", data)%;
}
</td>
Related
I have created a list in my program. In this list, you press ArrowUp or ArrowDown to navigate between the items that are generated with a v-for in said list. When you use the arrows to navigate the list, a counter is used to know which item inside this list is the "selected" one. My current problem is: When you "select" an item that is outside your view, the Browser's Window doesn't follow it down, allowing the selection to keep "going down" this list, but the user isn't able to see the other selected items.
I've tried messing around with the focus() function, but I don't know how to focus() a specific item without using the ref atribute, which I also can't seem to implement since it's being generated with v-for.
Here's my current table:
<table class="table table-striped p-0 m-0" v-show="pedidos.length > 0">
<thead class="table-dark">
<tr class="">
<th>Cliente Emissor</th>
<th>Produto</th>
<!-- <th>Endereço</th> -->
<!-- <th>Valor</th> -->
<th>Data Emissão</th>
<th>Estado</th>
</tr>
</thead>
<tbody>
<template v-for="(pedido, index) in pedidos">
<itemPedido :pedido="pedido" :key="index" :endereco="endereco" :class="[(navegacaoAtual == index) ? 'teste' : '']" ref="teste"></itemPedido>
<itemEdit #atualizarListaPedidos="atualizarListaPedidos" #atualizarStatusModal="atualizarStatusModal" class="animation-dropdown" :pedido="pedido" :key="pedido.nome"></itemEdit>
</template>
</tbody>
</table>
Here's the current navigateWithArrows() function I've made:
navegarSetas(key) {
if (this.navegacaoSetasAtiva == false) {
this.navegacaoSetasAtiva = true
this.navegacaoAtual = 0
} else if (this.modalAtivado == false && this.navegacaoSetasAtiva == true) {
if (key == 'ArrowDown' && this.navegacaoAtual < this.pedidos.length - 1) {
this.navegacaoAtual++
Vue.nextTick().then(() => {
let teste1 = this.$refs.teste[this.navegacaoAtual]
teste1.$el.focus()
})
} else if (key == 'ArrowUp' && this.navegacaoAtual <= this.pedidos.length && this.navegacaoAtual > 0) {
this.navegacaoAtual--
} else if (key == 'Enter') {
let pedidoSelecionado = this.pedidos[this.navegacaoAtual].id
Event.$emit('changeShow', pedidoSelecionado)
}
}
},
All help is appreciated. Thank you!
After taking a break and resuming my search, I stumbled upon something that did exactly what I wanted: the scrollIntoView() function.
https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
I'm trying to filter table rows in an intelligent way (as opposed to just tons of code that get the job done eventually) but a rather dry of inspiration.
I have 5 columns in my table. At the top of each there is either a dropdown or a textbox with which the user may filter the table data (basically hide the rows that don't apply)
There are plenty of table filtering plugins for jQuery but none that work quite like this, and thats the complicated part :|
Here is a basic filter example http://jsfiddle.net/urf6P/3/
It uses the jquery selector :contains('some text') and :not(:contains('some text')) to decide if each row should be shown or hidden. This might get you going in a direction.
EDITED to include the HTML and javascript from the jsfiddle:
$(function() {
$('#filter1').change(function() {
$("#table td.col1:contains('" + $(this).val() + "')").parent().show();
$("#table td.col1:not(:contains('" + $(this).val() + "'))").parent().hide();
});
});
Slightly enhancing the accepted solution posted by Jeff Treuting, filtering capability can be extended to make it case insensitive. I take no credit for the original solution or even the enhancement. The idea of enhancement was lifted from a solution posted on a different SO post offered by Highway of Life.
Here it goes:
// Define a custom selector icontains instead of overriding the existing expression contains
// A global js asset file will be a good place to put this code
$.expr[':'].icontains = function(a, i, m) {
return $(a).text().toUpperCase()
.indexOf(m[3].toUpperCase()) >= 0;
};
// Now perform the filtering as suggested by #jeff
$(function() {
$('#filter1').on('keyup', function() { // changed 'change' event to 'keyup'. Add a delay if you prefer
$("#table td.col1:icontains('" + $(this).val() + "')").parent().show(); // Use our new selector icontains
$("#table td.col1:not(:icontains('" + $(this).val() + "'))").parent().hide(); // Use our new selector icontains
});
});
This may not be the best way to do it, and I'm not sure about the performance, but an option would be to tag each column (in each row) with an id starting with a column identifier and then a unique number like a record identifier.
For example, if you had a column Produce Name, and the record ID was 763, I would do something like the following:
<table id="table1">
<thead>
<tr>
<th>Artist</th>
<th>Album</th>
<th>Genre</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td id="artist-127">Red Hot Chili Peppers</td>
<td id="album-195">Californication</td>
<td id="genre-1">Rock</td>
<td id="price-195">$8.99</td>
</tr>
<tr>
<td id="artist-59">Santana</td>
<td id="album-198">Santana Live</td>
<td id="genre-1">Rock</td>
<td id="price-198">$8.99</td>
</tr>
<tr>
<td id="artist-120">Pink Floyd</td>
<td id="album-183">Dark Side Of The Moon</td>
<td id="genre-1">Rock</td>
<td id="price-183">$8.99</td>
</tr>
</tbody>
</table>
You could then use jQuery to filter based on the start of the id.
For example, if you wanted to filter by the Artist column:
var regex = /Hot/;
$('#table1').find('tbody').find('[id^=artist]').each(function() {
if (!regex.test(this.innerHTML)) {
this.parentNode.style.backgroundColor = '#ff0000';
}
});
You can filter specific column by just adding children[column number] to JQuery filter. Normally, JQuery looks for the keyword from all the columns in every row. If we wanted to filter only ColumnB on below table, we need to add childern[1] to filter as in the script below. IndexOf value -1 means search couldn't match. Anything above -1 will make the whole row visible.
ColumnA | ColumnB | ColumnC
John Doe 1968
Jane Doe 1975
Mike Nike 1990
$("#myInput").on("change", function () {
var value = $(this).val().toLowerCase();
$("#myTable tbody tr").filter(function () {
$(this).toggle($(this.children[1]).text().toLowerCase().indexOf(value) > -1)
});
});
step:1 write the following in .html file
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">
<table id="myTable">
<tr class="header">
<th style="width:60%;">Name</th>
<th style="width:40%;">Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Germany</td>
</tr>
<tr>
<td>Berglunds snabbkop</td>
<td>Sweden</td>
</tr>
<tr>
<td>Island Trading</td>
<td>UK</td>
</tr>
<tr>
<td>Koniglich Essen</td>
<td>Germany</td>
</tr>
</table>
step:2 write the following in .js file
function myFunction() {
// Declare variables
var input, filter, table, tr, td, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
// Loop through all table rows, and hide those who don't match the search query
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
I have 3 checkboxes with ID as id1,id2,id3 and name as name1,name2,name3.
<table id="ContentPlaceHolder1_cblType">
<tbody>
<tr>
<td>
<input id="ContentPlaceHolder1_cblType_0" type="checkbox"
name="ctl00$ContentPlaceHolder1$cblType$0" value="Clinician">
<label for="ContentPlaceHolder1_cblType_0">Clinician</label>
</td>
<td>
<input id="ContentPlaceHolder1_cblType_1" type="checkbox"
name="ctl00$ContentPlaceHolder1$cblType$1" value="Administration">
<label for="ContentPlaceHolder1_cblType_1">Administration</label>
</td>
<td>
<input id="ContentPlaceHolder1_cblType_2" type="checkbox"
name="ctl00$ContentPlaceHolder1$cblType$2" value="Therapist">
<label for="ContentPlaceHolder1_cblType_2">Therapist</label>
</td>
</tr>
</tbody>
</table>
I need to check it upon the value retrieving from excel.
data.getEmpType().get(rowCnt)
With above code I will get the value from excel.
How to do it?
First take string value from excel sheet.
Then collect all or required check boxes.
Loop the check boxes and verify against required value which collected from excel.
String requiredValue= "from excel file";
List<WebElement> checkBoxes=oWebDriver
.findElements(By.cssSelector("input[id^='ContentPlaceHolder1'][type='checkbox']"));
for (int i = 0; i < checkBoxes.size(); i++) {
if(checkBoxes.get(i).getAttribute("value").equals(requiredValue)){
checkBoxes.get(i).click();
break;
}
}
Or use more straight way through Xpath:
String excelValue = "from excel file";
String xpathExp = String.format(
"//table[#id='ContentPlaceHolder1_cblType']" +
"//td[label[text()='%s']]/input", excelValue );
driver.findElement(By.xpath(xpathExp)).click();
String ExcelValue= "data from excel";
**/*Create a list using common attributes of all three check boxes */**
List< WebElement> CB=driver.findElements(By.xpath("\\input[starts-with(#id,'ContentPlaceHolder1') AND #type='checkbox']"));
**/*Store size of List created in CB_size(as per your data is should be three */**
int CB_size=CB.size();
**/*below loop will execute for all three check boxes one by one and with which so ever your value from excel matches, it click and come out of the loop */**
for (int i = 0; i < CB_size; i++)
{
if(CB.get(i).getAttribute("value").equals(ExcelValue))
{
CB.get(i).click();
break;
}
}
I have a page and all the function is working my only problem now is ciunying the record from the databse..
Class.user.php
Public function data($count)
{
$stmt=$this->db->prepare("SELECT COUNT(*) FROM login");
$result=$this->db->prepare($count);
$result->execute();
$number_of_rows=$result->fetchColumn();
}
Index.php
<table>
<thead>
<tr>
<th>2014</th>
</tr>
</thead>
<tbody>
<?php
$count="SELECT COUNT(*) FROM login";
$crud->data($count);
?>
The problem is that its not showing the count..
You're writing the same query as function argument, but also inside the function itself. And you're only really using one. This is nonsense. Dedicate your method to return the count, don't make the query a parameter.
prepareing the statement is pointless in this case, since you're neither reusing it nor are you binding any values. You can simply query() it directly.
The clincher: you're neither outputting nor returning the count, so it's very very expected that it doesn't show up anywhere.
Here's a sane version:
public function getCount() {
$result = $this->db->query('SELECT COUNT(*) FROM login');
return $result->fetchColumn();
}
<tbody>
<tr>
<td>
<?php echo $crud->getCount(); ?>
I think I usually do it like this link
$sql= "SELECT COUNT(*) FROM login";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$row =$stmt->fetchObject();
then to output it you would have to ECHO
<table>
<tbody>
<tr>
<td>
<?php echo $row['count'];?>
</td>
</tr>
</tbody>
</table>
Below is my code:
#{
Layout = "/_SiteLayout.cshtml";
var db = Database.Open("MyDatabase");
var query = "SELECT * FROM Team";
var Teams = db.Query(query);
}
<form>
<table>
<tr>
<td>Team Name</td>
<td>Played</td>
<td>Points</td>
</tr>
#{ foreach(var Team in Teams){
<tr>
<td>#Team.TeamName</td>
<td><input type="text" value="#Team.Played" name="Played"/></td>
<td><input type="text" value="#Team.Points" name="Points"/></td>
</tr>
}
}
</table>
</form>
This is the result:
So what I want to do is update my whole table.
What is the SQL query to do this? I want to update Points and Games Played in my database for all teams once the form is posted.
I don't actually understand what exactly you are trying to achieve (update your whole table with what?), but here is some information you might find useful:
SQL Update Tutorial, SQL Update, Update from Select
Following is My Solution. Anyone have an efficient Solution?
#{
var db = Database.Open("MYDATABSE");
var query = "SELECT * FROM Team";
var Teams = db.Query(query);
var InsertQuery = "";
if(IsPost){
foreach(var Team in Teams){
var Points = Request[Team.TeamName];
var TeamId = Team.TeamId.ToString();
var Played = Request[TeamId];
var executeQueryString="UPDATE Team Set Points=#0, Played=#1 WHERE TeamId=#2";
db.Execute(executeQueryString, Points, Played, Team.TeamId);
}
Response.Redirect("~/UpdateTable.cshtml");
}
}
<br /><br />
<form action="" method="post">
<table>
<tr>
<td><h5>Team Name</h5></td>
<td><h5>Played</h5></td>
<td><h5>Points</h5></td>
</tr>
#{ foreach(var Team in Teams){
<tr>
<td>#Team.TeamName</td>
<td><input type="text" value="#Team.Played" name="#Team.TeamId"/></td>
<td><input type="text" value="#Team.Points" name="#Team.TeamName"/></td>
</tr>
}
}