Solved

DataView Web Part - Datatables JQuery. Removing filtering/sorting from SharePoint

Posted on 2016-10-10
9
74 Views
Last Modified: 2016-10-15
We moved to SharePoint Online and I am trying to use some code I put together to pull in a Dataview web part and then have the table search able using Datatables.js

I was having trouble figuring out how to turn off the filtering and the sorting that is native to the SharePoint webpart so I opted to hide that row and replace it was a new row that is generated by script.

However, it appears that when Datatables is staging the table to display it is ignoring this new row that was added programatically.

I had to find the table based on a custom attribute and then force an ID attribute and that is all working. I just can't get the new row to become part of the DataTable display.

This is the code.  Any suggestions?

$(document).ready(function() 
{

$('table[summary="2CIA4_Rules "]').attr('id','ruletable');
$('#ruletable > tbody').prepend('<tr><td>Rule</td><td>Owner</td><td>Audience</td><td>Curriculum</td><td>Target Area</td><td>Leader</td><td>Leader_WWID</td><td>MRC</td><td>Legal</td><td>JobFunction</td><td>SubFunction</td><td>Exclusions</td><td>Year 4 Notes</td><td>Comments</td><td>ChangeNotes</td></tr>');

$( ".ms-listviewtable" ).DataTable( {
		dom: 'Tf<"clear">rtip',
        "scrollY":        "600px",
		"scrollCollapse": true,
		"paging":         false
} );


var table = $( ".ms-listviewtable" ).DataTable();

//table.column(13).visible( false );
//table.column(14).visible( false );
 
} );

Open in new window

0
Comment
Question by:aehrenwo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41837089
My first guess is that this is a timing thing.  JavaScript does not wait for the prepend to complete before moving on to initializing the datatable.

One way to test is to put an alert('whatever') just after the prepend line.  That will force JS to wait until you click OK before initializing the datatable.

If the new row now appears in the datatable, the permanent solution would be to use a promise on the line that prepends the row.

I'm not sure if this is going down the right track so I'll stop here until you've had a chance to test the theory using an alert.
0
 

Author Comment

by:aehrenwo
ID: 41837175
When the alert triggers I see the column headers, when I click OK datatables loads and the same behavior occurs and the create row disappears. I have some other CSS affecting the display of the original table header from SharePoint but that class should have nothing to do with the newly create row.

Any ideas? Do you need to see anything else from my code?

Regards,

Adam
0
 
LVL 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41837187
OK, that doesn't sounds like a timing issue then.  I'll play around will what you've posted so far and see if I can come up with any other ideas.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41837201
New theory.

You are attempting to init the datatable twice, which throws an error.  datatables only allows one init, unless the first table is destroyed before the second one is created.  In the code you posted above, line 7 and 15 both init the datatable.  You can consolidate the two lines by keeping line 7 but add var table = before it.

When I remove the second init on line 15, the added row shows up fine in the table.
Here is a Fiddle Demo.  Click the button to make the magic happen.
0
 

Author Comment

by:aehrenwo
ID: 41837310
zephyr,

Thank you for looking into this further. However, my scenario is a little different as the row that I am appending is actually the header row. I am hiding the SharePoint header row because it has the filtering and sorting function that is competing with DataTables.

I am not sure if this is what is causing my problem because I removed the 2nd init and I am still not getting the new header to persist.


 $(document).ready(function() 
{

$('table[summary="2CIA4_Rules "]').attr('id','ruletable');
$('#ruletable > tbody').prepend('<tr class="ruleheader"><td>Rule</td><td>Owner</td><td>Audience</td><td>Curriculum</td><td>Target Area</td><td>Leader</td><td>Leader_WWID</td><td>MRC</td><td>Legal</td><td>JobFunction</td><td>SubFunction</td><td>Exclusions</td><td>Year 4 Notes</td><td>Comments</td><td>ChangeNotes</td></tr>');

var table = $( ".ms-listviewtable" ).DataTable( {
		dom: 'Tf<"clear">rtip',
        "scrollY":        "600px",
		"scrollCollapse": true,
		"paging":         false
} ); 
} );

Open in new window


I am doing this to hide the SharePoint specific header row.  Should I use JavaScript to kill the row instead of hiding it?

.ms-viewheadertr {

display:none;	
	
}

Open in new window

0
 

Author Comment

by:aehrenwo
ID: 41837332
Actually I just double checked and it prepended the header but it actually is now at the bottom!

Any idea why it is prepending there?
0
 
LVL 43

Accepted Solution

by:
zephyr_hex (Megan) earned 500 total points
ID: 41837435
DataTables requires proper HTML syntax for the header row:

<table>
   <thead>
      <tr><th>Header</th></tr>
   </thead>
   <tbody>
      <tr><td>Table Row</td></tr>
   </tbody>
</table>

Open in new window


If you want a header row, you should be adding the row to thead, and use <th> tags instead of <td>

Here's an updated Fiddle Demo.
0
 

Author Comment

by:aehrenwo
ID: 41838951
thanks I got this to work by adding the required thead tag back into the prepend.

THANKS!
0
 
LVL 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41838990
wahoo!  glad you got it working.

you can close the question by selecting the solution that answered you question.
0

Featured Post

Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question