Jquery datatable get column index by name

By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I'm using DataTable 1. Columns for the datatable are defined at the initialization step in javascript and each column has a unique name, e.

I know I can get the column from the table by the name I've given it, but I can't seem to find how to find a column's name from the column object. I'm hoping it's possible with the current state of the component.

For example:. You can retrieve the initialization options through table. An example of a click handler that alerts the corresponding column. The following should work for you as per the API document :. Documentation shows that columns. Further examination of the column object shows that it has a column. This should provide the data you need. Learn more.

How to get name of datatable column? Ask Question. Asked 4 years, 7 months ago. Active 2 years, 2 months ago. Viewed 44k times. For example: table. Anna T Anna T 1 1 gold badge 9 9 silver badges 19 19 bronze badges. Active Oldest Votes. Accepting this as the part with the every would work for the example I've given in the question.

I can't be the only one who finds their api so backwards right? I needed table. James Jithin James Jithin 8, 3 3 gold badges 28 28 silver badges 48 48 bronze badges. That would probably work, but I'm hoping for a more DataTable-centric access solution - the class that is iterating through the columns only has DT object reference, it wouldn't have access to the tableColumns array.

Get Column Name Of The Selected Cell In Kendo Grid Using jQuery

Ozan Ozan 3, 2 2 gold badges 14 14 silver badges 22 22 bronze badges.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It only takes a minute to sign up. I have 2 tables. One holds only first column of my "data table" rest is stored in another table, that is placed right to first. What I need to do is to get all the data from specific row to pass it to jqplot.

My tables look like this. My code basically works, but I think it can be improved. First off, you should never have a selector with anything left of an id - searches for ids are about the fastest you can get on the DOM. Searching by tag is also pretty fast, so we use the id-element as our base for that. Next, you should cache elements you found, use more than once and which don't change.

And usually, it's a bad choice to bind events to the elements directly. I'm assuming jQuery 1. Next, accessing the DOM is rather slow.

jquery datatable get column index by name

If you need the data for jqplot, do you need the table headers and can the data change? I still don't like the need for index. I like it even less than event binding on elements, so I'll compromise:. This is still a little messy because we never call offbut as we only use jQuery to change the DOM, we rely on it to unbind event handlers if we change elements.

It's probably better to change the returned object so it has two functions: one to refresh the cache and one to rebind. Otherwise, this leaks event handlers. If you update the table infrequently, you should be fine. For frequent updates, I'd change it back to using the index function again.

Sign up to join this community. The best answers are voted up and rise to the top. Home Questions Tags Users Unanswered. Select row based on another table row index Ask Question. Asked 7 years, 8 months ago.

Active 3 years, 3 months ago. Viewed 88k times. Misiu Misiu 1 1 gold badge 5 5 silver badges 15 15 bronze badges. Active Oldest Votes. Anyway, I'd store all data in an array beforehand. Edit: Here's an example for what I described in my comment below. Arne Arne 4 4 silver badges 12 12 bronze badges. I'm still trying to learn jQuery and I would like to learn the best practices at start, so any advice's like Your are very helpful.In my ASP.

On the client side, I am using jquery to read the values of each column in every row of the DataTable in the json object, I am able to get them by iterating as shown below. Any help is much appreciated.

In the above statement, you are actually specifying the names of the columns as this. Here's a simple example.

jquery datatable get column index by name

Secondly, this article explained how to iterate through object keys and values, you could read it. How to get the names of the columns in DataTable which in turn is in a json object using javascript or jquery? Print Share Twitter Facebook Email. How to get the names of the columns in DataTable which in turn is in a json object using javascri Re: How to get the names of the columns in DataTable which in turn is in a json object using java Substring 0, HeadStr.

Replace Dt. Substring 0, Sb. Length - 1 ; if Sb. Append "]" ; return StripControlChars Sb. Thanks for your time and your knowledge that's great to find any help here.

Is there a way you can retrieve the names of the columns? Fei Han - MSThis article talks about a common requirement in enterprise dashboards where you have a bunch of columns in a table and you want to show hide columns, depending on the checkbox you selected. The approach is to create a bunch of checkboxes that match the number of columns in the table, as well as assign the same class as that of the checkboxes to the header as well as cells of each column in the table. The complete markup can be viewed in the source code accompanying this article :.

Start by caching the selector that selects all checkboxes inside the div grpChkBox. Then check all checkboxes at page load.

jquery datatable get column index by name

This will give you some performance improvement especially if you are using the code on a page that has a couple of tables. This line prevents the code to search through every DOM element to find the table and class names; rather it only looks for children rows under the table someTable. In the beginning, I told you that we are assigning the same class to the cells of each column as that of the checkboxes.

So far, we looked at an ideal scenario where the cells and header of the table were marked with the same class name corresponding to the checkboxes. But what if there are no classes marked on the individual cells and basically you are feeling lazy to go ahead and add classes on all the rows.

