如何为列设置 Column-Toggle Table Widget 的默认值?

2022-01-18 00:00:00 widget javascript jquery-mobile

我一直在玩弄 jQuery Mobile 的 Table Widget.有没有办法可以通过此小部件从表头名称设置列的显示隐藏状态?如果没有这样的方法,那么解决此类问题的最佳方法是什么?

解决方案

jQM 没有为此提供 out-of-the-box 解决方案,因此您必须通过 JS 来完成.column toggle 弹出窗口继承了 table 的 id 后跟 -popup.您需要在 tablecreate 事件触发以更改 checkbox(es) 属性时定位它.

请注意,只有带有 data-prioritythead 元素会被添加到 column toggle 弹出窗口中.此外,您需要通过 index 使用 .eq() 方法或 :eq()<来定位 checkbox(es)/code> 选择器.

$(document).on("pagebeforecreate", "#pageID", function () {$("#tableID").on("tablecreate", function () {$("#tableID-popup [type=checkbox]:eq(1)")/* 第二个复选框 */.prop("checked", false)/* 取消选中 */.checkboxradio("refresh")/* 刷新 UI */.trigger("改变");/* 触发更改更新表 */});});

<块引用>

演示

I have been toying around with the Table Widget for jQuery Mobile. Is there a way I can set the show-hide status of a column from its table header name via this widget? If there is no such way what will be the best solution for such a problem?

解决方案

jQM doesn't offer an out-of-the-box solution for this, therefore, you have to do it through JS. The column toggle popup inherits table's id followed by -popup. You need to target it when tablecreate event fires to change checkbox(es) property.

Note that only thead elements with data-priority will be added to column toggle popup. Moreover, you will need to target checkbox(es) by their index using .eq() method or :eq() selector.

$(document).on("pagebeforecreate", "#pageID", function () {
   $("#tableID").on("tablecreate", function () {
       $("#tableID-popup [type=checkbox]:eq(1)") /* second checkbox */
           .prop("checked", false)            /* uncheck it */
           .checkboxradio("refresh")          /* refresh UI */
           .trigger("change");                /* trigger change to update table */
   });
});

Demo

相关文章