2022/07/12

multiple checkbox filter for datatables

ๆœ€่ฟ‘ๅœจ็ทด็ฟ’ datatables ้€™ๅ€‹ๅฅฝ็”จ็š„ๅทฅๅ…ท,ไป–ๅฏไปฅๆญ้… jQuery ่ทŸ Bootstrap ่ฎ“ไฝ ๅฏไปฅๅพˆๅฟซ็š„ๅปบ็ซ‹ๆœ‰ RWD ็š„่กจๆ ผ,ไป–ๆœ‰้žๅธธ่ฑๅฏŒ็š„ API ๅฏไปฅๅฎข่ฃฝๅ…งๅฎน,ไปŠๅคฉ็คบ็ฏ„ไธ€ไธ‹ๆ€Ž้บผไฝฟ็”จ checkbox ๅŽป filter ๅ‡บ็ฌฆๅˆ็š„ๅ…งๅฎน。

ไฝฟ็”จๆญฃ่ฆๅผ

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet"
          href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.21/css/jquery.dataTables.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.21/js/jquery.dataTables.min.js"></script>
    <script>
        $(function() {
            let data = [
                [
                    "Chan",
                    "basketball"
                ],
                [
                    "Phoebe",
                    "volleyball"
                ],
                [
                    "Lucas",
                    "tennis"
                ],
                [
                    "LBJ",
                    "basketball"
                ],
            ];
            let table = $('#data_table').DataTable({
                data: data
            });

            $(':checkbox').on('click', function() {
                let options = $(':checkbox:checked').map(function() {
                    return $(this).val()
                }).get();

                if (options.length === 0) {
                    table.columns(1).search('').draw();
                } else {
                    let regex = options.join('|');

                    table.columns(1).search(regex, true, false).draw();
                }
            });
        });
    </script>
</head>
<body>
<div>
    <label> basketball
        <input type="checkbox" value="basketball">
    </label>
    <label> volleyball
        <input type="checkbox" value="volleyball">
    </label>
    <label> tennis
        <input type="checkbox" value="tennis">
    </label>
</div>
<table id="data_table">
    <thead>
    <tr>
        <th>name</th>
        <th>hobby</th>
    </tr>
    </thead>
    <tbody></tbody>
</table>
</body>
</html>

ไฝฟ็”จ extension

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet"
          href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.21/css/jquery.dataTables.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.21/js/jquery.dataTables.min.js"></script>
    <script>
        $(function() {
            let data = [
                [
                    "Chan",
                    "basketball"
                ],
                [
                    "Phoebe",
                    "volleyball"
                ],
                [
                    "Lucas",
                    "tennis"
                ],
                [
                    "LBJ",
                    "basketball"
                ],
            ];
            let table = $('#data_table').DataTable({
                data: data
            });

            $.fn.dataTable.ext.search.push(function(settings, data) {
                let options = $(':checkbox:checked').map(function() {
                    return $(this).val()
                }).get();

                if (options.length === 0) {
                    return true;
                }

                return options.indexOf(data[1]) !== -1;
            });

            $(':checkbox').on('click', function() {
                table.draw();
            });
        });
    </script>
</head>
<body>
<div>
    <label> basketball
        <input type="checkbox" value="basketball">
    </label>
    <label> volleyball
        <input type="checkbox" value="volleyball">
    </label>
    <label> tennis
        <input type="checkbox" value="tennis">
    </label>
</div>
<table id="data_table">
    <thead>
    <tr>
        <th>name</th>
        <th>hobby</th>
    </tr>
    </thead>
    <tbody></tbody>
</table>
</body>
</html>

ไธŠ้ขไฝฟ็”จไบค้›†็š„ๆ–นๆณ•็ฏฉ้ธ,็จๅพฎๆ”นๅฏซไธ€ไธ‹ๅฐฑๅฏไปฅ่ฎŠ่ฏ้›†ไบ†。

2022/07/06

learning latteart opening

่‡ชๅญธๆ‹‰่ŠฑไนŸๅฅฝๅนพๅนดไบ†,่ฒท้Žๅพˆๅคš่ทŸๆ‹‰่Šฑ็›ธ้—œ็š„ๅ™จๆ,ไฝ†็ฉฉๅฎšๆ€งไธ€็›ดไธๆ˜ฏๅพˆๅฅฝ。

https://www.instagram.com/p/Ce-TmSGh8np/

้€™ๆฌก็ต‚ๆ–ผ็—›ไธ‹ๆฑบๅฟƒๅŽปๆ‰พ่€ๅธซไธŠ่ชฒ,survery ไบ†ๅŠ้–“ๅพˆไน…,ๆฒ’ๆƒณๅˆฐ่‡ชๅทฑ็š„ๆ„›ๅบ— Shawn Coffee ๅฐฑๆœ‰้–‹่ชฒ,้€้Ž IG ่ทŸ่€ๅธซ่ฏ็นซไน‹ๅพŒ,็ด„ๅฎšๅฅฝไบ†ไธŠ่ชฒๆ—ฅๆœŸ(ไธๅคชๅฅฝๆŽ’,ๅคช็ด…ไบ†),ไปŠๅคฉๆ˜ฏๆˆ‘็š„็ฌฌไธ€ๅคฉไธŠ่ชฒ,ๆˆ‘้‚„่ ป็ทŠๅผต็š„,้ƒฝๅ››ๅๅนพๆญฒไบ†้‚„่ƒฝๅค ้‡ๅ›žๅญธ็”Ÿๆ™‚ไปฃ็š„ๅญธ็ฟ’ๅˆๅฟƒ่ทŸ้‡ๆบซๆœช็Ÿฅ็š„ๅฟๅฟ‘ๆ„Ÿๆˆ‘่ฆบๅพ—ๅพˆๆฃ’,Wish myself luck。

Compile Apache and OpenSSL

OS: CentOS 8
Target:

  • PHP 7.4
  • PHP-FPM
  • Apache 2.4.46
  • OpenSSL 1.1.1g

Start compile

sudo dnf install install -y gcc gcc-c++ make prec-devel libtool perl-core zlib-devel

# Compile apr
sudo ./configure --prefix=/usr/local/apr
sudo make
sudo make install

# Compile apr-util
sudo ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
sudo make
sudo make install

# Compile openssl
sudo ./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl shared zlib
sudo make
sudo make install

# Compile apache
sudo ./configure --prefix=/usr/local/apache2446 --enable-so --enable-ssl --enable-cgi --enable-rewrite --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork --with-zlib --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-ssl=/usr/local/openssl
sudo make
sudo make install

# Apache system configuration
# Add Apache executables to PATH ( You can use nano instead of vi)
# create and open the following file
sudo vim /etc/profile.d/httpd.sh
  
# paste the following content, save and exit
pathmunge /usr/local/apache2446/bin

# Add Systemd entry

# create and open the following file
sudo vim /etc/systemd/system/httpd.service

paste the following content, save and exit.

[Unit]
Description=The Apache HTTP Server
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/apache2446/bin/apachectl -k start
ExecReload=/usr/local/apache2446/bin/apachectl -k graceful
ExecStop=/usr/local/apache2446/bin/apachectl -k graceful-stop
PIDFile=/usr/local/apache2446/logs/httpd.pid
PrivateTmp=true

[Install]
WantedBy=multi-user.target
# reload the systemctl daemon
sudo systemctl daemon-reload

# start Apache httpd server (ignore the warnings at this stage)
sudo systemctl start httpd
sudo systemctl enable httpd

# Install PHP
sudo dnf install -y http://rpms.remirepo.net/enterprise/remi-release-8.rpm
dnf module reset php
dnf module install -y php:remi-7.4

sudo service php-fpm start
sudo systemctl enable php-fpm

Reference

2022/07/04

JsRender Sample

็พๅœจๅ‰็ซฏ framework ็™พๅฎถ็ˆญ้ณด,ไธ้Žๅ’ŒๅพŒ็ซฏ framework
้‡ๅˆฐไธ€ๆจฃ็š„ๅ•้กŒ,็‰ˆๆœฌๆ›ดๅ‹•่ถ…ๅฟซๅฏซๆณ•ไธ็›ธๅฎน,ไปŠๆ—ฅ็š„็•ถ็ด…็‚ธๅญๆฉŸ็›ธ้š”ไธ€ๅนดๅพŒๆถˆๅคฑ็š„็„กๅฝฑ็„ก่นคๆ™‚ๆœ‰ๆ‰€่ž,ๅพŒ็ซฏๆฏ”่ผƒๆฒ’ๆœ‰่ฟ…้€Ÿ้š•่ฝ็š„ๅ•้กŒ,็•ข็ซŸไป–็š„้–‹็™ผ้–€ๆชป้‚„ๆ˜ฏ็จ้ซ˜ไธ€ไบ›,ไนŸๅ› ๆญค้™คไบ†ๆœ‰็Žฉไธ€ไธ‹ vuejs ไปฅๅค–ๅฐ
front-end framework ็œŸ็š„ๆฒ’ๆœ‰ๅคช้ซ˜็š„่ˆˆ่ถฃ,ๅณไพฟๆˆ‘่ถ…ๆ„›ๅฏซ JavaScript,ไธ้Žๅฐˆๆกˆ่ฃก้ข้‚„ๆปฟๅธธ็”จๅˆฐ js render template ้€™ไปถไบ‹ๆƒ…,ๆ‰€ไปฅๆˆ‘้‚„ๆ˜ฏๆœ‰ไฝฟ็”จไธ€ไบ› plugin
ไพ†ๅฏฆ็พ้€™ไปถไบ‹ๆƒ…,ไปŠๅคฉๅฐฑไพ†ไป‹็ดน JsRender ้€™ๅ€‹ plugin

