PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 16, 2022

[FIXED] How to implement table row drag and drop for sorting in Yii?

 February 16, 2022     html, javascript, jquery, php, yii     No comments   

Issue

I am new to JavaScript and jQuery. I am trying to implement the fiddle functionality linked here for sorting table rows by drag and drop.

I used to copy - paste the entire code to make it work in NetBeans, but it is not working. Only displaying the table. Not able to drag and drop. Do I need to add any jQuery file. I am using Yii project. And I copied the html and JavaScript code in my PHP file. Are there any other requirements?

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

Solution

You most likely need to register jquery and jquery.ui in your view:

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


Answered By - deacs
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing