有没有办法使用gridDnD插件将行从JQGrid拖到可放置的文本字段?

ica*_*ats 5 jquery jqgrid jquery-ui-draggable jquery-droppable

我想从JQGrid拖动一行到文本输入字段,并从已删除的行添加列的文本到输入中文本的末尾.

显然这距离答案还有很长的路要走,但是在网格上拖动一行并设置了这一行(其中#inputTextField是'droppable'文本字段)会导致JavaScript错误this.p is undefined:

$("#searchResultsGrid").jqGrid('gridDnD',
    {
         connectWith:   '#inputTextField"
    }
);
Run Code Online (Sandbox Code Playgroud)

这是因为目标显然不是JQGrid并且没有this.p定义.我尝试了一些不同的东西......也许有一种方法可以让'滴滴'事件变成工作状态?非常感谢你的帮助:)

ica*_*ats 5

我想到了!!首先,使网格行可拖动(应在gridComplete网格事件处理程序中调用此函数):

function makeGridRowsDraggable() {

        var $searchResultsGrid  =   $("#searchResultsGrid"),
            $searchResultsRows =    $("#searchResultsContainer .ui-row-ltr");

        $searchResultsRows.css("cursor","move").draggable("destroy").draggable({
            revert:     "false",
            appendTo:   'body',
            cursor:     "move",
            cursorAt:   {
                            top: 10,
                            left: -5
                        },
            helper:     function(event) {

                            //get a hold of the row id
                            var rowId = $(this).attr('id');

                            //use the row id you found to get the column text; by using the getCell method as below, 
                            //the 'unformatter' on that column is called; so, if value was formatted using a
                            //formatter, this method will return the unformatted value 
                            //(as long as you defined an unformatter/using a built-in formatter)
                            var theValue = $searchResultsGrid.jqGrid('getCell', rowId, 'desiredValue');

                            //set the data on this to the value to grab when you drop into input box
                            $(this).data('colValue', theValue);

                            return $("<div class='draggedValue ui-widget-header ui-corner-all'>" + theValue+ "</div>");
                        },
            start:      function(event, ui) {
                            //fade the grid
                            $(this).parent().fadeTo('fast', 0.5);
                        },
            stop:       function(event, ui) {
                            $(this).parent().fadeTo(0, 1);
                        }
        });
    }
Run Code Online (Sandbox Code Playgroud)

然后,创建droppable元素:

function createDroppableElements() {

    $("#inputFieldOne, #inputFieldTwo").droppable({
        tolerance:  'pointer',
        hoverClass: 'active',
        activate:   function(event, ui) {
                        $(this).addClass("over");
                    },
        deactivate: function(event, ui) {
                        $(this).removeClass("over");
                    },

        drop:       function(event, ui) {
                        var theValue = ui.draggable.data('colValue');
                        theValue = theValue .replace(/<br>/gi,'; ');
                        console.log("dropped value: " + theValue );  

                        updateText($(this), theValue);
                    }
    });
}
Run Code Online (Sandbox Code Playgroud)

创建一个帮助方法,将文本追加到文本字段(附加一个尾随';'):

function updateText(txtTarget, theValue) {

    var currentValue = txtTarget.val().trim();

    if (currentValue.length > 0 
        && currentValue.substr(currentValue.length-1) !== ";") 
        currentValue = currentValue + '; ';

    currentValue += theValue;


    txtTarget.val(currentValue);
}
Run Code Online (Sandbox Code Playgroud)