Archivo de la categoría: Código

Create HTML Table from JSON – Crear tabla HTML desde un JSON


You can use this function to create a HTML table from JSON data:
HTML:

CSS:

.tableStyle
{
width: 100px;
border: 1px solid black;
}
.tableStyle th
{
color: red;
}

JS:

JSON:
var json = [{id: 1, name: ‘Carlitos’, age: 30}, {id: 2, name: ‘Miguel’, age: 32}, {id: 3, name: ‘Amanda’, age: 35}];
(You can get a Json from Ajax call)

Function call:
createTableFromJson(‘container’, json, true, ‘tableStyle’);

Function:
function createTableFromJson(containerID, json, withHeader, cssName)
{
var table = $(‘<table>’);
table.addClass(cssName);
$.each(json, function(k, v) {
var o = json[k];
var row = $(‘<tr>’);
$.each(o, function(a, b) {
if (withHeader && k == 0)
{
var head = $(‘<th>’);
head.text(a);
table.append(head);
}
var cell = $(‘<td>’);
cell.text(b);
row.append(cell);
});
table.append(row);
});
$(‘#’ + containerID).append(table);
}

FULL SAMPLE

 

Anuncios

Deja un comentario

Archivado bajo Código

Hide HTML Table Column with JQuery – Ocultar columna de tabla HTML con JQuery


If you want to hide a Column in HTML Table you can use JQuery to do it.
 function hideTableColumns(idTable, columnsIndexs)
{
$(“#” + idTable + ” tr”).each(function() {
for(var i = 0; i < columnsIndexs.length; i++)
{
var colIndex = columnsIndexs[i];
$($(this).find(“td”)[colIndex]).hide();
}
});
}

FULL SAMPLE

 

 

Deja un comentario

Archivado bajo Código

Obtein SQL Master Database File Path from SQL Script – Obtener el path de la base Master desde un Script de SQL


Si necesitas obetener el path donde se aloja la base de datos master.mdf (quizás necesitas obtenerla para crear una base nueva desde un script):

If you need to obtain the path of the master.mdf database you execute this script (maybe you want to obteir for create new database from script):

SELECT 
    reverse(substring(reverse(physical_name), CHARINDEX('\',reverse(physical_name)), len(physical_name))) 
FROM 
    sys.master_files 
WHERE 
    name = 'master'

🙂

Deja un comentario

Archivado bajo Código

Diferentes formas de verificar si un string contiene otra cadena de texto en JavaScript – Different ways to check if a string contains a substring in JavaScript


You have this ways to find a string in another string (tenes las siguientes formas de encontrar una cadena de texto en otra):

-indexOf:

str.indexOf(searchValue[, fromIndex])

Parameters:

searchValue
value to search for.
fromIndex (Optional)
Index at which to start the searching forwards in the string.
Default is 0.
fromIndex <= 0 entire string is searched.
fromIndex >= str.length string is not searched and -1 is returned.
If searchValue is an empty string, str.length is returned.

Return value:
The index of the first occurrence of the specified value; -1 if not found.

                    
'Blue Whale'.indexOf('Blue');            // returns  0
'Blue Whale'.indexOf('Blute');           // returns -1
'Blue Whale'.indexOf('Whale',0);         // returns  5
'Blue Whale'.indexOf('Whale',5);         // returns  5
'Blue Whale'.indexOf('');                // returns  0
'Blue Whale'.indexOf('', 9);             // returns  9
'Blue Whale'.indexOf('', 10);            // returns 10
'Blue Whale'.indexOf('', 11);            // returns 10
                    

Browser compatibility:

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)

-includes:

str.includes(searchString[, position])

Parameters:

searchString
A string to be searched for within this string.
position (Optional)
The position within the string at which to begin searching for searchString.(defaults to 0).

Return value:
true if the given string is found anywhere within the search string; otherwise, false if not.

                    
var str = 'To be, or not to be, that is the question.';
console.log(str.includes('To be'));                 // true
console.log(str.includes('question'));              // true
console.log(str.includes('nonexistent'));           // false
console.log(str.includes('To be', 1));              // false
console.log(str.includes('TO BE'));                 // false
                    

Note you may need to load es6-shim or similar to get this working on older browsers.

require('es6-shim')

Browser compatibility:

Feature Chrome Firefox (Gecko) Internet Explorer Edge Opera Safari
Basic support 41 (40) No support Yes No support 9

-search:

str.search(regexp)

Parameters:

regexp
A regular expression object. If a non-RegExp object obj is passed, it is implicitly converted to a RegExp by using new RegExp(obj).

Return value:
The index of the first match between the regular expression and the given string; if not found, -1.

                    
var string = "foo", expr = "/oo/";
string.search(expr);
                    

Browser compatibility:

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)

-RegExp test:

regexObj.test(str)

Parameters:

str
The string against which to match the regular expression.

Return value:
true if there is a match between the regular expression and the specified string; otherwise, false.

                    
var string = "foo", expr = /oo/;    //no quotes
expr.test(string);
                    

Browser compatibility:

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)

-RegExp Match:

regexp[Symbol.match](str)

Parameters:

str
A String that is a target of the match.

Return value:
An Array containing the entire match result and any parentheses-captured matched results, or null if there were no matches.

                
                var string = "foo",
                        expr = "/oo/";
                string.match(expr);
                    

Browser compatibility:

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (49) No support (Yes) (Yes)
Samples and definitions are from:
https://developer.mozilla.org/en-US/
Thanks Mozilla!

Deja un comentario

Archivado bajo Código, Teoría

TFSConnector – TFSConnectorPlugin – TFSPackageManagerCmd


Some time ago, I needed to connect Jenkins to the VSO (Visual Studio Online Team Services TFS) because I was working on a project that used that service repository.
Because Jenkins doesn’t have a plugin or a direct connection to this service, I developed a small plugin to do it in .Net.

Here you can know the source code:

https://github.com/mcrei/TFSPackageManagerCmd

https://github.com/mcrei/TFSConnectorPlugin

 

Deja un comentario

Archivado bajo Código, Programas

C# 7 – Local functions


In C# 7, now you can declare local functions, for example:

public int Run(int code)
{
int Mode(int i)
{
if (i > 10 )
return 0;
return i + 1;
}
//code
return Mode(code);
}

Deja un comentario

Archivado bajo Código

C# 7 -New support for out variables


In C# 7 you can declare an out variable right at the point of usage:

public void Get()
{
GetData(out int x);
//code
}

Deja un comentario

Archivado bajo Código