Can smarty do like foreach in codeigniter - foreach

this is sample foreach in codeigniter
<select>
<?php
$no = 0;
foreach($value as $v){
?>
<option value=<?=$v->id?>><?=$v->name." -- ".$buy[$no]->currency;?></option>
<?php
$no++;
} ?>
this is sample code in smarty
<select>
{foreach from="$value" item="v"}
<option value="{$v.id}" >"{$v.name} -- $buy[0].currency"</option>
{/foreach}
</select>
i want $buy like this
$buy[0]
$buy[1]
$buy[3]
etc...

Related

Cascade drop-down in MVC View without ajax?

I created one View Model with two Entities. I am passing this view model to my MVC Razor view which have two html drop-downs for each entity respectively.
<select class="form-control" id="Employees" name="Employees">
#foreach (var employee in Model.Employees)
{
<option value="#employee.Id"> #employee.name </option>
}
</select>
<select class="form-control" id="Tasks" name="Tasks">
#foreach (var task in Model.Tasks)
{
<option value="#task.Id"> #task.name </option>
}
</select>
Employee table is the parent of Task table. What I want is getting all the tasks which are related to particular employee only. e.g. In Employee drop-down I select John, then in Tasks drop-down I should get all the tasks which are relative to John. I know how to do this with ajax. I am looking for some other solution.
Is it possible to do something like this:
#foreach (var task in Model.Tasks.Where(x=>x.employeeId == 'Selected in previous dropdown'))
{
<option value="#task.Id"> #task.name </option>
}
Html block
<select id="Employees">
<option value="">Select Employee</option>
<option value="1">Employee1</option>
<option value="2">Employee2</option>
</select>
<select id="Tasks">
<option value="">Select Task</option>
<option value="1" data-employee="1">Employee1Task1</option>
<option value="2" data-employee="1">Employee1Task2</option>
<option value="3" data-employee="1">Employee1Task3</option>
<option value="1" data-employee="2">Employee2Task1</option>
<option value="2" data-employee="2">Employee2Task2</option>
<option value="3" data-employee="2">Employee2Task3</option>
</select>
Script scetion
<script>
$(document).ready(function () {
//on page ready hide all task option
$("#Tasks").find('option').hide();
// set task as empty
$("#Tasks").val('');
// onchange of employee Drop down
$("#Employees").on('change', function () {
var selectedEmployee = $("#Employees").val();
if (selectedEmployee != '') {
$("#Tasks").find('option').hide();
$("#Tasks option[value='']").show();
$('*[data-employee="' + selectedEmployee + '"]').show();
}
else {
// if employee not selected then hide all tasks
$("#Tasks").find('option').hide();
$("#Tasks").val('');
}
});
});
</script>
Please populate country and state drop down list using MVC way by for each loop and use above script. The mandatory case is you have to render all cascade options
<select class="form-control" id="Employees" name="Employees">
#foreach (var employee in Model.Employees)
{
<option value="#employee.Id"> #employee.name </option>
}
</select>
<select class="form-control" id="Tasks" name="Tasks">
#foreach (var task in Model.Tasks)
{
<option value="#task.Id" data-employee="#task.EmployeeId"> #task.name </option>
}
</select>

changes to &nbsp in asp.net mvc html.dropdownlist

I want to show items with indention in html.dropdownlist
this is code in controller
IEnumerable<ViewModels.testvm> test = db.Database.SqlQuery<ViewModels.testvm>(#"WITH tree (id, parentid, level, title, rn) as
(
some code
)
***********SELECT id,REPLICATE(' ',level) + title as title
FROM tree
order by RN");
ViewBag.ParentID = new SelectList(test, "ID", "Title");
as you can see I add &nbsp to dropdown items in replecate
but the problem is &nbsp shows in dropdown.
this is view > source result:
<select class="form-control" id="ParentID" name="ParentID"><option
value="10">Menu1</option>
<option value="11">&nbsp;Sub1</option>
<option value="14">&nbsp;Submenu1</option>
<option value="12">Menu2</option>
<option value="16">&nbsp;sub2</option>
<option value="13">Menu3</option>
<option value="15">&nbsp;sub3</option>
<option value="17">&nbsp;&nbsp;sub sub</option>
<option value="22">&nbsp;&nbsp;&nbsp;sub3 sub sub</option>
<option value="19">menu4</option>
<option value="20">&nbsp;sub4</option>
<option value="21">menu5</option>
</select>
as you can see converted to &nbsp;
I tried "\xA0" instead of &nbsp but it shows in dropdown too!
I think html.raw can solve this, but I don't know how can I use it with html.dropdownlist.
this is view
<div class="col-md-10">
#Html.DropDownList( "ParentID", null, htmlAttributes: new { #class = "form-
control" })
</div>
any Idea?
Edit
by changing view like this the problem solved:
I tried this after checked a post as answer
<select class="form-control" id="ParentID" name="ParentID">
<option value="0">First Level</option>
#foreach (var item in ViewBag.ParentID)
{
<option value="#item.Value">#Html.Raw(#item.Text)</option>
}
</select>
You can manually create the dropdown list assuming you place your collection in a ViewModel member called ParentIDs:
The View:
#if (Model.ParentIDs.Any())
{
<select class="form-control" id="ParentID" name="ParentID">
#foreach (var item in Model.ParentIDs)
{
if (Model.ParentID == item.id)
{
<option value="#item.id" selected>#item.title</option>
}
else
{
<option value="#item.id">#item.title</option>
}
}
</select>
}

Layout file, how can i list the contents in layout

I would like the view the country list, state list and city list in the layout side menu in all the pages.
<div class="container side-menu">
<div class="row">
<div class="col-md-3">
<?php $countries = array();?>
<select class="form-control">
<option value="">All</option>
<?php foreach($countries as $country) { ?>
<?php echo "<option>".$country['name']."</option>"; ?>
<?php } ?>
</select>
</div>
<div class="col-md-3">
<?php $categories = array(); ?>
<select class="form-control">
<option value="">All</option>
<?php foreach($categories as $category) { ?>
<?php echo "<option>".$category['name']."</option>"; ?>
<?php } ?>
</select>
</div>

Select2 does not show the set value

There is a select2 element :
<select name="commande_id" id="commande">
<option value=""> -- Choisir une commande -- </option>
<option value="1">COM-2-2015</option>
</select>
$(document).ready(function() {
...
$("#commande").select2({width:'100%'});
...
});
When I enter the page in update mode then I set the option value of the select element programmatically to be the value from database :
$(document).ready(function() {
...
<?php
if (isset($data)) { ?>
...
$("#commande").val("<?php echo $data[0]['commande_id'];?>");
...
<?php
}
?>
...
});
The problem is that at runtime the select element does not show the appropriate text :
But when I click it then the appropriate value is selected :
So how to make the appropriate text shown ?
I don't know how you are getting the $data, but I guess it's something like that, just an example:
<?php
$data[] = ["commande_id" => "1", "name_field" => "COM-2-2015"];
foreach ($data as $key => $value){
?>
<select>
<option value='<?php echo $value['commande_id']?>'><?php echo $value['name_field'] ?></option>
</select>
<?php } ?>
Look it here: https://ideone.com/PSpKmu

Dropdowns - selects dynamics-jquery

I'm try to do a dynamic selects with jQuery, example:
<select >
<option value=1> 1</option>
<option value=2> 2</option>
<option value=3> 3</option>
<option value=4> 4</option>
</select>
when I get value="x"
I would like to add
count=x;
for(int i=1 ; i<=count; i++){
<select > </select>
}
I have a problem with me code , this code add and add.. I dont want this
I just want add the 'x' select
http://jsfiddle.net/hqLPp/48/
If you want result on same page, then this might be useful,
this will be on your page, where you want result
<script>
function showSelectBox(str)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myresult").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","test.php?q="+str,true); // Pass value to another page Here->test
xmlhttp.send();
}
</script>
<select name='check' onchange="showSelectBox(this.value)">
<option>Select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<div id="myresult">
</div>
Now On test.php Simply Call Value & put select box,
<?php
$q = $_GET['q'];
for($i=1 ; $i<=$q; $i++)
{
echo '<select > </select>';
}
?>

Resources