ๅŸบ็คŽๅŠŸ่ƒฝ

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JsRender</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jsrender/0.9.88/jsrender.min.js"></script>
    <script>
        $(function() {
            var data = {name: 'chan'};
            var html = $.templates('<strong>{{:name}}</strong>').render(data);
            $('#app').append(html);
        });
    </script>
</head>
<body>
<div id="app"></div>
</body>
</html>

JsRender ๅ‚ณ้€ฒๅŽป็š„่ณ‡ๆ–™็‚บ JSON,ไธŠ้ข็š„ๅ…งๅฎนๆœƒๅพ—ๅˆฐ:

<div id="app"><strong>chan</strong></div>

ๅฆ‚ๆžœๆ˜ฏ่ค‡ๆ•ธ็š„ๅ…งๅฎน,JsRender ๆœƒ่‡ชๅ‹•ๅนซไฝ ๅš repeat。

script
$(function() {
    let data = [
        {name: 'chan'},
        {name: 'phoebe'}
    ];
    let html = $.templates('<strong>{{:name}}</strong>').render(data);
    $('#app').append(html);
});
output

<div id="app"><strong>chan</strong><strong>phoebe</strong></div>

ๆจฃๆฟ

ๅฆ‚ๆžœๅฐ‡ๆจฃๆฟๅฏซๅœจ script ่ฃก้ข,ๅพ€ๅพ€ๆœƒ่ฎŠๅพ—ๅพˆ้›ฃ็ถญ่ญท,ๅญ—ไธฒไนŸ่ถ…้•ท,ๆ‰€ไปฅ JsRender ๆœ‰ไธ€ๅ€‹ๆŠŠๆจฃๆฟ้š”้›ขๅ‡บไพ†็š„ๅฅ—็”จๆ–นๅผ。

script
$(function() {
    let data = [
        {name: 'chan'},
        {name: 'phoebe'}
    ];
    let html = $.templates('#info').render(data);

    $('#app').append(html);
});
html
<script id="info" type="text/html">
    <strong>{{:name}}</strong>
</script>
output
<div id="app">
    <strong>chan</strong>
    <strong>phoebe</strong>
</div>

ๅ…งๅปบๅ‡ฝๅผ

{{> …}} (HTML-encode)

ๅฐ‡่ผธๅ‡บ็š„้ƒจไปฝๅŠ ไธŠ > ็š„่ฉฑๆœƒๅฐ‡็จ‹ๅผ็ขผ encode。

script
$(function() {
    let data = {link: '<a href="http://www.google.com">google</a>'};
    let html = $.templates('#info').render(data);

    $('#app').append(html);
});
html
<script id="info" type="text/html">
    {{>link}}
</script>
output
&lt;a href&#61;&#34;http://www.google.com&#34;&gt;google&lt;/a&gt;

{{if …}}

ๅˆคๆ–ทๅผ。

script
$(function() {
    let data = [
        {name: 'chan', age: 37},
        {name: 'phoebe', age: 38},
        {name: 'lucas', age: 2}
    ];
    let html = $.templates('#info').render(data);

    $('#app').append(html);
});
html
<script id="info" type="text/html">
    {{:name}} is {{if age > 10}}adult{{else}}children{{/if}}<br>
</script>
output
<div id="app">
    chan is adult<br>
    phoebe is adult<br>
    lucas is children<br>
</div>

{{for …}}

่ฟดๅœˆ。

script
$(function() {
    let data = [{
        name: 'chan',
        likes: [{
            name: 'music'
        }, {
            name: 'movie'
        }]
    }, {
        name: 'phoebe'
    }, {
        name: 'lucas',
        likes: [{
            name: 'cartoon'
        }]
    }];
    let html = $.templates('#info').render(data);

    $('#app').append(html);
});
html
<script id="info" type="text/html">
    <div>
        {{:#index}}: {{:name}}
        {{if likes}}
        <ul>
            {{for likes}}
            <li>{{:#index + 1}}: {{:name}}</li>
            {{/for}}
        </ul>
        {{else}}
        <div>no likes</div>
        {{/if}}
    </div>
</script>
output
<div id="app">
    <div>
        0: chan
        <ul>
            <li>1: music</li>
            <li>2: movie</li>
        </ul>
    </div>
    <div>
        1: phoebe
        <div>no likes</div>
    </div>
    <div>
        2: lucas
        <ul>
            <li>1: cartoon</li>
        </ul>
    </div>
</div>

{{include …}}

ๅผ•ๅ…ฅๅ…ถไป–็š„ๆจฃๆฟ。

script
$(function() {
    let data = {
        name: 'test',
        data: [
            {
                name: 'chan',
                age: 42
            },
            {
                name: 'phoebe',
                age: 42
            }
        ]
    };

    console.log($('#main').render(data));
});

html
<script id="main" type="text/html">
    <h3>{{:name}}</h3>
    {{include tmpl="#sub"/}}
</script>
<script id="sub" type="text/html">
    <ol>
        {{for data}}
        <li>{{:name}}: {{:age}}</li>
        {{/for}}
    </ol>
</script>

output
<h3>test</h3>
<ol>
    <li>chan: 42</li>
    <li>phoebe: 42</li>
</ol>

{{props …}}

key value ๅผ็š„่ฟดๅœˆ。

script
$(function() {
    var data = [
        {
            name: 'Chan',
            likes: {
                name: 'a1',
                age: 33
            }
        },
        {
            name: 'Phoebe',
            likes: {
                food: 'steak',
                drink: 'juice'
            }
        }
    ]
    var html = $.templates('#info').render(data);

    $('#app').html(html);
});
html
<script id="info" type="text/html">
    <div>
        {{:name}}
        <ul>
            {{props likes}}
            <li>{{:key}}: {{:prop}}</li>
            {{/props}}
        </ul>
    </div>
</script>
output
<div id="app">
    <div>
        Chan
        <ul>
            <li>name: a1</li>
            <li>age: 33</li>
        </ul>
    </div>
    <div>
        Phoebe
        <ul>
            <li>food: steak</li>
            <li>drink: juice</li>
        </ul>
    </div>
</div>

helper

helper ไนŸๆ˜ฏไธ€็จฎ JSON ๆ ผๅผ,ๅฏไปฅๆ˜ฏๆ–‡ๅญ—ๆˆ–่€…ๆ˜ฏ function。

script
$(function() {
    var helpers = {
        prefix: function(val) {
            return val + ' is cool';
        },
        version: '1.1'
    };
    var data = {
        name: 'chan'
    };
    var html = $.templates('#info').render(data, helpers);

    $('#app').append(html);
});
html

<script id="info" type="text/html">
    version: {{:~version}} name: {{:~prefix(name)}}
</script>
output
<div id="app">
    version: 1.1 name: chan is cool
</div>

helper ้‚„ๆœ‰ๅฆไธ€็จฎ global ็š„ๅ‚ณๅ…ฅๆ–นๅผ。

script
$(function() {
    let helpers = {
        prefix: function(val) {
            return val + ' is cool';
        },
        version: '1.1'
    };
    $.views.helpers(helpers);
    let data = {
        name: 'chan'
    };
    let html = $.templates('#info').render(data);

    $('#app').append(html);
});

ไปฅไธŠๆ˜ฏๆœ€ๅŸบๆœฌ็š„็ฏ„ไพ‹,ไธ้Žๆ‡‰่ฉฒๅฏไปฅๆ‡‰ไป˜ๅคง้ƒจๅˆ†็š„ๆƒ…ๆณไบ†,่‹ฅ่ฆไฝฟ็”จๅ…ถไป–ๆ›ด่ฉณ็›ก็š„ๅŠŸ่ƒฝๅฏไปฅๅ‰ๅพ€ไป–็š„ๅฎ˜็ถฒ https://www.jsviews.com/

2022/06/10

Shell Script With Optional Arguments

ๆˆ‘ๅ€‘ๅœจๆ“ไฝœ Linux ไธŠ็š„ script ๆ™‚,ๅธธๅธธๆœƒไฝฟ็”จไธ€ไบ› optional arguments,ไพ‹ๅฆ‚:

ls -l
grep -iR --include *.php

ๆˆ‘ๅ€‘่‡ชๅทฑ่ฃฝไฝœ่…ณๆœฌๆ™‚ๅฏไปฅไฝฟ็”จ getopts ไปฅๅŠ shift ้”ๅˆฐ้€™ๆจฃ็š„ๆ•ˆๆžœ,ไปฅไธ€ๅ€‹ๅฎ‰่ฃ็ณป็ตฑ็š„่…ณๆœฌ่ˆ‰ไพ‹。

install.sh

#/bin/bash

install_nginx=0
install_mysql=0
nginx_port=80
mysql_port=3306

while getopts "nmP:p:" OPTION; do
  case "$OPTION" in
  n)
    install_nginx=1
    ;;
  m)
    install_mysql=1
    ;;
  p)
    nginx_port="$OPTARG"
    ;;
  P)
    mysql_port="$OPTARG"
    ;;
  esac
