简体   繁体   English

在循环中创建对象文字数组

[英]Create an array of object literals in a loop

I'm using a jQuery plugin to sort columns - tablesorter . 我正在使用jQuery插件对列tablesorter进行排序。

It lets you disable headers using options. 它使您可以使用选项禁用标题。 At the moment I disable headers 2 & 3 by passing options when initialising the plugin: 目前,我在初始化插件时通过传递选项来禁用标头2和3:

$("#mytable").tablesorter({
  headers: {
    1: {  
      sorter: false 
    },
    2: { 
      sorter: false 
    }
  } 
});

I'd like to do it dynamically, by checking if a class "header" exists; 我想通过检查类“ header”是否存在来动态地进行操作; if not I would disable the functionality. 如果没有,我将禁用该功能。

Here is my jsfiddle 这是我的jsfiddle

Any idea on how I could do it dynamically? 我如何动态地做任何想法?

You can get all the theade th elements without the class header and then create a dynamic object like 您可以获取没有类header所有theade th元素,然后创建一个动态对象,例如

var headers = {};
$('#mytable thead th').not('.header').each(function () {
    headers[$(this).index()] = {
        sorter: false
    };
})

console.log(headers)

$("#mytable").tablesorter({
    headers: headers
});

Demo: Fiddle 演示: 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM