[Joomla 2.5] ตัวอย่างโค้ด Ajax ส่งค่าเป็นรอบ


แชร์ ความรู้ในการ พัฒนา Joomla Component Extension Module Plugin

Moderators: mindphp, ผู้ดูแลกระดาน

User avatar
tsukasaz
PHP VIP Members
PHP VIP Members
Posts: 10602
Joined: 18/04/2012 9:39 am

[Joomla 2.5] ตัวอย่างโค้ด Ajax ส่งค่าเป็นรอบ

Post by tsukasaz » 19/03/2013 5:48 pm

ตัวอย่างโค้ดส่งค่า

Code: Select all

<script>
    jQuery(document).ready(function() {
        jQuery('#start-index').click(function() {
            if(confirm('Are you sure Train SVM ?')) {
                jQuery(this).hide('slow');
                jQuery('#indexer-working').show('slow');
                jQuery('#indexer-working > #pb-box').append('<div id="pb-perc" style="width: 100px;"></div>');
                jQuery.ajax({
                    url: 'index.php?option=com_msearch&c=botsvm&task=train&step=1',
                    success: function(data) {
                        if(data.length == 2) {
                            jQuery('#indexer-working > #pb-box > #pb-perc').css('width', '200px');
                            jQuery('#indexer-working > #msg').html('Check successfully');
                            jQuery('#indexer-content').show('slow');
                            jQuery('#indexer-content > #pb-box').append('<div id="pb-perc"></div>');
                            jQuery('#indexer-content > #msg').html('0 records / ' + data[1] + ' records');
                            runTrain(0, data[1]);
                        } else {
                            jQuery('#indexer-working > #pb-box').css('display','none');
                            jQuery('#indexer-working > #msg').html('Stop Working! Train is running');
                        }
                    },
                    cache: false
                });
            }
            return false;
        });
    });
    
    function runTrain(start, loop) {
        jQuery.ajax({
            url: 'index.php?option=com_msearch&c=botsvm&task=train&step=2&start='+start,
            cache: false
        }).done(function(data) {
            if(data == 'next') {
                jQuery('#indexer-content > #pb-box > #pb-perc').css('width', '200px');
                jQuery('#indexer-content > #msg').html(loop + ' records / ' + loop + ' records<br />Completed');
                jQuery('#indexer-finish').show('slow');
                finishTrain();
            } else {
                var p = eval((data / loop) * 100);
                jQuery('#indexer-content > #pb-box > #pb-perc').css('width', p+'%');
                jQuery('#indexer-content > #msg').html(data + ' records / ' + loop + ' records');
                runTrain(data, loop);
            }
        });
    }
    
    function finishTrain() {
        jQuery.ajax({
            url: 'index.php?option=com_msearch&c=botsvm&task=train&step=3',
            cache: false
        });
    }
</script>
<div id="start-index">
    <h3>Train SVM</h3>
</div>

<div id="indexer-working" style="display: none;">
    <p class="text">Check Bot Training</p>
    <div id="pb-box"></div>
    <span id="msg">Checking...</span>
</div>
    
<div id="indexer-content" style="display: none;">
    <p class="text">SVM Bot Training...</p>
    <div id="pb-box"></div>
    <span id="msg"></span>
</div>

<div id="indexer-finish" style="display: none; height: 70px;">
    <h3>Finish Train SVM</h3>
</div>
ตัวอย่างโค้ดรับค่า

Code: Select all

function train() {
    $db = JFactory::getDbo();
    $step = JRequest::getVar('step');
    if($step == 1) {
        ...
        ...
        $loop = count($arr_content);
        $data = array('next', $loop);
    } else if($step == 2) {
        $start = JRequest::getVar('start');
        $finish = $start + BOTSVM_TRAIN_AJAX;
        $data = $finish;
        ...
        ...
    } else if($step == 3) {
        $data = 'finish';
    }
    header('Content-type: application/json');
    echo json_encode($data);
    JFactory::getApplication()->close();
} 
The last bug isn't fixed until the last user is dead. (Sidney Markowitz, 1995)

Return to “Joomla Developing Knowledge”

Users browsing this forum: No registered users and 3 guests