如何在Yii中实现表行拖放以进行排序?

时间:2013-07-17 17:51:57

标签: php javascript jquery html yii

我是JavaScript和jQuery的新手。我正在尝试实现小提琴功能linked here,以便通过拖放对表行进行排序。

我曾经复制 - 粘贴整个代码以使其在NetBeans中运行,但它无法正常工作。仅显示表格。无法拖放。我是否需要添加任何jQuery文件。我正在使用Yii项目。我在PHP文件中复制了html和JavaScript代码。还有其他要求吗?

        <h1>Sorting A Table With jQuery UI</h1>
    <a href='http://www.foliotek.com/devblog/make-table-rows-sortable-using-jquery-ui-sortable/'>Make table rows sortable with jQuery UI</a>

    <table id="sort" class="grid" title="Kurt Vonnegut novels">
        <thead>
            <tr><th class="index">No.</th><th>Year</th><th>Title</th><th>Grade</th></tr>
        </thead>
        <tbody>
            <tr><td class="index">1</td><td>1969</td><td>Slaughterhouse-Five</td><td>A+</td></tr>
            <tr><td class="index">2</td><td>1952</td><td>Player Piano</td><td>B</td></tr>
            <tr><td class="index">3</td><td>1963</td><td>Cat's Cradle</td><td>A+</td></tr>
            <tr><td class="index">4</td><td>1973</td><td>Breakfast of Champions</td><td>C</td></tr>
            <tr><td class="index">5</td><td>1965</td><td>God Bless You, Mr. Rosewater</td><td>A</td></tr>
        </tbody>
    </table>

<script>

        var fixHelperModified = function(e, tr) {
        var $originals = tr.children();
        var $helper = tr.clone();
        $helper.children().each(function(index) {
            $(this).width($originals.eq(index).width())
        });
        return $helper;
    },
        updateIndex = function(e, ui) {
            $('td.index', ui.item.parent()).each(function (i) {
                $(this).html(i + 1);
            });
        };

    $("#sort tbody").sortable({
        helper: fixHelperModified,
        stop: updateIndex
    }).disableSelection();
</script>

1 个答案:

答案 0 :(得分:1)

您很可能需要在视图中注册jquery和jquery.ui:

<?php 
Yii::app()->clientScript->registerCoreScript('jquery'); 
Yii::app()->clientScript->registerCoreScript('jquery.ui');
?>