最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can't Change Search Box Width Or Position For DataTable - Stack Overflow

programmeradmin5浏览0评论

I have a DataTable which i have been styling/setting up and i'm almost there but i just cant figure out the last few styling things i'm after for the search input

  • Left align the input
  • Make field wider
  • Set focus on load
  • Make field same size as other fields on site

I have the following code i'm using

JQuery

$('#dialPlanListTable').dataTable({
    "ordering": true,               // Allows ordering
    "searching": true,              // Searchbox
    "paging": true,                 // Pagination
    "info": false,                  // Shows 'Showing X of X' information
    "pagingType": 'simple_numbers', // Shows Previous, page numbers & next buttons only
    "pageLength": 10,               // Defaults number of rows to display in table
    "columnDefs": [
        {
            "targets": 'dialPlanButtons',
            "searchable": false,    // Stops search in the fields 
            "sorting": false,       // Stops sorting
            "orderable": false      // Stops ordering
        }
    ],
    "dom": '<"top"f>rt<"bottom"lp><"clear">', // Positions table elements
    "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]], // Sets up the amount of records to display
    "language": {
        "search": "_INPUT_",            // Removes the 'Search' field label
        "searchPlaceholder": "Search"   // Placeholder for the search box
    }
});

Current look

HTML Returned/Rendered

<div class="top">
    <div id="dialPlanListTable_filter" class="dataTables_filter">
        <label>
            <input type="search" class="form-control input-sm" placeholder="Search" aria-controls="dialPlanListTable">
        </label>
    </div>
</div>

As you can see the search box is smaller than the one above (which will be removed once this one is styled) and is not left to the table. I have looked at the / site and cant find the last few things i need.

I'd prefer NOT to have to update my .css as i don't want to affect the reset of the site, just this page only so dont mind using JQuery to add styling. also the input is sat inside the label as shown in the HTML above

I have a DataTable which i have been styling/setting up and i'm almost there but i just cant figure out the last few styling things i'm after for the search input

  • Left align the input
  • Make field wider
  • Set focus on load
  • Make field same size as other fields on site

I have the following code i'm using

JQuery

$('#dialPlanListTable').dataTable({
    "ordering": true,               // Allows ordering
    "searching": true,              // Searchbox
    "paging": true,                 // Pagination
    "info": false,                  // Shows 'Showing X of X' information
    "pagingType": 'simple_numbers', // Shows Previous, page numbers & next buttons only
    "pageLength": 10,               // Defaults number of rows to display in table
    "columnDefs": [
        {
            "targets": 'dialPlanButtons',
            "searchable": false,    // Stops search in the fields 
            "sorting": false,       // Stops sorting
            "orderable": false      // Stops ordering
        }
    ],
    "dom": '<"top"f>rt<"bottom"lp><"clear">', // Positions table elements
    "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]], // Sets up the amount of records to display
    "language": {
        "search": "_INPUT_",            // Removes the 'Search' field label
        "searchPlaceholder": "Search"   // Placeholder for the search box
    }
});

Current look

HTML Returned/Rendered

<div class="top">
    <div id="dialPlanListTable_filter" class="dataTables_filter">
        <label>
            <input type="search" class="form-control input-sm" placeholder="Search" aria-controls="dialPlanListTable">
        </label>
    </div>
</div>

As you can see the search box is smaller than the one above (which will be removed once this one is styled) and is not left to the table. I have looked at the https://datatables/ site and cant find the last few things i need.

I'd prefer NOT to have to update my .css as i don't want to affect the reset of the site, just this page only so dont mind using JQuery to add styling. also the input is sat inside the label as shown in the HTML above

Share Improve this question edited Feb 21, 2019 at 11:08 murday1983 asked Feb 21, 2019 at 10:53 murday1983murday1983 4,03617 gold badges61 silver badges110 bronze badges 2
  • please add a code snippet for the same here to replicate. – Shiv Kumar Baghel Commented Feb 21, 2019 at 10:57
  • @ShivKumarBaghel Not sure what your after, i've given all details, screenshots of how it looks and HTML rendered for the search field, nothing more i can provide – murday1983 Commented Feb 21, 2019 at 11:07
Add a ment  | 

2 Answers 2

Reset to default 6

Not what i was hoping for but resolved by doing the below

    $('#dialPlanListTable').dataTable({
        "ordering": true,               // Allows ordering
        "searching": true,              // Searchbox
        "paging": true,                 // Pagination
        "info": false,                  // Shows 'Showing X of X' information
        "pagingType": 'simple_numbers', // Shows Previous, page numbers & next buttons only
        "pageLength": 10,               // Defaults number of rows to display in table
        "columnDefs": [
            {
                "targets": 'dialPlanButtons',
                "searchable": false,    // Stops search in the fields 
                "sorting": false,       // Stops sorting
                "orderable": false      // Stops ordering
            }
        ],
        "dom": '<"top"f>rt<"bottom"lp><"clear">', // Positions table elements
        "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]], // Sets up the amount of records to display
        "language": {
            "search": "_INPUT_",            // Removes the 'Search' field label
            "searchPlaceholder": "Search"   // Placeholder for the search box
        },
        "search": {
            "addClass": 'form-control input-lg col-xs-12'
        },
        "fnDrawCallback":function(){
            $("input[type='search']").attr("id", "searchBox");
            $('#dialPlanListTable').css('cssText', "margin-top: 0px !important;");
            $("select[name='dialPlanListTable_length'], #searchBox").removeClass("input-sm");
            $('#searchBox').css("width", "300px").focus();
            $('#dialPlanListTable_filter').removeClass('dataTables_filter');
        }
    });

So got the look i was going for

Solution 1

You can create a custom textbox (in this case you have full control of styling ) in the top of the table and hide the default text box.

  <p>
      <input type="text" id="mySearchText" placeholder="Search...">
      <button id="mySearchButton">Search</button>
   </p>


  var table =  $('#dialPlanListTable').dataTable({
        "ordering": true,               // Allows ordering
        "searching": false,              // Searchbox
        "paging": true,                 // Pagination
        "info": false,                  // Shows 'Showing X of X' information
        "pagingType": 'simple_numbers', // Shows Previous, page numbers & next buttons only
        "pageLength": 10,               // Defaults number of rows to display in table
        "columnDefs": [
            {
                "targets": 'dialPlanButtons',
                "searchable": false,    // Stops search in the fields 
                "sorting": false,       // Stops sorting
                "orderable": false      // Stops ordering
            }
        ],
        "dom": '<"top"f>rt<"bottom"lp><"clear">', // Positions table elements
        "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]], // Sets up the amount of records to display
        "language": {
            "search": "_INPUT_",            // Removes the 'Search' field label
            "searchPlaceholder": "Search"   // Placeholder for the search box
        }
    });
 $('#mySearchButton').on( 'keyup click', function () {
    table.search($('#mySearchText').val()).draw();
  } );

Solution 2

based on their documentation http://datatables/examples/basic_init/dom.html you can add a custom class to the search box container( for example myCustomClass)

   "dom": '<"myCustomClass"f>rt<"bottom"lp><"clear">', // Positions table elements

You can customize the look of the search box by adding styles on that class

.myCustomClass{
background-color:red    
}
发布评论

评论列表(0)

  1. 暂无评论