Hiển thị các bài đăng có nhãn web. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn web. Hiển thị tất cả bài đăng

How to create a data table in angular using ngTable

I'm trying to evaluate front end frameworks since it's becoming more and more popular lately (I'm more of a backend developer using primefaces). And so I'm looking at angular2 and react, but in this tutorial we will be trying to develop a data table from angular2.

What surprises me is, though there are a lot of ui frameworks, available production ready made components are lacking compared to primefaces.

Let the code talk for itself. Note though that I didn't use typescript yet since ngTable doesn't have a documentation yet on its website.

Also notice that the original web service that I use supported paging and sorting, that's why I have some extra parameters in the request: Page, RecsOnPage and SortBy. I did not bother to provider client side paging because it's already in the docs and is easy to understand.

<html>

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js"></script>
<script src="https://unpkg.com/angular@1.5.5/angular.js"></script>
<script src="https://unpkg.com/ng-table@3.0.1/bundles/ng-table.min.js"></script>

<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"></link>
<link href="https://unpkg.com/ng-table@3.0.1/bundles/ng-table.min.css" rel="stylesheet"></link>

<script>
var app = angular.module('ngTableTest', ['ngTable']);

app.controller("mainController", function($scope, NgTableParams, mainControllerService) {
console.debug('mainController.init');

$scope.tableParams = new NgTableParams({
page: 1, // show first page
count: 10 // count per page
}, {
total: 0, // length of data
getData: function(params) {
var sortNameValue = "";
for (var keyName in params.sorting()) {
sortNameValue = keyName;
sortNameValue = sortNameValue + "_" + params.sorting()[keyName];
}
return mainControllerService.getData(params.page(), params.count(), sortNameValue).then(function(data) {
console.debug("data received length=" + data.data.RestResponse.result.length);
params.total(data.data.RestResponse.result.length); // set total for recalc pagination
return data.data.RestResponse.result;
});
}
});

console.log('mainController.end');
});

app.service("mainControllerService", function($http) {
console.debug('mainControllerService.init');

var service = {
cachedData: [],
getData: function(page, count, sorting) {
console.debug("fetching page=" + page + ", count=" + count + ", sorting=" + JSON.stringify(sorting));
var params = {
Page: page,
RecsPerPage: count,
SortBy: sorting
};
var config = {
params: params
}
var promise = $http.get("http://services.groupkt.com/country/get/all", config)
.success(function(response) {
console.debug('downloaded ' + response.RestResponse.result.length + ' records');
angular.copy(response.RestResponse.result, service.cachedData);
});
return promise.then(function(result) {
return result;
});
}
}

return service;
});
</script>
</head>

<body>
<div ng-app="ngTableTest" ng-controller="mainController">
<div loading-container="tableParams.settings().$loading">
<table class="table table-condensed table-bordered table-striped" ng-table="tableParams" showfilter="false">
<tbody>
<tr ng-repeat="x in $data">
<td data-title="'Name'" sortable="'name'">{{ x.name }}</td>
<td data-title="'Alpha2 Code'" sortable="'alpha2_code'">{{ x.alpha2_code }}</td>
<td data-title="'Alpha3 Code'" sortable="'alpha3_code'">{{ x.alpha3_code }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>

</html>

Code is also available here for easier download: https://github.com/czetsuya/AngularNgTable

Maybe next article is about using typescript :-)

References:
http://ng-table.com/

How to migrate your Godaddy web hosting to DigitalOcean

Lately I've been reading some good articles about DigitalOcean as a cheap VPS option in online hosting and so I decided to try and subscribe to one.

Since my wordpress website hosting is already expiring I decided to moved the hosting to DigitalOcean and renew the domain name in Godaddy.

Here's how I point my Godaddy domain to DigitalOcean VPS hosting and migrate my wordpress website:

*You must already have a DigitalOcean account with linked payment method (can be either a credit card or paypal).
*Copy all your wordpress files from Godaddy.
*Make a backup of your wordpress database.


  1. Create a droplet in digital ocean.
    1. Distribution = 15.04 x64
    2. Applications = Wordpress 14.04
    3. I chose $5 / month
  2. Follow this article to configure your server: https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-on-ubuntu-14-04.
  3. Migrating your wordpress website:
    1. Using winscp, upload your files in your server's /var/www folder.
    2. Make sure that www-data user can write to the folders where it needed to, for example uploads. Note: use chmod. Note by default the uploaded files are owned by root, use chown and change the ownership to www-data.
      1. >chown www-data:www-data /var/www -R, change the owner and group of /var/www folder from root to www-data. This is done recursively.
      2. >chmod 774, means owner and group (read, write, execute) while others (read-only).
    3. Following the tutorial in #2, you should now have phpmyadmin installed. Restore your wordpress backup.
  4. We then need to point your domain name to the new ip address in digital ocean.
    1. To get your droplet's ip address, go to your digital ocean's droplet's page 
  5. Now that you have your ip address, go to DNS, and add a domain record like this: 
  6. We are done with digital ocean and we will now point godaddy's domain to digital ocean's server.
  7. In Godaddy expand Domains tab.
  8. Chose your domain then click Manage.
  9. In Settings tab, under Nameservers click Manage.
  10. Setup Type=Custom, then add the 3 nameservers below
    1. NS1.DIGITALOCEAN.COM
    2. NS2.DIGITALOCEAN.COM
    3. NS3.DIGITALOCEAN.COM 
  11. After adding hit SAVE.
  12. You should now see the 3 name servers in the next screen.
Wait for 5minutes, then try logging-in in your linux hosting, and in the terminal run:
>whois makeupchum.com

The command above should give you your digital ocean's ip address and name servers.

And that's it! Your wordpress website must now be running in digital ocean. That's in less than 30mins :-)

Common problems I've encountered:

  1. 2 widget were broken, need to set the content again
  2. some folders are not writeable when I uploaded need to execute chmod.
Want to try DigitalOcean? Register with this link.

Setup wordpress in a sub-domain in Godaddy's windows hosting

This configuration is for windows only.

Configuration:
For example you have domain anime.com that is powered by wordpress, you set up permalinks to use postname so post will be accessible at http://anime.com/postname.

The problem is when you create a sub-domain, for example shounen.anime.com and set permalinks like that of the main domain so that you can do SEO. Obviously this will not work as the main domain permalinks will interfere, so you have to set a redirect rule.

Taken into account that it's a windows hosting, therefore we need to configure Web.config rather than .htaccess.

<?xml version="1.0" encoding="UTF-8"?>

<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="wordpress" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>