Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Support more than column_number, like columnDefs.targets #513

Closed
VictorioBerra opened this issue Nov 6, 2018 · 9 comments

Comments

@VictorioBerra
Copy link

VictorioBerra commented Nov 6, 2018

Just like columnDefs.targets it would be nice to support a class name.

I have a set of default <th> columns across a few of my tables, some define them in different orders. Because of this, I use the string target type so my defs are matched up to the column name and everything just works.

Being forced to provide a 0-based index to column_number throws a wrench in my setup. I now have to iterate the <th> columns and store my YADCF definitions in such a way that they can be retrieved by a name, and then I must assign numbers. This is some pretty unfortunate boilerplate.

Please support a target options that can take a string.

@vedmack
Copy link
Owner

vedmack commented Nov 12, 2018

Please provide a minimal code sample to explain what do you exactly mean / which to work that way...

@VictorioBerra
Copy link
Author

VictorioBerra commented Nov 12, 2018

Desired Feature:

<table>
    <tr>
        <th class="Description">Description</th>
        <th class="Name">Name</th>
    </tr>
</table>
  var myTable = $('#example').DataTable();
  
  yadcf.init(myTable, [
    { targets : '.Name' }
]);
});

Notice how to init a yadcf column I can now use targets: '.classname'.

Imagine now, that I can re-use the yadcf.init all around my code base and the order of my columns no longer matter.

I do have a temporary work around.

/**
* Takes a list of YADCF definitions and filters them down if it can locate a column on the table with that target
* The target should usually be a class without the "."
* @param { DataTable } dataTableApi The target data table to trigger the edit against.
* @param { Array } YADCFDefinitions The array of YADCF definitions.
* @returns { Array } The array of YADCF definitions for matching column targets.
*/
function FilterYADCFDefinitionsByColumnDefsTarget(dataTableApi, YADCFDefinitions) {
return YADCFDefinitions.filter(function (YADCFDefinition) {

    var column = dataTableApi.column(YADCFDefinition.targets);

    // targets lets you pass a class name as a string (without the ".")
    // column-selector strings are essentially jQuery selectors. So we add the "." back.
    if (jQuery.type(YADCFDefinition.targets) === "string" &&
        YADCFDefinition.targets !== "_all" &&
        YADCFDefinition.targets.indexOf(".") === -1) {

        column = dataTableApi.column("." + YADCFDefinition.targets);
    }

    if (column.index() >= 0) {
        YADCFDefinition.column_number = column.index();
        return YADCFDefinition;
    }

});
}

To use this, you have to add an additional param to your yadcf definitions called targets. This will be passed as a column-selector to the data table API. If a hit is found the column index in the table is returned and passed to the yadcf definitions column_number .

@vedmack
Copy link
Owner

vedmack commented Nov 13, 2018

Please post a minimal working example (jsfiddle / etc)

@VictorioBerra
Copy link
Author

@vedmack

http://jsfiddle.net/Lord_Zero/7zx8htof/40/

Both tables share the same columnDefs, and the same yadcf options. But the HTML tables define the columns in different orders. This is currently not possible with yadcf because the library forces us to provide the column index instead of letting us provide any [column-selector](https://datatables.net/reference/type/column-selector) we want.

@vedmack
Copy link
Owner

vedmack commented Nov 19, 2018

Try 0.9.4.beta.10, see it in action here

Usage is as follows (notice its not camel case since from the start I used _ separated:

column_selector: '.Description',

@VictorioBerra
Copy link
Author

@vedmack Great job on this! I appreciate it. I am also thankful that you pass the column_selector option directly to .column() thereby making it more flexible for us.

When do you expect to do a release?

@vedmack
Copy link
Owner

vedmack commented Nov 19, 2018

@VictorioBerra, you are welcome... if you referring the npm module update - then I will update the beta on npm the day after tomorrow

p.s

Thanks for your code that I based this feature on

@VictorioBerra
Copy link
Author

Checking in, any word on the NPM package?

vedmack added a commit that referenced this issue Nov 22, 2018
…ility in column selectors (instead of hard coded column_number)

#513
@vedmack
Copy link
Owner

vedmack commented Nov 22, 2018

Heya...
npm install yadcf@beta

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants