2

I'm using the latest version 1.10.0 of Datatables Plugin for JQuery and need to have some columns editable and some not. Since datatables does not offer this functionality out of the box and following directions from here I added in my columns initialization/specification a class readonly that I use to check against e.g.

"drawCallback": function(settings) {
  var dt = this;
  $('tbody td[class != "readonly"]').editable("/@context/edit/do",

This works but it is insufficient for my needs because I need to filter when readonly occurs as a substring of the class i.e. there are other classes as well e.g. readonly sorting_1. I have tried doing the following but it fails:

"drawCallback": function(settings) {
  var dt = this;
  $('tbody td[class.indexOf("readonly") == -1]').editable("/@context/edit/do",

with error:

Error: Syntax error, unrecognized expression: tbody td[class.indexOf("readonly") == -1]

How can this be done?

4

1 回答 1

1

You can't use JavaScript expressions in selectors. What you want is probably jQuery's .filter() method, which allows you to pass it a function which returns true if an item in the set of matched elements should be kept.

Using this, your code can be re-written like this:

$('tbody td').filter(function(){
    return !$(this).hasClass('readonly')
}).editable("/@context/edit/do",

In the filtering function, I check if the <td> has the class readonly, and by reversing this check (using !), the function will return false whenever it has that class, resulting in the removal of the "read-only" <td> element(s) from the list that gets passed to the editable method.

于 2014-05-19T16:58:50.950 回答