2

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

SignUp
  • 45
  • 7
  • If I understood the question correctly, you have a string called `editableTargets` containing this: `"0,1,2,3,4,5"`. You need this to be converted into an array of integers, so you can use it like this: `"targets": myIntArray`. See this question: [Convert string with commas to array](https://stackoverflow.com/questions/13272406/convert-string-with-commas-to-array). – andrewJames Mar 28 '20 at 16:14
  • yes, you are right – SignUp Mar 28 '20 at 18:48
  • But with the change is happening. I am trying further few modifications – SignUp Mar 28 '20 at 18:57

1 Answers1

1

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 }
]
andrewJames
  • 19,570
  • 8
  • 19
  • 51