done

echo "Install basic"

if [ "$install_nginx" == 1 ]; then
  echo "Install nginx with port $nginx_port"
fi

if [ "$install_mysql" == 1 ]; then
  echo "Install mysql with port $mysql_port"
fi
bash install.sh

# Install basic

bash install.sh -n

# Install basic
# Install nginx with port 80

bash install.sh -n -p 8888 -m
# Install basic
# Install nginx with port 8888
# Install mysql with port 3306

ไธŠ้ข็š„ๆ–นๆณ•ๅช่ƒฝๅธถๅ…ฅๅ–ฎไธ€ๅญ—็ฌฆ,ๆ‰€ไปฅๅƒๆ˜ฏ port ้€™็จฎๅธธ่ฆ‹ๅ…ฑ็”จ็š„ๆ–‡ๅญ—ๅช่ƒฝ็”จๅคงๅฐๅฏซๆˆ–่€…ไธๅŒๅญ—็ฌฆๅ–ไปฃ,ๅฝˆๆ€งๆฏ”่ผƒๅฐ‘,ไฝฟ็”จ shift ๅฏไปฅ่งฃๆฑบ้€™ๅ€‹ๅ•้กŒ。

#/bin/bash

install_nginx=0
install_mysql=0
nginx_port=80
mysql_port=3306

while [ "$1" != "" ]; do
  case "$1" in
  --nginx)
    install_nginx=1
    ;;
  --nginx-port)
    shift
    nginx_port="$1"
    ;;
  --mysql)
    install_mysql=1
    ;;
  --mysql-port)
    shift
    mysql_port="$1"
    ;;
  esac
  shift
done

echo "Install basic"

if [ "$install_nginx" == 1 ]; then
  echo "Install nginx with port $nginx_port"
fi

if [ "$install_mysql" == 1 ]; then
  echo "Install mysql with port $mysql_port"
fi
bash install.sh --nginx
# Install basic
# Install nginx with port 80

bash install.sh --nginx --nginx-port 8888
# Install basic
# Install nginx with port 8888

bash install.sh --nginx --mysql --mysql-port 3307
# Install basic
# Install nginx with port 80
# Install mysql with port 3307

reference

2022/04/07

Makefile

ๅฆ‚ๆžœๆˆ‘ๅ€‘็š„ๅฐˆๆกˆๆœ‰ๆŒ‡ไปคๆ˜ฏๆœƒ้‡่ค‡ๅŸท่กŒ็š„,้€้Ž Makefile ๅฏไปฅ้›†ๆˆๅ…งๅฎน,ๆœ€ๆ–นไพฟ็š„ๆ˜ฏๅคš็ต„ๅˆไพๅบๅŸท่กŒ。

ๅปบ็ซ‹ไธ€ๅ€‹ๆช”ๆกˆๅ็จฑ็‚บ Makefile:

ๅŸบๆœฌๆ‡‰็”จ

init:
        echo first command
        echo second command
$ make init
echo first command
first command
echo second command
second command

ๅฎ‰้œ่ผธๅ‡บ

ๅฆ‚ๆžœไฝ ไธๅธŒๆœ› terminal ๅฐๅ‡บไฝ ไธ‹็š„ๅ‘ฝไปคๅ…งๅฎน,ๆœ‰ๅนพ็จฎๅฏซๆณ•ๅฏไปฅ้ฟๅ…。

.SILENT: init

init:
        echo first command
        echo second command
.SILENT: #ๅฝฑ้Ÿฟๅ…จ้ƒจ

init:
        echo first command
        echo second command
init:
        @echo first command
        @echo second command

ๅคš้‡ๆญฅ้ฉŸ

first_check:
        echo first check

second_check:
        echo second check

init: first_check second_check
        echo init
$ make init
echo first check
first check
echo second check
second check
echo init
init

่ฎŠๆ•ธ

file=default

init:
        @echo file is $(file)
$ make
file is default

$ make file=new
file is new

่ฎŠๆ•ธๅฏไปฅ็ถ“็”ฑๅธถๅ…ฅๆ–นๅผ่ฆ†่“‹。