Category Archives: CSS
Passing and Retrieving Data from Booststrap Modal
First of all add following modal code to your HTML.
<div class=”modal-dialog”>
<div class=”modal-content”>
<div class=”modal-header”>
<button type=”button” class=”close” data-dismiss=”modal” aria-hidden=”true”></button>
<h4 class=”modal-title”>Confirmation</h4>
</div>
<div class=”modal-body”>
<p> Are you sure you want to delete? </p>
<input type=”hidden” id=”objectId”/>
</div>
<div class=”modal-footer”>
<button type=”button” data-dismiss=”modal” class=”btn dark btn-outline”>Cancel</button>
<button id=”confirmDelete” type=”button” data-dismiss=”modal” class=”btn green”>Continue</button>
</div>
</div>
</div>
As you can in above code I have added one hidden field in modal where we will store object id.
Now following will your code of delete button.
<a id=”deleteButton” data-id=”{{$object->id}}” data-toggle=”modal” href=”#deleteConfirmation” class=”btn btn-danger btn-sm”>
As soon as you click on delete button modal will be displayed.
Now add following code to your JavaScript.
$(document).on(“click”, “#deleteButton”, function () {
var id = $(this).data(‘id’);
jQuery(‘#deleteConfirmation .modal-body #objectId’).val( id );
});
$(‘#deleteConfirmation .modal-footer button’).on(‘click’, function (e) {
var $target = $(e.target); // Clicked button element
$(this).closest(‘.modal’).on(‘hidden.bs.modal’, function () {
if($target[0].id == ‘confirmDelete’){
$id = $(‘#deleteConfirmation .modal-body #objectId’).val();
}
});
});
As you can in above code we have added click event on delete button and setting id in modal hidden field.
Then we are adding event for confirm button in modal window and getting value stored in hidden field and then you can call the APIs or submit form whatever you want.
Hope this helps you.
HTML Fit Content to Available Space – Dynamic Font Size
Recently in one of my project we have to fit content to available screen height. Basically it was cross platform mobile application built in HTML 5 and JavaScript. On screen there was a card with content. Card height and width as set to screen height and width. Now the problem was content can big or small and there should not be scrollbar. So what I did is I reduced the size of font till the content fits available width and height. This I did in JavaScript. So here in blog I am going to explain how to do this.
First of all add onload method to your body tag.
<body onload=”resizeFontSizeInit()”>
<div id=”contentContainer” style=”width:100%;height:100%;overflow:hidden;”>
<div id=”content”>
Dynamic Content Here
</div>
</div>
</body>
Now add following function to JavaScript
function resizeFontSizeInit(){
resizeFont(0.1);
}
function resizeFont(emSize){
//first get the screen height
var screenHeight = document.documentElement.clientHeight;
//Now set the font size to content div
var contentDiv = document.getElementById(‘content’);
contentDiv.style.fontSize = String(emSize)+”em”;
//Now check the height of screen to height of content div
var contentHeight = contentDiv.clientHeight;
//Check if content height is less than screen height.
if(contentHeight < screenHeight){
//Increase font size and call the same function recursively.
emSize = emSize + 0.1;
resizeFont(emSize);
}else{
//content height is greater than screen size so time to stop recursion.
//set fontsize to previous size.
revertFontSizeToPreviousSize(emSize);
}
}
function revertFontSizeToPreviousSize(emSize){
emSize = emSize – 0.1;
var contentDiv = document.getElementById(‘content’);
contentDiv.style.fontSize = String(emSize)+”em”;
}
So basically we start with 0.1 em font size and keep increasing it till the content height is greater than screen height. As soon as it’s bigger we have to stop and reset it to previous size. This way no matter what your content is, it will fit to screen without scroll bar.
Sencha Touch List, Different background colors for Group Header
Here I added background color in each items of the list and then grouped it based in certain field. For example.
Ext.define(‘MyApp.model.MyModel’, {
extend: ‘Ext.data.Model’,
config:{
fields: [
{ name:’id’, type:’int’},
{ name:’group_background_color’, type:’string’},
{ name:’item_name’, type:’auto’},
{ name: ‘group’, type: ‘string’}
]
}
})
Then we have following Store definition.
Ext.define(‘MyApp.store.MyStore’, {
extend: ‘Ext.data.Store’,
config: {
model: ‘MyApp.model.MyModel’,
autoLoad: true,
grouper: {
groupFn: function (item) {
return item.get(‘group’);
}
},
proxy: {
type:’ajax’,
actionMethods:{
read: ‘POST’
},
url:’items.json’,
reader:{
type:’json’,
rootProperty:’items’
}
}
}
});
Now we have our list where we will show this data.
{
xtype: ‘list’,
id:’myList’,
itemId:’myList’,
scrollable: true,
grouped: true,
itemTpl:'<div>{item_name}</div>’ ,
store: ‘MyStore’
}
This will create list with groups and group header. If you use default Sencha Theme the group header will have blue as background color. We have to change it to our colors dynamically. For that we will update on list refresh. List refresh event is fired when list is visible first time and there is some change in data in store or list store is loaded. Add refresh event in controller.
control:{
‘#myList’:{
refresh: ‘refreshListHeaders’
}
}
And here is the function.
refreshListHeaders: function(list){
if(list.groups){
var groupElements = document.getElementsByClassName(‘x-list-header’);
for(var i=0;i <
groupElements.length i++){
var firstChild = list.groups[i].children[0]; var backGroundColor = firstChild.data.banner_background_color;
groupElements[i+1].style.setProperty(‘background-color’, backGroundColor, ‘!important’);
}
}
}
So our logic is very simple. We get all the elements with class name “x-list-header” . That gives us all the header elements. Now for each group we are getting background color from the first item of the group and set it as background color to group header.
Alternate Way to Create Image Button in Sencha Touch
Recently I was working on a sencha touch project where I need to use a nig round image in sencha touch button. We all now that we can use images inside button using iconCls and iconMask property. That will put small icons inside your button along with button rounded corners and background colors. But in my case we don’t need button background color or rounded border of button. We just need a round image with transparent background and user should be able to click on it. There are number of ways for doing it.
First we can use xtype : image and put image url to it. But sometimes tap event does not work over image. So this not an option. Other option is to use panel and put image as html inside and add tap evet to panel body. But that will be on complete panel body. So in case of round image if user tapped outside the image but inside a panel, tap event will be fired. So here is the third solution.
As we know that Ext.button extends the Ext.component so we can add html to component. So we can add image as html as follow.
{
After adding this image is added inside the button and but still have button background colors and rounded border. This you can remove by updating your sencha touch CSS. Find the following class in sencha touch CSS.
.x-button, .x-button.x-button-back:after, .x-button.x-button-forward:after, .x-toolbar .x-button, .x-toolbar .x-button.x-button-back:after, .x-toolbar .x-button.x-button-forward:after {
background-image: none;
background-color: #ccc;
background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #f2f2f2), color-stop(3%, #d9d9d9), color-stop(100%,#bfbfbf));
background-image: -webkit-linear-gradient(top, #f2f2f2,#d9d9d9 3%,#bfbfbf);
background-image: -moz-linear-gradient(top, #f2f2f2,#d9d9d9 3%,#bfbfbf);
background-image: -o-linear-gradient(top, #f2f2f2,#d9d9d9 3%,#bfbfbf);
background-image: linear-gradient(top, #f2f2f2,#d9d9d9 3%,#bfbfbf);
}