how to use simpleHTMLdom to get a string that is inside a <th> tag (and there is many other <th>) For example :
<th>name</th>
<td>john</td>
<th>age</th>
<td>32</td>
How to get 32 ? note that sometimes, the website i'm parsing, doesn't contain the same number of <th> sometimes there is no <th>name</th> so i cant do : $html->find('th',1) to get age and go to <td> to get 32
Any way to perform something like : $html->find('age') ?
Thank you
You have to do something like this:
$ageIndex = 0;
foreach($html->find('th') as $key => $val)
{
if($val->innertext == 'age')
{
$ageIndex = $key;
break;
}
}
$age = $html->find('td',$ageIndex);
Related
I am printing the data from ViewModel into DataTable in the following way, but how to obtain a space between #item.FirstName and #item.LastName so that the output can be of the form FirstName LastName? At present, I am getting the Name as FirstNameLastName. Ex: MarkStevens. I want to print it as Mark Stevens.
#if(Model.Count() != 0)
{
<thead>
<tr>
<th data-column-id="Name">Name</th>
...
</tr>
</thead>
}
else
{
<h3 style="text-align:center">No Records Found</h3>
}
<tbody>
#foreach(var item in Model)
{
<tr>
<td>
#(if (item.UserId != null)
{
#item.FirstName #item.LastName
}
else
{
#item.OtherUser
}
</td>
</tr>
}
</tbody>
You can use #Html.Raw helper which means, you render whatever inside without any html encoding like space or string:
#item.FirstName #Html.Raw(" ") #item.LastName
If you want html, you can also use a simple non-breaking spaces:
#item.FirstName #item.LastName
How about string format the output: $"{#item.FirstName} {#item.LastName}"
use string.Format. i feel this is very clean approach.
string.Format("{0} {1}", #item.FirstName,#item.LastName)
I'am learning MVC5 & Entity Framework 6, and have accomplished a connection to a SQL Database.
I need to change the class of the <tr> field based on the TicketStatus.StatusID result
For Example If the StatusID =1 then use:
<tr class="ticket even status-new priority-low">
if the statusID = 2 then use:
<tr class="ticket even status-open priority-low">
Etc.. Etc..
This is the current Foreach loop code I have based on the number fields to display in a Bootstrap table.
#foreach (var item in Model)
{
<tr class="ticket even status-new priority-low">
<td class="status"><span>#Html.DisplayFor(modelItem => item.TicketStatus.Status)</span></td>
<td class="number"><ul class="user-dashboard-tickets"><li class="user-dashboard-tickets"><span>#Html.DisplayFor(modelItem => item.TicketID)</span></li></ul></td>
<td class="assigned">#Html.DisplayFor(modelItem => item.TicketOwner.OwnerName)</td>
<td class="title"><h2>#Html.DisplayFor(modelItem => item.Title)</h2></td>
<td class="type">Problem</td>
<td class="assigned">test.user</td>
<td class="age">2 days</td>
</tr>
}
I don't know if you can put another foreach loop inside the loop? or add a query to the <tr> field?
you can put any code inside Razor view, even inside a loop, like this:
var statusClass = "";
switch (TicketStatus.StatusID){
case 1:
statusClass = "status-open";
break;
}
<tr class="ticket even #statusClass priority-low">
I've got a view that is showing the breakdown of all fault categories reported and the area that they've been reported in. What I need to get is a total count of the number of faults of each category, and then a sub count of each area under that type. I've seen around that there are options to use Model.Count(), however whenever I try that it doesn't return the right number.
The view code that I'm using is below:
<table class="table table-responsive">
<tr>
#foreach (var catGroup in Model.GroupBy(item => item.job_category))
{
foreach (var itemCat in catGroup.Take(1))
{
if (itemCat.job_category != null)
{
<td>
<table id="Tbl_Total_#itemCat.job_category">
<tr>
<th>
#Html.DisplayFor(modelItem => itemCat.job_category)
</th>
</tr>
#foreach (var item in catGroup.GroupBy(item => item.job_area))
{
foreach (var itemArea in item.Take(1))
{
<tr>
<td>
#Html.DisplayFor(modelItem => itemArea.job_area)
</td>
</tr>
}
}
</table>
</td>
}
}
}
</tr>
</table>
Can someone point me in the right direction to get a count per category, and per area under the categories?
Thanks
You can get the number of element per group calling Count extension method. eg, for the category groups you can do this:
catGroup.Count()
And you can do the same for the areas:
item.Count()
Im new with MVC.
I have a model called UAV.
│Callsign│NumDeliveries│Mileage│MaxVelocity│MinVelocity│
Hawk61 37 96 20 10
BURL14 2047 57 30 15
OTTO93 82 72 25 10
in cshtml file, i made a table only using Callsign, NumDeliveries, Mileage.
<table class="UAV_table" id="UAV_table">
<tr>
<th>Callsign</th>
<th>NumDeliveries</th>
<th>Mileage</th>
</tr>
#foreach (UAV uav in Model.UAVs)
{
<tr onclick="click_row()">
<td onclick="click_row()">
#Html.DisplayFor(modelItem => uav.Callsign)
</td>
<td>
#Html.DisplayFor(modelItem => uav.NumDeliveries)
</td>
<td>
#Html.DisplayFor(modelItem => uav.Mileage)
</td>
</tr>
}
</table>
so the table shows all datas for Callsign, NumDeliveries, Mileage.
what i want to do is, when i click the row of the table, i want to see only that correspond information.
#foreach (UAVs uavid in Model.uavs)
{
<p class="detail_title" id="detail_title">
UAV: # (#Html.DisplayFor(modelItem => uavid.MaxVelocity))
</p>
}
for example, using above line of code, if i click first row of that table(callsign = Hawk61), i want to see like UAV: # 20 (MaxVelocity for Hawk61). MaxVelocity is not in the table, so i need to get it from database.
But I have problem with showing data. If i use right above code, it has #foreach statement, it shows all the Hawk61, BURL14, OTTO93's MaxVelocity.
it shows me like
UAV:# 20
UAV:# 30
UAV:# 25
I need to see only what i selected. (just shows what i click, in this example, only need to show UAV:# 20 which is first row, Hawk61's MaxVelocity).
is there any way to get the data from database not using foreach statement?
Thank you.
Since the values of MaxVelocityand MinVelocity are populated, you can make use of data- attributes to store the values in the DOM and use jquery to display them. For example
#foreach (UAV uav in Model.UAVs)
{
<tr class="uavrow" data-maxvelocity="#uav.MaxVelocity" data-minvelocity="#MinVelocity">
<td>#Html.DisplayFor(modelItem => uav.Callsign)</td>
<td>#Html.DisplayFor(modelItem => uav.NumDeliveries)</td>
<td>#Html.DisplayFor(modelItem => uav.Mileage)</td>
</tr>
}
And include some elements to display the associated data when you click on the row
<div>
<div><span>Call Sign: </span><span id="callsign"></span>
<div><span>Max Velocity: </span><span id="maxvelocity"></span>
<div><span>Min Velocity: </span><span id="minvelocity"></span>
</div>
And the script
$('.uavrow').click(function) {
// Get the call sign for the td element
$('#callsign').text($(this).children('td').eq(0).text());
// Get the velocity from the data attributes
$('#maxvelocity').text($(this).data('maxvelocity'));
$('#minvelocity').text($(this).data('minvelocity'));
});
If however the value were not populated, or you have a large number of properties to display, then it may be better to make an ajax call to a method (passing the callsign) which returns a partial view containing the details
<div id="uavdetails"></div>
$('.uavrow').click(function) {
var callSign = $('#callsign').text($(this).children('td').eq(0).text());
var url = '#Url.Action("Details", "YourController")';
$('#uavdetails').load(url, { CallSign: callsign });
});
Controller
public ActionResult Details(string CallSign)
{
UAV uav = // Get the UAV base on the CallSign value
return PartialView(uav);
}
Actually you have all data that you need in there.
The only thing that you need is to show proper item by using JavaScript.
You need to add parameter to your function call here:
<tr onclick="click_row('#uav.Callsign')">
And also add css class here:
#foreach (UAVs uavid in Model.uavs)
{
<p class="detail_title #uavid.Callsign" id="detail_title" style="display=none;">
UAV: # (#Html.DisplayFor(modelItem => uavid.MaxVelocity))
</p>
}
And then add a bit of javascript:
<script>
function click_row(elClass){
var elements = document.getElementsByClassName("detail_title");
for (i = 0; i < x.length; i++) {
if(x[i].className.contains(elClass)){
x[i].style.display = 'block';
} else{
x[i].style.display = 'none';
}
}
};
<script/>
I Have a Modle like:
public class Invoice
{
int Code{get;set;}
List<InvoiceItem> InvoiceItems{get;set;}
}
I fill InvoiceItems in a Table and I want Get Table Item as InvoiceItems in Controller
[HttpPost]
public virtual ActionResult Create(Invoice model)
{
//Save Invoice
return View();
}
How Can I fill InvoiceItems and send it to controller?
The outcome of your html should look something like this
<input type="text" name="Code" value=""/>
...
<table>
<thead>
<tr>
<th>#</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td><input type="hidden" name="InvoiceItems[0].Name" value="InvoiceItem1"/></td>
</tr>
<tr>
<td>2</td>
<td><input type="hidden" name="InvoiceItems[1].Name" value="InvoiceItem2"/></td>
</tr>
<tr>
<td>3</td>
<td><input type="hidden" name="InvoiceItems[2].Name" value="InvoiceItem3"/></td>
</tr>
</tbody>
</table>
<button type="submit">Submit</button>
</form>
This way you can use plain form submission or ajax
// assuming you have only one form. otherwise use more specific selector
var $form = $("form");
var data = $form.serializeArray();
var url = $form.attr("href") || document.URL;
$.post(url, data, yourCallbackFunction);
The rest is done by ModelBinder. The key part is to maintain InvoiceItems indexes. They must start from 0 and be sequential. i.e. 0,1,2,3 etc.. If you skip some index, modelbinder will end binding your list where indexes are broken.
lists can be represented by quasi-2-dimensional-arrays:
$arr =
array(
array(
{
"key":"value",
"key2":"value2"
},
{
"key":"value",
"key2":"value2"
}
),
array(
{
"key":"value",
"key2":"value2"
},
{
"key":"value",
"key2":"value2"
}
)
);
(json_encode)
can be read e.g. with $val = $arr["0"]["0"]["key2"];
this is just an example > use and combine Array-, Object- and Json-Syntax to get your list match your needs. working with json is very handy and features very "sourced" and object-oriented coding and easy data handling.
ps: to me your question seems rather unclear, dont know what you are exactly aiming at...