gpt4 book ai didi

javascript - 无法拖动工作

转载 作者:行者123 更新时间:2023-12-03 09:28:13 24 4
gpt4 key购买 nike

我正在尝试实现与 this 非常相似的东西

但是,我使用的是 table (dataTable()),而不是他们正在使用的 div

似乎将数据打包到传递给 drop 容器/回调的对象中的方法找不到我的任何 external-event

因此,在我的 drop 回调中,我最终得到一个 undefined eventObject

function populateWorkOrders(){

var workOrdersTableBody = document.getElementById("workOrdersTableBody");//Link to the containing element using getElementById or similar

for (i=0;i<workOrders.length;i++) { //Loops for the length of the list

var tr=document.createElement('tr'); //Creates <ul> element
tr.setAttribute("class","external-event");
tr.setAttribute("draggable","true");


var workOrderId=document.createElement('td'); //Chrome seems not to like the variable "name" in this instance
workOrderId.innerHTML=workOrders[i].workOrderId; //Adds name
tr.appendChild(workOrderId);

var workOrderTitle=document.createElement('td');
workOrderTitle.innerHTML=workOrders[i].workOrderTitle;
tr.appendChild(workOrderTitle);

var workOrderAccount=document.createElement('td');
workOrderAccount.innerHTML=workOrders[i].workOrderAccount;
tr.appendChild(workOrderAccount);

var workOrderPriority=document.createElement('td');
workOrderPriority.innerHTML=workOrders[i].workOrderPriority;
tr.appendChild(workOrderPriority);

workOrdersTableBody.appendChild(tr);
}
//THIS IS WHATS NOT RUNNING AND IT SEEMS THAT IT CANT FIND ANYTHING
$('#external-events tr.external-event').each(function() {

var eventObject = {
title: "fulltitle"//$.trim($(this).data) // use the element's text as the event title
};

// store the Event Object in the DOM element so we can get to it later
$(this).data('eventObject', eventObject);

// make the event draggable using jQuery UI
$(this).draggable({
zIndex: 999,
revert: true, // will cause the event to go back to its
revertDuration: 0 // original position after the drag
});

});


$('#workOrdersTable').dataTable();

}

这最终产生了这个 enter image description here

似乎在类名后附加了“奇数”和“偶数”。为了摆脱这个问题,我可以将所有内容包围在 div 标记中(在表格下方),但这会破坏 DataTables() 并且仍然无法解决问题。

这是 html:

    <title>TITLE</title>
<script type="text/javascript" src="js/libs/jquery/jquery.js"></script>
<script type="text/javascript" src="js/indexJs.js"></script>
</head>
<body>
<div>
<!--had to load this separate from full calender lib else there were conflicts-->
<script type="text/javascript" src="js/libs/datatables/media/js/jquery.dataTables.js"></script>
<table class="display" id="workOrdersTable">
<thead>
<tr>
<th>Id</th>
<th>Title</th>
<th>Account</th>
<th>Priority</th>
</tr>
</thead>
<tbody id="workOrdersTableBody">
</tbody>
</table>
</div>
<script>
populateWorkOrders();
</script>
<br>
<div>
<select id="technicianServiceTeamSelectorDropDown" style="width:25%" onchange="populateTechnicians();"></select>
<br>
<select id="technicianSelectorDropDown" style="display:none; width:25%" onchange="populateCalendar()"></select>
<script>
populateTechniciansServiceTeams();
</script>
</div>
<br>
<div id='calendar'>
<!--had to load this separate from data tables lib else there were conflicts-->
<script type="text/javascript" src='js/libs/fullcalendar/lib/jquery.min.js'></script>
<script type="text/javascript" src='js/libs/fullcalendar/lib/moment.min.js'></script>
<script type="text/javascript" src='js/libs/fullcalendar/fullcalendar.js'></script>
</div>
</body>

所以上面显示了可拖动元素。这是掉落的代码。

$(document).ready(function() {
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
editable: false,
droppable: true, // this allows things to be dropped onto the calendar !!!
drop: function(date, allDay) { // this function is called when something is dropped

// retrieve the dropped element's stored Event Object
var originalEventObject = $(this).data('eventObject');
console.log(originalEventObject);
// we need to copy it, so that multiple events don't have a reference to the same object
var copiedEventObject = $.extend({}, originalEventObject);

// assign it the date that was reported
//copiedEventObject.title = "h";
copiedEventObject.start = date;
copiedEventObject.allDay = true;

// render the event on the calendar
// the last `true` argument determines if the event "sticks" (http://arshaw.com/fullcalendar/docs/event_rendering/renderEvent/)
$('#calendar').fullCalendar('renderEvent', copiedEventObject, true);

}
}); });

最佳答案

首先,我在您提供的图像上的任何位置都没有看到 id="external-events",所以我不确定绑定(bind)设置在哪里。您是否在开发过程中的某个时刻更改了 id 名称?

其次,当我过去做过类似的事情时,我发现最简单的方法就是创建一个包含实际 html 代码的字符串,如下所示:

    var html = '<td id="' + theId + '" class="' + theClass + '" >' + someText + '</td>';

然后最后使用 jquery .append() 或 .html() 取决于到底发生了什么:

    $('#myTable').append(html);

我不知道这是否对您有帮助,但我已经通过这种方法取得了很大的成功。

对于设置可拖动事件,有几个选项: 如果您需要的话,可以使用 jQuery 的可拖动事件。我之前也手动完成过这一切,使用 .on('mousedown'.....) 启动事件,然后使用 .on('mousemove', ...) 更改 left 属性,然后.on('mouseup', ...) 结束它。当可拖动对象更加复杂时,这对我来说效果更好。

希望这有帮助。

关于javascript - 无法拖动工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31597386/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com