Why jQuery mobile style not applied when table created dynamically - jquery-mobile

I use jQuery mobile 1.4.5 in my project.
I create dynamically table with buttons by click. When table created the JQM style isn't apply.
Here is my HTML code:
<div data-role="page" id="page1">
<div data-role="header">
<h1>My page</h1>
</div>
<div data-role="content" class="ui-content">
<button onclick="CretaeTable()">Cretate table</button>
<div id="vectorLayerslist"></div>
</div>
Here is JavaScript code:
function CretaeTable(){
var vectorLayersList = $('#vectorLayerslist');
var arr = [{id:'124',Title:'qqq'},
{id:'589',Title:'www'},
{id:'45',Title:'eee'},
{id:'567',Title:'rrr'}]
var table = layersListTable(arr);
vectorLayersList.append(table);
}
function layersListTable(layers) {
// build the table
var frame = '<fieldset style="border: solid 1px #6b6b6b;">';
var smallHeader = '<legend>title</legend>';
var content = frame + smallHeader + '<table data-role="table" id="layersListEditable" data-mode="columntoggle:none" class="ui-responsive"><thead><tr></tr></thead>';
$.each($(layers), function () {
// we'll store the ID in HTML5 data-attribute for later
content += '<tr>';
// give classes to your buttons for later
content += '<td><button data-mini="true" data-inline="true" data-icon="edit" data-theme="b" type="button" class="edit">Edit</button></td>';
content += '<td><button data-mini="true" data-inline="true" data-icon="delete" data-theme="b" type="button" class="delete">Delete</button></td>';
content += '<td style="vertical-align: inherit;">' + this.Title + '</td>';
content += '</tr>';
});
content += '</table>';
content += '</fieldset>'
return content;
}
Here is JSFIDDLE.
Why JQM style isn't apply?

You jist only need to rebuild the table, and then maybe update the layout of the whole page content:
... your dynamic table creation
vectorLayersList.append(table);
// add this:
$("#layersListEditable").table().table("rebuild");
$(".ui-content").trigger("updatelayout");
Please, note: you may also adjust the button markup, something like this:
content += '<td><button class="ui-btn ui-mini ui-btn-inline ui-btn-icon-left ui-icon-edit">Edit</button></td>';
content += '<td><button class="ui-btn ui-mini ui-btn-inline ui-btn-icon-left ui-icon-delete">Delete</button></td>';
Here is your updated Fiddle: http://jsfiddle.net/aJUy8/105/

Related

populate Jquery Mobile gridView Dynamically

Is there is any way to fill jQuery Mobile GridView dynamically?
Normally we have to declare gridViews children class with ui-block_a,b,c as follows,
<div class="ui-grid-b">
    <div class="ui-block-a"><div class="ui-bar ui-bar-a" style="height:60px">Block A</div></div>
    <div class="ui-block-b"><div class="ui-bar ui-bar-a" style="height:60px">Block B</div></div>
    <div class="ui-block-c"><div class="ui-bar ui-bar-a" style="height:60px">Block C</div></div>
</div><!-- /grid-b -->
If there's any work around or plugins to achieve dynamic population for the gridview
You can add the grid dynamically. Here is an example:
Given page markup like this
<div data-role="page" id="page1">
<div id="cont" role="main" class="ui-content">
<button id="btnAdd">Add Dynamic Grid</button>
<div id="dynamicGrid"></div>
</div>
</div>
The script to add the grid on button click
$(document).on("pagecreate","#page1", function(){
$("#btnAdd").on("click", function(){
var html = '<div class="ui-grid-b">';
html += '<div class="ui-block-a"><button>A button</button></div>';
html += '<div class="ui-block-b"><input type="text" value="text 1" /></div>';
html += '<div class="ui-block-c"><input type="checkbox" data-role="flipswitch" name="flip-checkbox-1" id="flip-checkbox-1"></div>';
html += '</div>';
$("#dynamicGrid").empty().append(html).enhanceWithin();
});
});
NOTE: call .enhanceWithin() to have jQM initialize widgets contained in the grid.
DEMO

The Scroll bar is not wrapped in the scrollable content area in jQuery mobile1.4.0

