简体   繁体   中英

datatables Column width trying to divide evenlly

Working on the Datatables Code and I had specified the like this:

{className: "editable alignCenter", "targets":"_all"}

Basically all my columns are coming dynamically, so i want to target the editable only the column which are , i had a dataattribute where i can identify the field is primarykey or identity field.

So i want to modify that if that field is defined, leaving that field, all other fields should be editable and that field can be left out

trying like this

var editableTargets = $('#layout').attr('data-array'); - this is coming as a comma seperated list like 0,1,2,3,4,5

"columnDefs": [
            {className: "editable alignCenter", "targets": [editableTargets]}
        ]

but it seems to be not working

@andrewjames Thanks for the quick tip, it is working,

Now i am trying to find the last array from the editableTargets and remove the class editable aligncenter and add a different class to it and add a sorting: false to it

just give me a start

Regarding your updated notes:

Now i am trying to find the last array from the editableTargets and remove the class editable aligncenter and add a different class to it and add a sorting: false to it

Again, I may have misunderstood, but... here are some pointers to help you:

Assume you have this:

var editableTargets  = "0,1,2,3,4,5";

The following code will split this into two arrays - one containing only the final element, and the other containing everything else:

// convert to an array of numbers:
var array = editableTargets.split(",").map(Number);
// remove the last element, and capture it as "i":
var i = array.pop();
// create an array containing the last element:
var lastItem = [];
lastItem.push(i);

So, array is [0,1,2,3,4] and lastItem is [5] .

There may be better ways to do this in JavaScript, but this should work.

Now you should be able to use these in your column defs section - something like this (but I have not tested this part!):

"columnDefs": [
  { className: "editable alignCenter", "targets": array },
  { sorting: false, "targets": lastItem }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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