how to disable a DataTables/TableTools button - datatables

I'm using DataTable 1.10 and TableTools 2.2.1.
Given the following snipped I would like to disable/enable the edit-button.
var myTable = $("#myTable ").DataTable({
tableTools : {
"aButtons" : [ {
"sExtends" : "text",
"sButtonText" : "Edit",
"fnClick" : function(nButton, oConfig, oFlash) {
/* some stuff */
}
}]
}
})
Is there a possibility to do this at runtime?
Thanks a lot

This was a good question! Seems that the fnClick in
dataTable.tabletools().fnSettings().buttonSet[id].fnClick
only is a reference to the event stored elsewhere, not accessible (changing fnClick on the API has no effect). However, you can use the predefined class DTTT_disabled and check for that in your fnClick-handler :
var dataTable = $("#example").DataTable({
sDom: 'TC',
oTableTools : {
aButtons : [{
sExtends : "text",
sButtonText : "Edit",
fnClick : function(nButton, oConfig, oFlash) {
if ($(nButton).hasClass('DTTT_disabled')) return;
alert('edit button clicked');
}
}]
}
});
example with a checkbox enabling or disabling the button :
$("#enable").click(function() {
if ($(this).is(':checked')) {
$('.DTTT_button_text').removeClass('DTTT_disabled');
} else {
$('.DTTT_button_text').addClass('DTTT_disabled');
}
});
see demo -> http://jsfiddle.net/ev2N2/

Related

Sencha touch 2 custom list item button tap not fired

I am developing a Hybrid Mobile App over sencha touch 2.
Now I was in a need of a custom component to be specific a custom list item consisting of a button along with.
My view has rendered as i wanted to but the button that is added to the list item is not firing the TAP event as expected. Instead on every tap, the ITEMTAP event is fired which is creating a bit of mess.
Can someone suggest me where to look to make this work ?
Below is the code for the custom component that i created:
var listView = {
xtype : "list",
id : "desk-list-search-results",
cls : "desk-list-search-results-cls",
selectedCls : "",
defaultType:"desksearchlistitem",
store : "deskstore",
flex : 2
};
This is the code for the custom component
Ext.define("MyApp.view.DeskSearchListItem",{
extend:"Ext.dataview.component.ListItem",
requires:["Ext.Button"],
alias:"widget.desksearchlistitem",
initialize:function()
{
this.callParent(arguments);
},
config:{
layout:{
type:"hbox",
align:"left"
},
cls:'x-list-item desk-search-list-item',
title:{
cls:"desk-list-item",
flex:0,
styleHtmlContent:true,
style:"align:left;"
},
image:{
cls:"circle_image",
width:"28px",
height:"28px"
},
button:{
cls:'x-button custom-button custom-font bookdesk-button',
flex:0,
text:"Book",
width:"113px",
height:"46px",
hidden:true
},
dataMap:{
getTitle:{
setHtml:'title'
}
}
},
applyButton:function(config)
{
return Ext.factory(config,Ext.Button,this.getButton());
},
updateButton:function(newButton,oldButton)
{
if(newButton)
{
this.add(newButton);
}
if(oldButton)
{
this.remove(oldButton);
}
},
applyTitle:function(config)
{
return Ext.factory(config,Ext.Component,this.getTitle());
},
updateTitle:function(newTitle,oldTitle)
{
if(newTitle)
{
this.add(newTitle);
}
if(oldTitle)
{
this.remove(oldTitle);
}
},
applyImage:function(config)
{
return Ext.factory(config,Ext.Component,this.getImage());
},
updateImage:function(newImage,oldImage)
{
if(newImage)
{
this.add(newImage);
}
if(oldImage)
{
this.remove(oldImage);
}
}
})
Finally got a solution to this,
It can be done into the view using the listeners object.
Here is the code sample for it.
listeners:{
initialize:function()
{
var dataview = this;
dataview.on("itemtap",function(list,index,target,record,event){
event.preventDefault();
});
dataview.on("itemswipe",function(list,index,target,record,event){
if(event.direction === "right")
{
var buttonId = target.element.down(".bookdesk-button").id;
var buttonEl = Ext.ComponentQuery.query("#"+buttonId)[0];
if(Ext.isObject(buttonEl))
{
buttonEl.setZIndex(9999);
buttonEl.show({
showAnimation:{
type:"slideIn",
duration:500
}
});
var listeners = {
tap:function(btn,e,opt)
{
console.log("Button Tapped");
}
};
buttonEl.setListeners(listeners);
}
else
{
console.log("This is not a valid element");
}
}
});
}
}
Thanks Anyways.

