php - 如何在Yii中实现表格行拖放排序?

标签 php javascript jquery html yii

我是 JavaScript 和 jQuery 新手。我正在尝试实现 fiddle 功能linked here用于通过拖放对表格行进行排序。

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

        <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>

最佳答案

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

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

关于php - 如何在Yii中实现表格行拖放排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17706803/

相关文章:

javascript - .params 到底是什么以及它有什么作用?

php - 有关购物车 HTML 按钮中错误的帮助

php - jquery - 查找具有特定类的最后一个元素并将其传递给 php?

javascript - 脚本不运行 React

javascript - 内联 onclick 覆盖 JQuery click()

javascript - text() 与 .parent().html() 后续更改的 JQuery 行为

php - 在 Laravel 中向嵌套查询添加 where

php - 选择 msql 中的计数公式 - 速度

javascript - updatepanel 触发后功能不起作用

javascript - 将内容保持在 Mobile Safari iOS 8 的底部栏上方