In my jQuery mobile app for android , I have a long list view inside a popup , I have used iscroll because overflow:auto is not working on android 2.3.6 , and scrolling now is working ok on all android devices after using iscroll , but the problem is that the scroll bar starts from the header of the popup to the end of the popup ( along the popup height) not the list view height "popup content" although i applied the iscroll to the div that contains the listview. How can I fix this problem and make the scroll appears a long the list height? please help me
This is the iscroll.js that i use jsfiddle
<head>
<script type="text/javascript" src="js/iscroll.js"></script>
</head>
<body>
<div data-role="page" id="index">
<div data-role="header" data-theme="b">Main</div>
<div data-role="content">
Show Popup
</div>
<div data-role="popup" id="MyPOPUP" data-position-to="window" data-corners="false" data- overlay-theme="a" data-dismissible="false">
<div data-role="header" data-theme="a">
<div style="text-align:center;float:center;padding-top:11px;">
<font size="6px" color="white">Countries</font>
</div>
</div>
<div id="scrollContent" class="content" data-role="content" style="background-color: white;">
<ul data-role="listview" id="countrieslist" style="margin: 0 !important;">
</ul>
</div>
</div>
</div>
</body>
JS code
$(document).on( 'pagecreate', '#index',function(){
var scrollObj;
// to make the popup take the screen width ,height
var window_Width = $( window ).width();
var window_Height = $(window).height();
$('#MyPOPUP').css('width', window_Width-30 );
$('#scrollContent').css('width', window_Width-30 );
$('#MyPOPUP').css('height', window_Height-(window_Height/4) );
$('#scrollContent').css('height', window_Height-(window_Height/4)-87 );
scrollObj = new iScroll('scrollContent', { vScrollbar: true, hScrollbar:false,fixedScrollbar : true,momentum: true});
for(var i=1;i<=50;i++) {
$('#countrieslist').append('<li id="'+i+'"><font>Country' + i +'</font></li>');
}
$('#countrieslist').listview('refresh');
setTimeout(function () {
scrollObj.refresh();
}, 0);
$('#Btn1').on('click', function(){
$(this).attr('href','#MyPOPUP');
});
$('#countrieslist').on('click','li', function() {
selected_elem = $(this).attr('id');
console.log('you selected' + selected_elem);
$('#MyPOPUP').popup('close');
});
});
CSS
#scrollContent{
padding: 0 !important;
}

append() to dynamically created tab

I am creating an editor application which requires dynamically appending HTML into the document. I am using JQuery UI tabs to divide the document into sections. I want to be able to dynamically add tabs and then use append() to add content into the tabs.
The page has 3 tabs when it loads and I want to be able to dynamically add more when required.
Currently my code works when appending content into the 3 non-dynamic tabs, and when creating a new tab. For some reason I cannot append any content into a new, dynamically created tab.
Why won't my code work for adding content to a dynamically created tab?
$(function () {
$('#addTab').click(function () {
var num_tabs = $('div#tabs ul li.tab').length + 1;
$('div#tabs ul').append(
'<li class="tab">Section ' + num_tabs + '</li>');
$('div#tabs').append(
'<div id="tab-' + num_tabs + '></div>');
$('#tabs').tabs("refresh");
$('#tabs').tabs("option", "active", -1); //makes the new tab active
});
});
//Insert content into the currently selected tab
function insertContent(content) {
var activeTab = $("#tabs").tabs('option', 'active');
activeTab += 1;
console.log(activeTab);
$("#tab-" + activeTab).append(content);
}
HTML after creating a 4th tab:
<div id="tabs" class="ui-tabs ui-widget ui-widget-content ui-corner-all">
<ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all" role="tablist">
<li class="tab ui-state-default ui-corner-top" role="tab" tabindex="0" aria-controls="tab-1" aria-labelledby="ui-id-1" aria-selected="false">Section 1</li>
<li class="tab ui-state-default ui-corner-top" role="tab" tabindex="-1" aria-controls="tab-2" aria-labelledby="ui-id-2" aria-selected="false">Section 2</li>
<li class="tab ui-state-default ui-corner-top" role="tab" tabindex="0" aria-controls="tab-3" aria-labelledby="ui-id-3" aria-selected="false">Section 3</li>
<li class="tab ui-state-default ui-corner-top ui-tabs-active ui-state-active" role="tab" tabindex="0" aria-controls="tab4" aria-labelledby="ui-id-7" aria-selected="true">Section 4</li></ul>
<div id="tab-1" aria-labelledby="ui-id-1" class="ui-tabs-panel ui-widget-content ui-corner-bottom" role="tabpanel" aria-expanded="false" aria-hidden="true" style="display: none;">Section Title:<input class="sectionTitle"><br><p contenteditable="true" class="cke_editable cke_editable_inline cke_contents_ltr cke_show_borders" tabindex="0" spellcheck="false" role="textbox" aria-label="Rich Text Editor, editor1" title="Rich Text Editor, editor1" aria-describedby="cke_83" style="position: relative;">Paragraph text</p></div>
<div id="tab-2" aria-labelledby="ui-id-2" class="ui-tabs-panel ui-widget-content ui-corner-bottom" role="tabpanel" aria-expanded="false" aria-hidden="true" style="display:none;">Section Title:<input class="sectionTitle"><p contenteditable="false" class="cke_editable cke_editable_inline cke_contents_ltr cke_show_borders" tabindex="0" spellcheck="false" role="textbox" aria-label="Rich Text Editor, editor2" title="Rich Text Editor, editor2" aria-describedby="cke_153" style="position: relative;">Paragraph text</p></div>
<div id="tab-3" aria-labelledby="ui-id-3" class="ui-tabs-panel ui-widget-content ui-corner-bottom" role="tabpanel" aria-expanded="false" aria-hidden="true" style="display: none;">Section Title:<input class="sectionTitle"><p contenteditable="false" class="cke_editable cke_editable_inline cke_contents_ltr cke_show_borders" tabindex="0" spellcheck="false" role="textbox" aria-label="Rich Text Editor, editor3" title="Rich Text Editor, editor3" aria-describedby="cke_223" style="position: relative;">Paragraph text</p></div>
</div>
After looking at the HTML generated following are the problems I found with your code:
1) The following line generated li with wrong Id i.e. tab4 instead of tab-4
$('div#tabs ul').append(
'<li class="tab">Section ' + num_tabs + '</li>');
change <a href="#tab' to <a href="#tab-'
2) The following line was missing " in id because of which the div wasn't appended in the tabs
$('div#tabs').append(
'<div id="tab-' + num_tabs + '"></div>');
Once you fix there, you'll see your code is working properly
See working fiddle

content of dynamically-added jQuery-ui tabs only showing once