Failed to change the text of a titlebar in a container with sencha touch 2.1

I want to create an application with users login and logout. When the user logs in ,the system will store the user information such as username,sessionid into the localstorage and then go to the index page. The index page has a titlebar with username on the left and a button to log out.But it failed to get the data stored in the localstorage that was stored when a user logs in when the index page appears.
My code is as below:
index page:
Ext.define('MyApp.view.MyPanel', {
extend : 'Ext.Panel',
id : 'myPanel',
config : {
xtype : 'panel',
layout : 'vbox',
height : '100%',
width : '100%',
items : [
{
xtype : 'titlebar',
docked : 'top',
title : 'my panel',
items : [
{
align : 'left',
id : 'portal-username',
text : 'qq' //i used localStorage.getItem('userName') first,but not as good.
},
{
align : 'right',
text : 'log out',
listeners : {
tap : function(){
var userName=null;var rememberPassword=null;var password=null;
if(userName=Ext.getCmp('userName'))userName.setValue('');
if(password=Ext.getCmp('password'))password.setValue('');
if(rememberPassword=Ext.getCmp('rememberPassword'))rememberPassword.setChecked(false);
//localstorage
localStorage.removeItem('userName','');
localStorage.removeItem('password','');
localStorage.removeItem('SessionId','');
localStorage.removeItem('rememberPassword','');
var oldp=Ext.getCmp('loginPanel');if(oldp)oldp.destroy();
var newActivePanel = Ext.create('MyApp.view.LoginPanel');
Ext.Viewport.add(newActivePanel);
Ext.Viewport.animateActiveItem(newActivePanel,{type:'slide',direction:'left'});
}
}
}
]
}
]
}
});
part of login panel:
items : [
{
xtype : 'button',
text : 'login',
ui : 'action',
handler : function() {
var userName = Ext.getCmp('userName').getValue();
var password = Ext.getCmp('password').getValue();
var rememberPassword = Ext.getCmp('rememberPassword').getChecked()+ '';
if(!userName||!password){
Ext.Msg.alert('prompt','login failure,try again!',Ext.emptyFn);
}
else Ext.Ajax.request({
url : remoteServer,
params : {
loginName: userName,
passWord : password,
SessionId : localStorage.getItem('SessionId');
},
method : 'POST',
callback : function(options,success,response) {
if (success) {
var result = Ext.JSON.decode(response.responseText.trim());
var status = result.status;
if (status == 'success') {
if (rememberPassword == 'true')
localStorage.setItem('rememberPassword',rememberPassword);
localStorage.setItem('userName',userName);
localStorage.setItem('password',password);
localStorage.setItem('SessionId',result.SessionId);
var oldp=Ext.getCmp('MyPanel');if(oldp)oldp.destroy();
var newActivePanel = Ext.create('MyApp.view.MyPanel');
Ext.Viewport.add(newActivePanel);
Ext.Viewport.animateActiveItem(newActivePanel,{type:'slide',direction:'left'});
} else {
localStorage.setItem('userName','');
localStorage.setItem('password','');
localStorage.setItem('JSessionId','');
localStorage.setItem('rememberPassword','');
Ext.Msg.alert('prompt','login failure,try again!',Ext.emptyFn);
}
} else {
Ext.Msg.alert('prompt','login failure,try again!',Ext.emptyFn);
}
}
});
}
}
]
Below is my controller :
Ext.define('MyApp.controller.MyController',
{
extend : 'Ext.app.Controller',
id : 'MyController',
config : {
control : {
MyPanel : {
show : 'panelActive'
},
},
refs : {
MyPanel : '#myPanel'
}
}//config
,panelActive : function(){
Ext.getCmp('portal-username').setText(localStorage.getItem('userName'));
}
});
After Following line
Ext.Viewport.animateActiveItem(newActivePanel,{type:'slide',direction:'left'});
in login handler function just set the username using
Ext.getCmp('portal-username').setText(userName);

How to hide button is Sencha Touch 2.1

I've found similar questions on this site but none of the answers has been working for me. This is button I want to hide (view):
{
xtype: 'button',
id: 'btn_messenger',
text: 'Messenger'
}
Controller for that view has init function:
init : function() {
var me = this;
me.callParent();
me.hideMessengerButton(me);
}
This is the function that should hide button:
hideMessengerButton: function(me) {
var user = me.getLoggedUser(); // return user or undefined
if (user == undefined) {
Ext.select('#btn_messenger').hide(); // Does nothing
}
}
I've tried these options:
Ext.getCmp('btn_messenger').hide(); // Ext.getCmp('btn_messenger') returns undefined
Ext.getCmp('#btn_messenger').hide(); // Ext.getCmp('#btn_messenger') returns undefined
In controller's refs there is btn_messenger: '#btn_messenger', so I've tried:
this.getBtn_messenger().hide() // this.getBtn_messenger() returns undefined
Thanks for help in advance.
PS.: I don't know if this matters but the view mentioned above is not the main view. It is pushed after button tap on the main view.
EDIT:
Here is the controller:
Ext.define('First.controller.HomePage', {
extend : 'First.controller.Controller',
requires : ['First.view.Main', 'First.view.HomePage'],
config : {
refs : {
pnl_home: 'pnl_home',
btn_messenger : '#btn_messenger'
},
control : {
btn_messenger : {
tap : 'btn_openMessenger'
}
pnl_home: {
show: 'hideMessengerButton'
}
},
},
init : function() {
var me = this;
me.callParent();
},
btn_openMessenger : function() {
Ext.Msg.alert('Open', 'Messenger');
},
/**
* Hide meseenger button when user's not logged in
*/
hideMessengerButton : function() {
var me = this;
var user = me.getLoggedUser();
if (user == undefined) {
me.getBtn_messenger().setHidden(true);
}
}
});
http://docs.sencha.com/touch/2-1/#!/api/Ext.Button-method-setHidden
in your action
replaces refs
refs : {
btnMessenger: '#btn_messenger'
},
then when you need this button on action use next
this.getBtnMessenger().setHidden(true)
also on init button are not yet added to dom as i remember
lauch : function() {
this.hideMessengerButton();
},
hideMessengerButton: function() {
var user = this.getLoggedUser(); // return user or undefined
if (user == undefined) {
this.getBtnMessenger().setHidden(true); // Does nothing
}
}
I've added pnl_home: 'pnl_home', to refs and called show event on the panel. Modified code can be found above, in my initial post.

Ext JS StatusBar with clock

I'm trying to create a status bar with clock.
I found example in sencha examples, but I'm trying to build it more object oriented way.
Ext.define('Urlopy.Components.Statusbar', {
extend : 'Ext.ux.statusbar.StatusBar',
id : 'basic-statusbar',
defaultText : 'Wczytane...',
defaultIconCls : 'x-status-valid',
iconCls : 'x-status-valid',
autoClear : 3000,
initComponent : function() {
this.currentUserDisplay = Ext.create('Ext.toolbar.TextItem');
this.currentUserDisplay.setText('Not logged in!');
this.timeDisplay = Ext.create('Ext.toolbar.TextItem');
this.timeDisplay.setText(Ext.Date.format(new Date(), 'Y-m-d H:i:s'));
Ext.apply(this, {
items : [this.currentUserDisplay, {
xtype : 'tbseparator'
}, this.timeDisplay],
listeners : {
render : {
fn : function() {
Ext.TaskManager.start({
run : function() {
this.timeDisplay.setText(Ext.Date.format(new Date(),'Y-m-d H:i:s'));
},
interval : 1000
});
},
delay : 100
}
}
});
this.callParent();
},
setCurrentUser : function(username) {
this.currentUserDisplay.setText('Logged as' + ": " + username);
},
startLoad : function(message) {
if (message !== null) {
this.showBusy({
text : message,
iconCls : "x-status-busy"
});
} else {
this.showBusy();
}
},
endLoad : function() {
this.clearStatus({
useDefaults : true
});
}
});
The first time component shows it displays date and time correct, but it doesn't update.
Problem is probably TaskManager or my listeners.
Instead of them I've tried doing this:
initComponent : function() {
this.currentUserDisplay = Ext.create("Ext.toolbar.TextItem");
this.currentUserDisplay.setText('Nie jesteś zalogowany!');
this.timeDisplay = Ext.create("Ext.toolbar.TextItem");
this.timeDisplay.setText(Ext.Date.format(new Date(), "Y-m-d H:i:s"));
Ext.apply(this, {
items : [this.currentUserDisplay, {
xtype : 'tbseparator'
}, this.timeDisplay]
});
this.callParent();
var task = {
run : function() {
this.timeDisplay.setText(Ext.Date.format(new Date(), "Y-m-d H:i:s"));
},
interval : 1000
}
Ext.TaskManager.start(task);
},
but no luck :(
I know that this is probably a tiny mistake, but can't find it.
What you have is a scope issue, this in your run function doesn't point to the status bar. you need to add a scope: this after the interval.
see example http://jsfiddle.net/nscrob/kR9ev/17/

Sencha touch: cannot get reference to items in panel

I'm am trying to get MVC to work in my first application and am trying to change cards based button clicked in toolbar.
I'm getting a runtime error when I click the hello or world button in the toolbar: TypeError: Cannot read property 'items' of undefined on the line:
var exerciseListPanel = this.items.items[0], // CODE ERRORS HERE!
I'm having a problem in getting a reference to the items in the panel, WHY?
Here is the complete code for the panel:
MyApp.views.HomeScreenPanel = Ext.extend(Ext.Panel, {
layout : 'card',
cardSwitchAnimation: 'slide',
initComponent : function() {
this.dockedItems = this.buildDockedItems();
this.items = this.buildItemList();
MyApp.views.HomeScreenPanel.superclass.initComponent.call(this);
},
buildItemList : function(){
return [
new Ext.Panel({html:"hello"}),
new Ext.Panel({html:"world"}),
];
},
buildDockedItems : function(){
return [
this.buildTopDockToolbar(),
this.buildBottomDockToolbar(),
];
},
buildTopDockToolbar : function(){
return {
xtype : 'toolbar',
dock : 'top',
title : 'My first MVC app',
};
},
buildBottomDockToolbar : function(){
return this.NavigationToolbar();
},
NavigationToolbar : function(){
return{
xtype : 'toolbar',
dock : 'bottom',
defaults : {
handler : this.NavigationToolbarHandler,
controller: 'NavigationBarController'
},
items : [
{
text : 'hello',
action: 'hello'
},
{
text : 'world',
action : 'world'
}]
};
},
NavigationToolbarHandler : function(btn) {
var exerciseListPanel = this.items.items[0], // CODE ERRORS HERE!
workoutListPanel = this.items.items[1];
Ext.dispatch({
controller : btn.controller,
action : btn.action,
views : {
exerciseListPanel: exerciseListPanel,
workoutListPanel: workoutListPanel,
}
})
},
});
Ext.reg('HomeScreenPanel',MyApp.views.HomeScreenPanel);
Here is the code for the controller but I don't there is a problem here:
Ext.regController('NavigationBarController', {
world : function(dataObj) {
Ext.Msg.alert(
'world!',
'world button pressed from toolbar',
Ext.emptyFn
);
MyApp.views.HomeScreenPanel.setActiveItem(dataObj.views.worldPanel); // CORRECT SYNTAX????
},
hello : function(dataObj) {
Ext.Msg.alert(
'hello',
'hello button pressed',
Ext.emptyFn
);
MyApp.views.HomeScreenPanel.setActiveItem(dataObj.views.helloPanel); // CORRECT SYNTAX?????
}
});
It is probably just a scope issue. try:
exerciseListPanel = MyApp.views.HomeScreenPanel.items.items[0];
In this context this is probably the button.
Items is a MixedCollection so use the get function.
http://docs.sencha.com/touch/1-1/#!/api/Ext.util.MixedCollection
MyApp.views.HomeScreenPanel.items.get(0);
Bit late but for anyone else googling in the future ;)