Using jQueryMobile 1.4.5, I need to toggle the presence of the middle button on and off so that the other 2 buttons butt_up against each other if the middle button is gone.
My js script makes it disappear but leaves room between the ouster 2 buttons.
How can this be achieved? Thanks
jQuery.fn.invisible = function() {
return this.css('visibility', 'hidden');
};
jQuery.fn.visibilityToggle = function() {
return this.css('visibility', function(i, visibility) {
return (visibility == 'visible') ? 'hidden' : 'visible';
});
};
$("li:nth-child(2)").invisible();
<footer data-role="footer" data-position="fixed">
<div data-role="navbar">
<ul>
<li><button type="submit" data-theme="c">NO</button></li>
<li><button type="submit" data-theme="c">EXTRA</button></li>
<li><button type="submit" data-theme="c">YES</button></li>
</ul>
</div>
</footer><!-- footer -->
At the moment there is no jqm method to refresh a navbar. Hopefully we get one with 1.5 ...
But you can manipulate the "grid classes" of the navbar.
I think this is the easiest way if you only want to add/remove one button.
$('#toggle').on("click", function() {
if ($("#navbar > ul").hasClass('ui-grid-b')) {
$("#extra").hide();
$("#navbar > ul").removeClass('ui-grid-b').addClass('ui-grid-a')
.find("li").last().removeClass('ui-block-c').addClass("ui-block-b");
} else {
$("#extra").show();
$("#navbar > ul").removeClass('ui-grid-a').addClass('ui-grid-b')
.find("li").last().removeClass('ui-block-b').addClass("ui-block-c");
}
});
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jquerymobile/1.4.5/jquery.mobile.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquerymobile/1.4.5/jquery.mobile.min.js"></script>
<button id="toggle">Toggle</button>
<footer data-role="footer" data-position="fixed">
<div data-role="navbar" id="navbar">
<ul>
<li>
<button type="submit" data-theme="c">NO</button>
</li>
<li id="extra">
<button type="submit" data-theme="c">EXTRA</button>
</li>
<li>
<button type="submit" data-theme="c">YES</button>
</li>
</ul>
</div>
</footer>
<!-- footer -->
Related
I am creating an ASP.NET (version 5) Core MVC application where I have a list of items. I try to make it so that when you click on an item it opens a (Bootstrap) modal view with the item's details (from another view). However, it seems like a hyperlink doesn't open the modal but instead opens the page itself (so not inside the modal). I got it working with a button, but I would like to make the user click on an item itself instead of a button.
This is the list item that I would like the user to be able to click on (the button is for testing):
I have the following page:
#model DetailsPatientFileViewModel
#section Scripts {
<script type="text/javascript">
$("#addBtn").click(function () {
$.ajax({
url: $(this).attr("formaction"),
}).done(function(res) {
$("#Modal").html(res);
$("#Modal").modal();
})
});
$("#detailCard").click(function () {
$.ajax({
url: $(this).attr("formaction"),
}).done(function(res) {
$("#Modal").html(res);
$("#Modal").modal();
})
});
</script>
}
<div class="patient-file-details-container">
<div class="title-container">
<h4>Treatments</h4>
<!-- This works just fine: -->
<button class=" btn-primary btn-primary" asp-controller="Treatment" asp-action="Create" asp-route-patientId="#Model.PatientId" data-toggle="ajax-modal" data-target="add-treatment" id="addBtn">Add</button>
</div>
<div id="component">
<!-- My list view component: -->
#await Component.InvokeAsync("TreatmentList", new { patientFileId = #Model.PatientFile.Id })
</div>
<!-- My modal: -->
<div id="Modal" class="modal fade">
</div>
</div>
The list view component (I also tested it with a button, see comment):
<ul class="card-list">
#foreach (var treatment in Model)
{
<li class="list-item-card">
<!-- Doesn't work: -->
<a asp-controller="Treatment" asp-action="Details" asp-route-id="#treatment.Id" data-toggle="ajax-modal" data-target="Modal" id="detailCard">
<h5>#treatment.Type</h5>
<p>#treatment.Date</p>
<p>#treatment.Employee.FirstName #treatment.Employee.LastName</p>
</a>
<!-- Does work: -->
<button asp-controller="Treatment" asp-action="Details" asp-route-id="#treatment.Id" data-toggle="ajax-modal" data-target="Modal" id="detailCard">Details</button>
</li>
}
</ul>
And finally the Details.cshtml (the to be opened view in the modal):
#using Core.Domain
#model Treatment
#{
Layout = null;
}
<h3>#Model.Type</h3>
<div class="modal-diaglog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="detailTreatmentLabel">Treatment details</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>#Model.Date</p>
<p>#Model.Description</p>
<p>#Model.Employee.FirstName #Model.Employee.LastName</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
Does anyone know if it is possible to open a seperate view in a modal using a hyperlink? And if not, would there be a workaround to still be able to click on the list item itself?
Thank you in advance!
You can try to call a js function when click hyperlink:
ViewComponent:
<ul class="card-list">
#foreach (var treatment in Model)
{
<li class="list-item-card">
<!-- Doesn't work: -->
<a href="javascript:Details(#treatment.Id)">
<h5>#treatment.Type</h5>
<p>#treatment.Date</p>
<p>#treatment.Employee.FirstName #treatment.Employee.LastName</p>
</a>
<!-- Does work: -->
<button asp-controller="Treatment" asp-action="Details" asp-route-id="#treatment.Id" data-toggle="ajax-modal" data-target="Modal" id="detailCard">Details</button>
</li>
}
</ul>
page js:
#section Scripts {
<script type="text/javascript">
function Details(id) {
$.ajax({
type: "GET",
url: "Treatment/Details?id="+id,
success: function (res) {
$("#Modal").html(res);
$("#Modal").modal();
}
});
}
$("#addBtn").click(function () {
$.ajax({
url: $(this).attr("formaction"),
}).done(function(res) {
$("#Modal").html(res);
$("#Modal").modal();
})
});
$("#detailCard").click(function () {
$.ajax({
url: $(this).attr("formaction"),
}).done(function(res) {
$("#Modal").html(res);
$("#Modal").modal();
})
});
</script>
}
I want to generate a MVC partial view with a Bootstrap 5 accordion. Here is the code:
Index.cshtml
#{
ViewData["Title"] = "Home Page";
}
<input type="button" class="load_btn" id="1" value="Load" />
<div id="pv-1"></div>
#section Scripts
{
<script type="text/javascript">
$(function () {
$(".load_btn").click(function () {
var div_id = 'pv-' + $(this).attr('id');
$.ajax({
type: "GET",
url: "/Home/PV",
success: function (response) {
$('#' + div_id).empty();
$('#' + div_id).html(response);
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
alert(response.responseText);
}
});
});
});
</script>
}
HomeController.cs
...
namespace WebApplication4.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult PV()
{
return View("_PartialView");
}
...
}
}
_PartialView.cshtml
#{
Layout = null;
}
<link rel="stylesheet" href="~/lib/bootstrap/css/bootstrap.css" />
<div class="accordion" id="2">
<div class="accordion-item">
<h2 class="accordion-header" id="h-1">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#c-1" aria-expanded="false" aria-controls="c-1">
First Button
</button>
</h2>
<div id="c-1" class="accordion-collapse collapse" aria-labelledby="h-1">
<div class="accordion-body">
TO DO
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header" id="h-2">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#c-2" aria-expanded="false" aria-controls="c-2">
Second Button
</button>
</h2>
<div id="c-2" class="accordion-collapse collapse" aria-labelledby="h-2">
<div class="accordion-body">
TO BE
</div>
</div>
</div>
</div>
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/bootstrap/js/bootstrap.bundle.js"></script>
Click the "Load" button, I can get the Accordion successfully, and I can also expand each item, but then I cannot collapse the expanded items.
With another test, I put the accordion in the Index.cshtml directly (without using a Partial View ).
Index.cshtml
Exactly the same code with _PartialView.cshtml
And the Accordion works smoothly (can expand and collapse).
Could you help me on this strange behavior?
The css file and js files shouldn't be present in the _PartialView.cshtml file. The strange behavior (cannot collapse after expanded) was fixed after removing those files.
New code for _PartialView.cshtml
#{
Layout = null;
}
<div class="accordion" id="2">
<div class="accordion-item">
<h2 class="accordion-header" id="h-1">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#c-1" aria-expanded="false" aria-controls="c-1">
First Button
</button>
</h2>
<div id="c-1" class="accordion-collapse collapse" aria-labelledby="h-1">
<div class="accordion-body">
TO DO
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header" id="h-2">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#c-2" aria-expanded="false" aria-controls="c-2">
Second Button
</button>
</h2>
<div id="c-2" class="accordion-collapse collapse" aria-labelledby="h-2">
<div class="accordion-body">
TO BE
</div>
</div>
</div>
</div>
I'm using ui-boostrap dropdown directive, and want to add my own ng-if to prevent showing it on some cases.
When adding this ng-if, even when it always returns "true" - the dropdown stop working (doesn't pop up). It seems the drop-down button stops receiving ng-click events (therefore not changing the isOpen state..).
HTML:
<!doctype html>
<html ng-app="ui.bootstrap.demo">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-animate.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.14.2.js"></script>
<script src="example.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div ng-controller="DropdownCtrl">
<!-- Single button -->
<div class="btn-group" uib-dropdown is-open="status.isopen">
<!-- When putting this ng-if, the drop down stops working -->
<div ng-if="isAllowed()">
<button id="single-button" type="button" class="btn btn-primary" uib-dropdown-toggle ng-disabled="disabled">
Button dropdown <span class="caret"></span>
</button>
<ul class="uib-dropdown-menu" role="menu" aria-labelledby="single-button">
<li role="menuitem">Action</li>
<li role="menuitem">Another action</li>
<li role="menuitem">Something else here</li>
<li class="divider"></li>
<li role="menuitem">Separated link</li>
</ul>
</div>
</div>
<script type="text/ng-template" id="dropdown.html">
<ul class="uib-dropdown-menu" role="menu" aria-labelledby="button-template-url">
<li role="menuitem">Action in Template</li>
<li role="menuitem">Another action in Template</li>
<li role="menuitem">Something else here</li>
<li class="divider"></li>
<li role="menuitem">Separated link in Template</li>
</ul>
</script>
</div>
</body>
</html>
JS:
angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('DropdownCtrl', function ($scope, $log) {
$scope.items = [
'The first choice!',
'And another choice for you.',
'but wait! A third!'
];
$scope.status = {
isopen: false
};
$scope.toggled = function(open) {
$log.log('Dropdown is now: ', open);
};
$scope.toggleDropdown = function($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.status.isopen = !$scope.status.isopen;
console.log('isOpen = ' + $scope.status.isopen);
};
$scope.isAllowed = function() {
return true;
}
});
Here is a plunker reproduce the problem:
http://plnkr.co/edit/8K9MUsDfFcjyiZvFdv5x?p=preview
Any ideas?
Take out the div with the ng-if on it and put the ng-if on the enclosing div, i.e.
<div class="btn-group" uib-dropdown is-open="status.isopen" ng-if="isAllowed()">
<button id="single-button" type="button" class="btn btn-primary" uib-dropdown-toggle ng-disabled="disabled">
Button dropdown <span class="caret"></span>
</button>
<ul class="uib-dropdown-menu" role="menu" aria-labelledby="single-button">
<li role="menuitem">Action</li>
<li role="menuitem">Another action</li>
<li role="menuitem">Something else here</li>
<li class="divider"></li>
<li role="menuitem">Separated link</li>
</ul>
</div>
Basically the use case is this:
I have a form that the user is filling out a form and accidentally click another link. In the pagebeforehide event, I want to stop the navigation. I tried to do
event.stopPropagation();
event.preventDefault();
but neither of them stop the navigation from happening. Does anyone have any idea how to do this? It's not immediately clear how to handle this.
event.preventDefault(); should work if all your code is in the right places. Here is a simple example:
<div data-role="page" id="page1">
<div data-role="header" data-position="fixed" data-tap-toggle="false">
<h1>My page</h1>
</div>
<div role="main" class="ui-content">
<label for="text-basic">Type 1 to allow navigation:</label>
<input type="text" name="text-basic" id="text-basic" value="" />
<br /><br />
<a class="ui-btn" href="#page2">Go to page 2</a>
</div>
<div data-role="footer" data-position="fixed" data-tap-toggle="false">
<h1>My page footer</h1>
</div>
</div>
<div data-role="page" id="page2">
<div data-role="header" data-position="fixed" data-tap-toggle="false">
<h1>My page 2</h1>
</div>
<div role="main" class="ui-content">
page 2
<a class="ui-btn" href="#page1">Go back to page 1</a>
</div>
<div data-role="footer" data-position="fixed" data-tap-toggle="false">
<h1>My page footer</h1>
</div>
</div>
I have 2 pages and only want the user to go to page 2 if he/she types "1" into the text box.
$(document).on("pagecreate", "#page1", function(){
$( "body" ).on( "pagecontainerbeforetransition", function( event, ui ) {
var currPage = $( "body" ).pagecontainer( "getActivePage" ).prop("id");
if (currPage == "page1"){
var val = $("#text-basic").val();
if (val != "1") {
alert("you must enter 1 in the text box!");
event.preventDefault();
} else {
$("#text-basic").val("");
}
}
});
});
In the pagecontainer widget pagecontainerbeforetransition event, I see which page we are currently looking at, if it is page1, then check what the user has entered in the textbox. If I do not find the string "1", prevent navigation and alert the user.
Here is a working DEMO
i am newly working for jquery mobile panels, i have tried the reference of this jsfiddle link here http://jsfiddle.net/Gajotres/vds2U/47/ , i just added jquery ajax call to parse xml data. My problem is after searching city and keyword in page1, ajax call shows success alert but not trigger to page2. I want to display data in page2. If i click on page2 in jquery panel it appends the searched data. What wrongs here If any changes need to do please help me. My code is like below:
html code:
<div data-role="panel" data-display="push" data-theme="b" id="nav-panel" data-position="left">
<h2> All Pages </h2>
<ul data-role="listview" data-icon="false">
<li data-icon="delete">Close menu</li>
<li>Main panel page</li>
<li>page 1 page</li>
<li>page 2 Page</li>
</ul>
</div>
<div data-role="page" id="panel-main" data-title="Panel main" data-theme="a" >
<div data-role="header" data-position="fixed">
<h1 id="header1">main panel</h1>
Menu
</div>
<div role="main" class="ui-content"">
<p id="para1">main panel</p>
</div>
</div>
<div data-role="page" id="page1" data-theme="a">
<div data-role="header" data-position="fixed">
<h1>page 1</h1>
Menu
</div>
<div data-role="main" class="ui-content">
<div class="text"><input type="text" id="p1" name="p1" placeholder="Location...." class="user-text1 textboxes" data-clear-btn="true" data-mini="true" />
</div>
<div class="height"></div>
<div class="text"><input type="text" id="p2" name="p2" placeholder="Term...." class="user-text2 textboxes" data-clear-btn="true" /></div>
<div class="height"></div>
<button type="submit" id="searchBtn" data-theme="f" data-corners="false"/>Search Events</button>
</div>
</div>
<div data-role="page" id="page2" data-theme="a" >
<div data-role="header" data-position="fixed">
<h1>page 2</h1>
Menu
</div>
<div data-role="main" class="ui-content">
<ul id="list" data-role="listview" data-icon="false" data-autodividers="true" data-theme="c"></ul>
</div>
</div>
<script>
$(function () {
$("[data-role=panel]").enhanceWithin().panel();
});
javascript code:
$(document).ready(function(){
$("#searchBtn").bind("click",function(){
// alert('success');
$.mobile.loading('show');
var p1=$("#p1").val();
var p2=$("#p2").val();
$.ajax({
type: 'GET',
url:"http://api2.yp.com/listings/v1/search?searchloc="+p1+"&term="+p2+&format=xml&key=5cbqjx3cdl",
dataType: "xml",
success: function(data){
//alert('success');
$(data).find("searchListing").each(function(){
var name=$(this).find("businessName").text();
var id=$(this).find("listingId").text();
window.location.href="#page2";
//$.mobile.changePage("#page2");
$.mobile.loading('hide');
$("#list").append('<li>name:'+ name +'</li>');
$("#list:visible").listview("refresh");
});//ajax ends
});
});
Use the jQM pagecreate instead of $(document).ready, then in the success function, append the new items to the list, navigate to page2 using pagecontainer("change"), then refresh the list:
$(document).on("pagecreate", "#page1", function(){
$("#searchBtn").on("click" ,function(){
// your AJAX code
success: function(data){
//alert('success');
$(data).find("searchListing").each(function(){
var name=$(this).find("businessName").text();
var id=$(this).find("listingId").text();
$.mobile.loading('hide');
$("#list").append('<li>name:'+ name +'</li>');
$("body").pagecontainer( "change", "#page2", {} );
$("#list").listview("refresh");
});
});
});
Simplified DEMO
I have left the AJAX out of the demo. I am just appending 3 items and then changing page.