可调整表格列的大小

发布于 2024-09-28 07:01:44 字数 239 浏览 0 评论 0原文

我正在开发一个网络应用程序,并正在寻找一种创建我自己的数据网格的方法。

我知道有很多很棒的脚本,具有所有的花哨功能,但我需要自己的特定功能、CSS 样式以及在其中使用我自己的 ui 控件的能力。

实际上,我唯一需要的是调整列大小的能力。我并不真正关心标记是否不维护行结构或者不是语义,因为所有数据都将由 ajax 请求填充。

我在想一个可能的解决方案是让每一列成为一个 div。

有教程可以帮助我吗?

I'm developing a web app and am looking for a way to create my own datagrids.

I know that there are lots of fantastic scripts out there with all the bells and whistles, but I need my own specific functionality, css styling, and the ability to use my own ui controls in it.

Really, the only thing I need is the ability to resize the columns. I don't really care if the markup doesn't maintain the row structure or isn't semantic, because all of the data will be populated by ajax requests.

I was thinking a possible solution would be to make each column a div.

Is there a tutorial out there that can help me?

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

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

发布评论

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

评论(1

怕倦 2024-10-05 07:01:44

我建议使用 jQuery UI Ressized 并进行一些增强。该插件实际上只专注于调整大小并支持完全自定义,因为您可以在任何情况下添加自己的回调函数。然而,默认情况下,该插件无法调整表头的大小,但我可以使其使用有效的 HTML 运行,并在调整大小时更新表的 COLGROUP 区域。

具体的想法是:

  1. HTML 表格在其 COLGROUP 区域中指定初始宽度,并具有 CSS 属性 table-layout:fixed。
  2. 使用 jQuery UI 的 .ressized() 装饰 TH 元素。
  3. 调整大小开始时:找到活动 TH 的匹配 COL 元素并记住原始宽度。
  4. 调整大小时:计算调整大小增量并更新(增加/减少)选定的 COL 元素。这将根据每个浏览器调整整个列的大小。

插件初始化:

$(".resizable th").resizable({
 handles: "e",

 // set correct COL element and original size
 start: function(event, ui) {
   var colIndex = ui.helper.index() + 1;
   colElement = table.find("colgroup > col:nth-child(" +
   colIndex + ")");

   // get col width (faster than .width() on IE)
   colWidth = parseInt(colElement.get(0).style.width, 10);
   originalSize = ui.size.width;
 },

 // set COL width
 resize: function(event, ui) {
   var resizeDelta = ui.size.width - originalSize;

   var newColWidth = colWidth + resizeDelta;
   colElement.width(newColWidth);

   // height must be set in order to prevent IE9 to set wrong height
   $(this).css("height", "auto");
 }
});

我描述了完整的解决方案,包括 JavaScript、HTML 标记、跨浏览器 CSS 和 Live-Demo,网址为 http://ihofmann.wordpress.com/2012/07/31/ressized-table-columns-with-jquery-ui/

I recommend to use jQuery UI Resizable with some enhancements. The plugin really focuses only on resizing and enables full customization since you can add your own callback functions at any event. By default, however, the plugin cannot resize table headers, but I could make it running with valid HTML, updating the table's COLGROUP area on resize.

The concrete idea would be:

  1. The HTML table specifies the initial width in its COLGROUP area and has CSS property table-layout:fixed.
  2. Decorate TH elements with jQuery UI’s .resizable().
  3. On resizing start: Find the matching COL element of active TH and remember original width.
  4. On resizing: Calculate the resize delta and update (increase/decrease) the selected COL element. This will resize the whole column with every browser.

Plugin init:

$(".resizable th").resizable({
 handles: "e",

 // set correct COL element and original size
 start: function(event, ui) {
   var colIndex = ui.helper.index() + 1;
   colElement = table.find("colgroup > col:nth-child(" +
   colIndex + ")");

   // get col width (faster than .width() on IE)
   colWidth = parseInt(colElement.get(0).style.width, 10);
   originalSize = ui.size.width;
 },

 // set COL width
 resize: function(event, ui) {
   var resizeDelta = ui.size.width - originalSize;

   var newColWidth = colWidth + resizeDelta;
   colElement.width(newColWidth);

   // height must be set in order to prevent IE9 to set wrong height
   $(this).css("height", "auto");
 }
});

I described the full solution including JavaScript, HTML markup, cross-browser CSS and Live-Demo at http://ihofmann.wordpress.com/2012/07/31/resizable-table-columns-with-jquery-ui/

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