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

发布于 2025-01-01 13:25:57 字数 410 浏览 0 评论 0原文

我想将一行从 JQGrid 拖动到文本输入字段,并将列的文本从拖放的行添加到输入中文本的末尾。

显然,这与答案相去甚远,但是从设置了此设置的网格中拖动一行(其中 #inputTextField 是一个“可放置”文本字段)会导致 JavaScript 错误 this.p 未定义:

$("#searchResultsGrid").jqGrid('gridDnD',
    {
         connectWith:   '#inputTextField"
    }
);

这是因为目标显然不是 JQGrid,并且没有定义 this.p。我尝试了一些不同的事情......也许有一种方法可以“欺骗”放置事件以使其工作?非常感谢您的帮助:)

I would like to drag a row from a JQGrid to a text input field and add a column's text from the dropped row to the end of the text in the input.

Obviously this is a long way from the answer, but dragging a row from a grid with this set up on it (where #inputTextField is a 'droppable' text field) results in the JavaScript error this.p is undefined:

$("#searchResultsGrid").jqGrid('gridDnD',
    {
         connectWith:   '#inputTextField"
    }
);

This is because the destination is obviously not a JQGrid and does not have this.p defined. I've tried a few different things...maybe there is a way I can 'trick' the drop event into working? Thank you so much for any help :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日暮斜阳 2025-01-08 13:25:57

我想通了!!首先,使网格行可拖动(应在 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);
                        }
        });
    }

然后,创建可放置元素:

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);
                    }
    });
}

创建一个辅助方法以将文本附加到文本字段(附加尾随“;”):

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);
}

I figured it out!! First, make the grid rows draggable (this function should be called in your gridComplete grid event handler):

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);
                        }
        });
    }

Then, create droppable elements:

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);
                    }
    });
}

Create a helper method to append text to text field (appending a trailing ';'):

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文