I'm trying to create a tab set using jQuery UI that has some permanent tabs as well as some special purpose tabs. The special tabs are added temporarily: when the form they contain is submitted, the tabs are removed.
I've got this working except for one thing: after a tab is removed, if it is re-added later its content isn't shown, and I can't figure out why. I've distilled it down to this jsFiddle example, code also reposted below.
HTML:
<div id="tabs">
<ul>
<li>Foo</li>
</ul>
<div id="foo">
<h2>Foo Tab</h2>
</div>
<div id="bar" class="transient" style="display: none">
<h2><button type="button" class="close" style="float: right"><span class="ui-icon ui-icon-closethick">close</span></button>Bar Tab</h2>
</div>
<div id="baz" class="transient" style="display: none">
<h2><button type="button" class="close" style="float: right"><span class="ui-icon ui-icon-closethick">close</span></button>Baz Tab</h2>
</div>
</div>
<hr>
<button onClick="openTransientTab('bar', 'Bar')">Add Bar Tab</button>
<button onClick="openTransientTab('baz', 'Baz')">Add Baz Tab</button>
JavaScript:
$('#tabs').find('div.transient').find(".close").live('click', function() {
var footer_tabs = $('#tabs');
var tab_id = $(this).closest("div.transient").attr("id");
var index = footer_tabs.tabs("option", "selected");
footer_tabs.tabs("select", -1);
footer_tabs.tabs("remove", index);
});
function openTransientTab(id, title) {
var footer_tabs = $("#tabs");
footer_tabs.tabs("select", -1);
footer_tabs.tabs("select", "#" + id);
var selected = footer_tabs.tabs("option", "selected");
if (selected < 0) {
footer_tabs.tabs("add", "#" + id, title);
footer_tabs.tabs("select", "#" + id);
}
$("#" + id).css("display", "block");
}
$(function() {
var footer_tabs = $("#tabs");
footer_tabs.tabs({
collapsible: true,
selected: -1
});
});
When you load the page, the bar and baz tabs are created, but in their style, display is set to none which is why they are not visible originally. Inside the tab, when you hit the X, it actually removes the div for bar and baz completely. When you re-click to add the bar or baz tab after it closes, it recreates the div, but you are not putting anything within it. Add something like the following to once you create the tab.
document.getElementById("bar").innerHtml = whatever you want within it here
Before:
<div id="foo" class="ui-tabs-panel ui-widget-content ui-corner-bottom ui-tabs-hide">
<h2>Foo Tab</h2>
</div>
<div id="baz" class="transient" style="display: none">
<h2>
<button class="close" style="float: right" type="button">
<span class="ui-icon ui-icon-closethick">close</span>
</button>
Baz Tab
</h2>
</div>
<div id="bar" class="transient ui-tabs-panel ui-widget-content ui-corner-bottom" style="display: block;">
<h2>
<button class="close" style="float: right" type="button">
<span class="ui-icon ui-icon-closethick">close</span>
</button>
Bar Tab
</h2>
</div>
After opening and closing both
<div id="foo" class="ui-tabs-panel ui-widget-content ui-corner-bottom ui-tabs-hide">
<h2>Foo Tab</h2>
</div>
<div id="bar" class="ui-tabs-panel ui-widget-content ui-corner-bottom ui-tabs-hide" style="display: block;"></div>
<div id="baz" class="ui-tabs-panel ui-widget-content ui-corner-bottom" style="display: block;"></div>
After Collecter provided the key insight about what wasn't working, I found a nicer way to preserve the tab content for reuse. I changed my close function to the following:
$('#tabs').find('div.transient').find(".close").live('click', function() {
var footer_tabs = $('#tabs');
var tab = $(this).closest("div.transient");
var index = footer_tabs.tabs("option", "selected");
footer_tabs.tabs("select", -1);
footer_tabs.tabs("remove", index);
footer_tabs.append(tab);
});

Dynamiaclly append to grid row

Im trying to modify grid layout so that if the user clicks a grid row, another grid row is appended to the selected grid row and displays extra information about the selected grid. Is this possbible using jQuery mobile ?
<div class="ui-grid-a">
<div class="ui-block-a"><div class="ui-bar ui-bar-b" style="height:20px">A</div>
</div>
It's not fully functional but I hope this give you an idea, Live Example: http://jsfiddle.net/F6Zdp/44/
HTML:
<div data-role="page" id="gridplus">
<div data-role="content">
<div class="ui-grid-a" id="currentgrid">
<div class="ui-block-a"><div class="ui-bar ui-bar-e" style="height:50px">I'm a Block and text inside will wrap.</div></div>
<div class="ui-block-b"><div class="ui-bar ui-bar-e" style="height:50px">I'm a Block and text inside will wrap.</div></div>
</div>
<br />
<input type="button" id="addcolumn" name="addcolumn" value="Add Column"/>
<input type="button" id="removecolumn" name="removecolumn" value="Remove Column"/>
</div>
</div>
JS:
var currentGrid = $('#currentgrid');
var previousGrid;
$('#addcolumn').click(function() {
grid = currentGrid.attr('class');
previousGrid = grid;
nextGrid = grid.substring(0,grid.length-1)+String.fromCharCode(grid.charCodeAt(grid.length-1)+1);
$('#currentgrid').removeClass(grid).addClass(nextGrid);
$("#currentgrid").children().each(function() {
c = $(this);
block = c.attr('class');
nextBlock = block.substring(0,block.length-1)+String.fromCharCode(block.charCodeAt(block.length-1)+1);
if($('.'+nextBlock).length) {
doNothing = true;
} else {
c.after('<div class="'+nextBlock+'"><div class="ui-bar ui-bar-e" style="height:50px">I\'m a Block and text inside will wrap.</div></div>');
}
});
});
$('#removecolumn').click(function() {
grid = currentGrid.attr('class');
$('#currentgrid').removeClass(grid).addClass(previousGrid);
$("#currentgrid").children().last().remove();
}).page();
Use collapsable content - http://jquerymobile.com/demos/1.0a4.1/docs/content/content-collapsible.html
The content can be a jQuery grid I think.

Resources