在表格中选择一个类进行拖动

时间:2012-07-16 14:57:47

标签: javascript jquery ajax jquery-ui

我有一张桌子。在里面我有一个名为drag的类。我需要拖动那个班级。我该怎么拖呢?

//Counter
counter = 0;
//Make element draggable
$('.drag').draggable({
  revert: 'invalid',
  helper: 'clone',
  containment: 'frame',
  cursorAt: { left: -1, top: -1 },
  //When first dragged
  stop: function (ev, ui) {
    //code here
  }
});

以下是我的表格。我应该如何拖动桌子?

<div class="options">
  <div>
    <table id="gvEventDetails" cellspacing="0" border="1"
      style="border-collapse:collapse;" rules="all">
      <tbody>
        <tr><th scope="col">EventID</th></tr>
        <tr>
          <td>
            <div id="drag0" class="drag"
              style="background-color:RGB(30,140,70)" height:32px;=""
              width:32px;="" position:absolute;="">Event1
            </div>
          </td>
        </tr>
        <tr>
          <td>
            <div id="drag1" class="drag"
              style="background-color:RGB(40,30,255)" height:32px;=""
              width:32px;="" position:absolute;="">Event2
            </div>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

2 个答案:

答案 0 :(得分:0)

将它放入http://jsfiddle.net/ABt8h/后,我的代码看不出任何问题..

我在你自己的代码中做了一些调试。也许你的js中有一个重叠选择器?

答案 1 :(得分:0)

我使用ajax控件加载并动态创建div的id。所以它不起作用。 因为ajax contol会在javascript加载后加载。

 $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Eventcalander.aspx/BindDatatable",
                data: "{}",
                dataType: "json",
                success: function (data) {
                    for (var i = 0; i < data.d.length; i++) {

  $("#gvEventDetails").append("<tr><td><div width:32px; height:32px; id= drag" + i + " class=\"dragg\" style= background-color:" + data.d[i].Color + ">" + data.d[i].EventID + "</div></td></tr>");

                    }
                },
                error: function (result) {
                    alert("Error");
                }

            });

在代码中,我像这样使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;

namespace DragDropDynamically
{
    public partial class Eventcalander : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindColumnToGridview();
            }

        }

        /// <summary>
        /// This method is used to bind dummy row to gridview to bind data using JQuery
        /// </summary>
        private void BindColumnToGridview()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("EventID");
            dt.Rows.Add();
            gvEventDetails.DataSource = dt;
            gvEventDetails.DataBind();
            gvEventDetails.Rows[0].Visible = false;
        }

        [WebMethod]
        public static EventDetails[] BindDatatable()
        {
            DataTable dt = new DataTable();
            List<EventDetails> lstEventdetails = new List<EventDetails>();

            using (SqlConnection con = new SqlConnection("Server=CTSINDLFVMOSS;Database=databasename;uid=userid;pwd=password"))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT TOP 1000 [EventID],[EventStartDate],[EventEndDate],[EventPeriod],[Feature],[Display],[FeatureandDisplay],[TPR],[Color] FROM [DB_CGTPO_DEVE].[dbo].[HistoryEvent]", con))
                {
                    con.Open();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(dt);
                    foreach (DataRow dtrow in dt.Rows)
                    {

                        eventdetails.EventID = dtrow["EventID"].ToString();

                        eventdetails.Color = dtrow["Color"].ToString().Trim();


                        lstEventdetails.Add(eventdetails);
                    }
                }
            }
            return lstEventdetails.ToArray();
        }
        public class EventDetails
        {
            public string EventID { get; set; }
            public string Color { get; set; }
        }
    }
}