In this case, just mark the header row with classes corresponding to the ones on the checkboxes and use the solution demonstrated in this recipe. Observe this markup subset where the classes have been removed from all rows in the table body.

The complete markup can be viewed in the source code accompanying this article. As you can observe, we have changed a couple of things as compared to our previous example. The first change is I have cached two selectors, the first that references the table and the other that references only the header of the table.

Once we have the table header we need, we retrieve the index of the column header. For this we use index. As the jQuery documentation states " If no argument is passed to the. In our case, index function returns index based on the filter we provide, starting with 0 for first table header, 1 for second table header and so on.

jQuery How to Get Table Cell Value TD Value [4 ways]

The nth-child selector is fantastic. All you need to give it, is an integer like nth-child 1 and it will select all rows index.If you are trying to get row number or toiling to get column number using jQuery, then I have the answers for you. In this article, I am going to show you how to get row index and column index using jQuery with examples.

Here is the HTML source code common to all the following examples:. When you run the above code and click on any cell, it will give you the index of that cell. So for example, if you click on 3rd Row 3rd Column cell, it will show you the result as:. No, the code is not buggy. Remember that index starts from 0 similar to arrays index.

In the above code, all we are doing is adding 1 to the index of the row number and cell number to give you the exact numbers. So if you would like to get row index and column index without any manipulations, then use the code given in Example 1.

If you would rather get the row number and column numbers in easy to understand format without having to mentally add 1 to get the correct row number and column number, then use the code in Example 2.

You can use any of the codes as you see fit. Feel free to share your thoughts. Toggle navigation E xtreme W eb D esigns. Search Extreme Web Designs. Try Demo. Read Comments Write A Comment. Jun 8, In jQueryTips and Tricks. They are helpful. Can this examples in my projects for free.Sponsored by and others. Copy code. Please tell us why you want to mark the subject as inappropriate. Maximum characters. Report Inappropriate Cancel. From : guest. Content :. Type the characters you see in the picture below.

Send Cancel. Subject :. Send Update Cancel. Email ID. Attach files. Desktop Google Docs. Each Attachment size should not exceed 1. Max no of attachments : 3. Loading User Profile Response title. Publish Back to edit Cancel. Sign In. New to this Portal?

c# get datatable row column value by column name

Click here to Sign up. You can also use the below options to login. Search jQuery Search.The DataTables columns and column also optionally cells and cell methods provide the ability to select columns from the table. Which columns are selected and how the selector actually operates is controlled by this column-selector data type. The column selector can be given in a number of different forms, to make it easy to apply to your data and use case:.

If no selector is given more specifically undefinedthen all columns are selected. DataTables stores each column internally with a column index for fast look up of column information. When the selector is given as an integer, this value represents a column data index columns.

Note that this is the column data index and not the visible index. The data index is fixed regardless of column visibility, while the visible index will change as the visibility of columns are changed see below for a visible index selector.

The visible index of a column is the index when hidden columns are taken into account. This can be useful when working with event handlers and some columns are hidden, or can be hidden by the end user. This selector is simply a modification of the above integer type with the string :visIdx or :visible postfixed.

For example: 3:visIdx. DataTables provides the ability to assign a name to columns through the columns.

Get Selected Cell column name

The :name selector provides the ability to selector columns based on the assigned name. This selector is simply a string the column name with :name postfixed. For example salary:name. Note that assigned column names need not be unique although that you would normally wish them to be so. If a selector is given which matches multiple columns from the same name they will all be selected. When the selector is given as a string, it is treated as a jQuery selector that operates on the th and td elements of the column headers in the table.

Each column has only one cell which is used as the column header - see orderCellsTop for information on how DataTables selected the cells to use for the column headers, if there is more than one possible cell for each column header in the table's thead element.

For full information about the options available for jQuery selectors, please refer to the jQuery selector documentation. Note that just like jQuery selector, is it possible to supply multiple selectors using comma separated notation i.

jquery datatable get column index by name

This can be useful for getting data from a column, or performing other column based operations, when you have only the DOM node for reference, for example in an event handler. For complete control over which columns are selected, it is possible to provide a function with logic you've defined to perform the selection. That logic can be as simple or as complex as you wish, performing the selection by simply returning true if the column should be included in the selected results and false if not.

The function is called once for every column that can be selected, based on the selector-modifier options, which also defines the order of the data passed in as the second argument for the called function. Very similar to the above node type since a jQuery object is an array-like list of DOM nodesa jQuery instance can be given as a column selector, with any nodes which are selected by jQuery and match those available in the table's header cells.

Please note that when working with a jQuery selected set of columns and hidden columns, jQuery itself will not select the column header cells which have been hidden as DataTables removes them from the document when the column is hidden.