Leap year

January 31st, 2015 No comments

Here’s an Javascript example how to check if a year is leap r not:

var isLeap = (((cYear%4)==0) || (((cYear%100)!=0) && ((cYear%400)==0)));

Where cYear variable where is stored the year number.

Categories: JavaScript Tags:

ExtJS4 – activate the function by clicking on the icon/text in the Grid object

December 1st, 2014 No comments

There are several ways you can create the action by clicking on the icon or the text that is displayed in the cell Grid object:

  • Using the attribute renderer when defining the Grid column object
  • Using xtype actioncolumn that allows a more flexible way of calling the function
  • Using event cellclick, which reacts to the whole cell, regardless of whether you clicked on a text (the last two are acting as link)

Using the attribute renderer

This is the simplest way to create the action. When defining attributes for a column add the attribute renderer. This attribute calls a function which is passed to the value of that cell (parameter v), HTML tag within which it is located on the page (parameter md) and record with all the data in the row (parameter r).

This attribute is used to change the view in the cell. If the function set “return v;” the effect is the same as if we did not set this attribute. Convenient if, for example, you want depending on the value set icon that defines status. In our case, is used to create the link. Click on the link calls a function show_comments_of_user().

The disadvantage of this method is that you can not forward the variable but only specific values. This example, in contrast to the other two is no variable “r” which represents the object with the data of the whole line.

Conclusion, This method use only when you want to display a value, or to call a function which forward concrete values.

columns : [
    ...
    {
        dataIndex:'comment',
        cls:'colComments',
        header:'User comments',
        tooltip:'User comments',
        width:24,
        menuDisabled:true,
        renderer:function(v, md, r) 
        {
            return '<a href="javascript:void(0)" '+
                      'onclick="show_comments_for_user(\'comment_view\');">'+
                   'View<\/a>'; 
        }
    }
    ...
]

Using xtype actioncolumn

This approach, and the next, is much more flexible when it comes to data forwarding function. Instead of attributes “renderer” column is set attribute “xtype” which assigns the value “actiontype”. This type enables the cell Grid object appears one or more icons that will create an action when you click on them. Each icon is an object that has a large number of its attributes. For a description of all attributes is best to check the official documentation.

We will process only the attribute “handler”, whose value is a function that will be executed when clicking on the icon. This function contains three variables: Grid object, row index amd cell index. The first two variables we use to get the record with all the data in a given row (line 17). In this way we get a third variable from the previous example. Then need to call the appropriate functions (line 18).

columns : [
    ...
    {
        dataIndex:'comment',
        cls:'colComments',
        header:'User comments',
        tooltip:'User comments',
        width:24,
        menuDisabled:true,
        xtype:'actioncolumn',
        items:[ 
            {
                iconCls:'icon-comments',
                tooltip:'View user comments',
                handler:function(grid, rIdx, cIdx) 
                {
                    var r = grid.getStore().getAt(rIdx);
                    show_comments_for_user(r, 'comment_view');
                    return false; 
                } 
            } 
        ]
    }
    ...
]

Using event cellclick

Unlike the previous example in which it is possible to set up multiple icons in the same column, in this example is possible to set only one icon per column. The reason is that the actions are performed on a cell in the Grid object and not the icon itself. This example has the simplest way of defining the columns. The only thing you need to keep in mind is that the attribute “dataIndex” be unique for each column and here are the reasons.

Click on any of the cells activates the event “cellclick“. On this occasion are forwarded to multiple parameters, and for us the most interesting parameter “cellIndex” because it defines the number of columns on which the user clicked the mouse. The numbers start from 0. This parameter should be sufficient to determine the cell, but it’s only half true. For example, your cells in the grid object has an index 4, but if some of the previous column is not visible, the index decreases. So, the “cellIndex” provides the indexr of visible columns in the Grid. For this reason it is essential that the value of the dataIndex attribute be unique. Lines 16-21 show how to get the dataIndex value from cellIndex. In this way, regardless of the order the cell you clicked, you will always have the correct value. It remains to check whether it is a cell that suits you. In example below, it is used switch() statement.

columns : [
    ...
    {
        dataIndex:'comment',
        cls:'colComments',
        header:'User comments',
        tooltip:'User comments',
        width:24,
        menuDisabled:true,
    }
    ...
]
listeners : {
    cellclick : function(ths, td, cellIndex, r, tr, rowIndex, e, eO)
    {
        var dataIdx = ths.up('grid')
                        .getView()
                        .panel
                        .headerCt
                        .getHeaderAtIndex(cellIndex)
                        .dataIndex;
        switch (dataIdx) {
            case 'comment' : 
                show_comments_for_user(r, 'comment_view'); 
                break;
            default :
                show_user(r, 'view');
                break;
        }
    }
}
Categories: Web Tags:

Encode String to Only Alphanumeric Chars

November 27th, 2014 No comments

If you need to encode a string with all sorts of problem characters into a URL-safe string, urlencode() is not right solution.

What you need is a function that will give you only numbers and letters. No %’s or &’s, spaces, or any other non-alphanumeric character.

For this we will use PHP’s bin2hex() function, and its pack() function.

For example, it can convert: “(!X>4Ob=h/&hN\'” into this much nicer string: “2821583e344f623d682f26684e5c27”.

Here are the functions you need:

function hex_encode($string)
{
    return bin2hex($string);
}

function hex_decode($string)
{
    return pack("H*", $string);
}
Categories: PHP Tags:

Adding QTips to GridPanel cell in ExtJS 4.0

November 8th, 2014 No comments

Need to add a custom tooltip (quick tip) to cell in a GridPanel in ExtJS 4.0? In your column definitions, simply add a attribut “rendered”, passing a custom function.

...
columns [
    ...
    {
      text:"Bookmark", 
      dataIndex:"title", 
      flex:1, 
      renderer:function(value,metaData) 
      {
        metaData.tdAttr = 'data-qtip="' + value + '"';
        return value;
      }
    },
    ...
}
...

A couple notes:

  1. To show tooltip, you need to add line “Ext.QuickTips.init();” before Grid definition
  2. data-qtip is one of four attributes for tooltip. Tooltip attributes are:
    • data-qtip – Set tooltip text content
    • data-qtitle – Set the title
    • data-qwidth – Set tooltip width
    • data-qalign – Represented by a reference point that corresponds to the original, which reference point. For example: “tl-br” is represented by the tip of the upper left corner, corresponding to the lower right corner of the original

With such a modification, you get a nice tooltip generated for each cell in your grid.

Categories: JavaScript Tags: , ,

Extjs4 tree (un)check all children

October 20th, 2014 No comments

If you use Extjs treePanel with checkbox to select elements, here’s the solution how to check/decheck all tree elements.

Example for check:

Ext.getCmp('tree-list').getRootNode().cascade(function(n) 
{
    n.set("checked",true); 
});

Example for uncheck:

Ext.getCmp('tree-list').getRootNode().cascade(function(n) 
{
    n.set("checked",false); 
});
Categories: JavaScript Tags: ,

Extjs change grid cell background based on value

October 20th, 2014 No comments

If you want to change the color of a Grid cell Grid, use the renderer attribute.
Example: Suppose you have a cell that has a numeric value. If this value is greater than 0, then the background color is green, otherwise it is red.
Here’s a code that describes:

renderer : function(v, md) 
{
    if(parseInt(v) > 0)
        md.style = "background-color:green;";
    else
        md.style = "background-color:red;";
}

Extjs 4 only numbers textfield

February 17th, 2014 No comments

ExtJS 4 contains the field type “numberfield” to enter numbers. If the numbers have a lot of digits, this field can create problems. On this occasion, it can help a text field (“textfield”) with a filter for input only numbers.

Within the setting properties of the text field, enter the attribute “maskRe”. It allows you to create masks with regular expressions. Here is a mask that allows you to enter only the numbers:

    maskRe:/[0-9.]/
Categories: JavaScript Tags: ,

String replace in MySQL

December 4th, 2013 No comments

Change a part of text in a MySQL database, can execute with the command REPLACE().

Example: change HTML bold text in the ‘description’ field that uses B tag with STRONG tag.

UPDATE texts SET description = REPLACE(description, ‘B>’, ‘STRONG>’);

.htaccess redirect domain.com to www.domain.com

October 28th, 2013 No comments

In the. htaccess file place the following two lines:

rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

Instead “domain.com” set your domain

Categories: Web Tags: ,

CodeIgniter: Your recaptcha.php file does not appear to contain a valid configuration array

October 28th, 2013 No comments

For those who have run into this problem as I do, check the settings for PHP marker (<? and <?=).
If not activated, you will see this error because they are placed in reCAPTCHA scripts.

Modify “<?” into “<?php” and “<?=” into “<?php echo” and everything will be fine.
The other option is to allow shorter markers.

Categories: PHP Tags: ,